|
| 1 | +/* |
| 2 | +* Flow CLI |
| 3 | +* |
| 4 | +* Copyright 2019-2021 Dapper Labs, Inc. |
| 5 | +* |
| 6 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +* you may not use this file except in compliance with the License. |
| 8 | +* You may obtain a copy of the License at |
| 9 | +* |
| 10 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +* |
| 12 | +* Unless required by applicable law or agreed to in writing, software |
| 13 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +* See the License for the specific language governing permissions and |
| 16 | +* limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package quick |
| 20 | + |
| 21 | +import ( |
| 22 | + "github.com/onflow/flow-cli/internal/command" |
| 23 | + "github.com/onflow/flow-cli/internal/emulator" |
| 24 | + "github.com/onflow/flow-cli/internal/project" |
| 25 | + "github.com/onflow/flow-cli/pkg/flowkit" |
| 26 | + "github.com/onflow/flow-cli/pkg/flowkit/services" |
| 27 | + "github.com/spf13/cobra" |
| 28 | + "sync" |
| 29 | +) |
| 30 | + |
| 31 | +type flagsRun struct { |
| 32 | +} |
| 33 | + |
| 34 | +var runFlags = flagsRun{} |
| 35 | +func DeployHelper(args []string, globalFlags command.GlobalFlags, services *services.Services, wg *sync.WaitGroup) { |
| 36 | + |
| 37 | + for true { |
| 38 | + //check if the server has started |
| 39 | + _, err := services.Status.Ping(globalFlags.Network) |
| 40 | + if err == nil { |
| 41 | + // if the emulator is running run the deploy command |
| 42 | + project.DeployCommand.Cmd.Run(project.DeployCommand.Cmd, args) |
| 43 | + break |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + wg.Done() |
| 48 | + |
| 49 | +} |
| 50 | + |
| 51 | +func EmulatorHelper(args []string, globalFlags command.GlobalFlags, services *services.Services, wg *sync.WaitGroup) { |
| 52 | + // run the emulator |
| 53 | + emulator.Cmd.Run(emulator.Cmd, args) |
| 54 | + wg.Done() |
| 55 | +} |
| 56 | + |
| 57 | +// This command will act as an alias for running the emulator and deploying the contracts |
| 58 | +var RunCommand = &command.Command{ |
| 59 | + Cmd: &cobra.Command{ |
| 60 | + Use: "run", |
| 61 | + Short: "Start emulator and deploy all project contracts", |
| 62 | + Example: "flow run", |
| 63 | + Annotations: map[string]string{ |
| 64 | + "HotCommand": "true", |
| 65 | + }, |
| 66 | + }, |
| 67 | + Flags: &runFlags, |
| 68 | + Run: func( |
| 69 | + args []string, |
| 70 | + _ flowkit.ReaderWriter, |
| 71 | + globalFlags command.GlobalFlags, |
| 72 | + services *services.Services, |
| 73 | + ) (command.Result, error) { |
| 74 | + var waitGroup sync.WaitGroup |
| 75 | + // set number of goroutines |
| 76 | + waitGroup.Add(2) |
| 77 | + go EmulatorHelper(args, globalFlags, services, &waitGroup) |
| 78 | + go DeployHelper(args, globalFlags, services, &waitGroup) |
| 79 | + // wait until completion of the goroutines |
| 80 | + waitGroup.Wait() |
| 81 | + |
| 82 | + emulator.Cmd.Run(emulator.Cmd, args) |
| 83 | + return &RunResult{}, nil |
| 84 | + }, |
| 85 | +} |
| 86 | + |
| 87 | +type RunResult struct { |
| 88 | +} |
| 89 | + |
| 90 | +func (r *RunResult) JSON() interface{} { |
| 91 | + result := make(map[string]string) |
| 92 | + return result |
| 93 | +} |
| 94 | + |
| 95 | +func (r *RunResult) String() string { |
| 96 | + return "" |
| 97 | +} |
| 98 | + |
| 99 | +func (r *RunResult) Oneliner() string { |
| 100 | + return "" |
| 101 | +} |
0 commit comments