Skip to content

Commit 99b6a6d

Browse files
authored
Merge pull request #406 from sakshamb2113/HotCommands
Add CLI Feature: Hot commands
2 parents 0a29d6e + 99b22de commit 99b6a6d

4 files changed

Lines changed: 148 additions & 1 deletion

File tree

cmd/flow/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ func main() {
4949

5050
// quick commands
5151
quick.InitCommand.AddToParent(cmd)
52+
quick.DeployCommand.AddToParent(cmd)
53+
quick.RunCommand.AddToParent(cmd)
5254
status.Command.AddToParent(cmd)
5355

5456
// structured commands
@@ -67,7 +69,8 @@ func main() {
6769
cmd.AddCommand(signatures.Cmd)
6870

6971
command.InitFlags(cmd)
70-
72+
// Set usage template to custom template
73+
cmd.SetUsageTemplate("Usage:{{if .Runnable}}\n{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}\n{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}\n\nAliases:\n{{.NameAndAliases}}{{end}}{{if .HasExample}}\n\nExamples:\n{{.Example}}{{end}}{{if .HasAvailableSubCommands}}\n\n{{if (eq .Name \"flow\")}}Hot Commands:\n{{range .Commands}}{{if (and (.IsAvailableCommand) (index .Annotations \"HotCommand\") )}}\n{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}\n\n{{end}}Available Commands:\n{{range .Commands}}{{if (and (or .IsAvailableCommand (eq .Name \"help\")) (not (index .Annotations \"HotCommand\")))}}\n{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}\n\nFlags:\n{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}\n\nGlobal Flags:\n{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}\n\nAdditional help topics:\n{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}\nUse \"{{.CommandPath}} [command] --help\" for more information about a command.{{end}}\n")
7174
if err := cmd.Execute(); err != nil {
7275
util.Exit(1, err.Error())
7376
}

internal/quick/deploy.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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/spf13/cobra"
23+
24+
"github.com/onflow/flow-cli/internal/command"
25+
"github.com/onflow/flow-cli/internal/project"
26+
)
27+
28+
// add deploy command
29+
var DeployCommand = &command.Command{
30+
Cmd: &cobra.Command{
31+
Use: "deploy",
32+
Short: "Deploy all project contracts",
33+
Example: "flow deploy",
34+
Annotations: map[string]string{
35+
"HotCommand": "true",
36+
},
37+
},
38+
Flags: project.DeployCommand.Flags,
39+
RunS: project.DeployCommand.RunS,
40+
}

internal/quick/init.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ var InitCommand = &command.Command{
3232
Use: "init",
3333
Short: "Initialize a new configuration",
3434
Example: "flow project init",
35+
Annotations: map[string]string{
36+
"HotCommand": "true",
37+
},
3538
},
3639
Flags: &config.InitFlag,
3740
Run: config.Initialise, // TODO(sideninja) workaround - init needed to be copied in order to work else there is flag duplicate error

internal/quick/run.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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

Comments
 (0)