1
1
# Adding a new CLI command
2
2
3
-
4
3
The mobile CLI uses the [ cobra] ( https://github.com/spf13/cobra ) library to provide a consistent framework for building out the CLI tool.
5
4
6
5
7
6
## Adding a new base command
8
7
### What is a base command
9
8
A base command is a command that works with an entirely new resource or that adds a new verb i.e (get, delete)
10
9
### Changes required to add a new base command
11
- to add a new base command that operates on a new resource type you should add a new file under the ``` pkg/cmd ``` directory
12
- named ``` <resourceName>.go ``` with an accompanying test file ``` <resourceName>_test.go ```
10
+ To add a new base command that operates on a new resource type you should add a new file under the ``` pkg/cmd ``` directory
11
+ named ``` <resourceName>.go ``` with an accompanying test file ``` <resourceName>_test.go ``` .
13
12
Once this is done, then you should follow the patterns in the other base commands:
14
13
15
14
- Create a new type ``` <resourceName>Cmd ```
16
15
- Create new constructor ``` New<resourceName>Cmd ```
17
16
- Dependencies such as the kubernetes clients should be passed to this constructor as their interface types to allow for simpler testing
18
17
- The cobra commands should then be returned from methods from this the base type. See below:
19
18
20
- ```
19
+ ``` go
21
20
type MyResourceCMD struct {}
22
21
23
- func (mr *MyResourceCMD)ListMyResourceCMD()*cobra.Command{}
24
-
22
+ func (mr *MyResourceCMD ) ListMyResourceCMD () *cobra .Command {}
25
23
```
26
24
27
25
This command should then be wired up in ``` main.go ``` inside the ``` cmd/mobile ``` pkg.
28
26
29
- If it is being added to an existing verb command then add this command in the same way as the other commands
30
-
31
- If it is a new verb command then you will want to create a new block of it and its sub commands
27
+ If it is being added to an existing verb command then add this command in the same way as the other commands. If it is a new verb command then you will want to create a new block of it and its sub commands.
32
28
33
29
Example of adding a new resource command:
34
30
35
- ```
31
+ ``` go
36
32
var (
37
- out = os.Stdout
38
- rootCmd = cmd.NewRootCmd()
39
- clientCmd = cmd.NewClientCmd(mobileClient, out)
40
- bindCmd = cmd.NewIntegrationCmd(scClient, k8Client)
41
- serviceConfigCmd = cmd.NewServiceConfigCommand(k8Client)
42
- clientCfgCmd = cmd.NewClientConfigCmd(k8Client)
43
- clientBuilds = cmd.NewClientBuildsCmd()
44
- svcCmd = cmd.NewServicesCmd(scClient, k8Client, out)
45
- // new command added here
46
- myResource = cmd.NewMyResourceCmd(scClient,k8Client, out)
47
- )
33
+ out = os.Stdout
34
+ rootCmd = cmd.NewRootCmd ()
35
+ clientCmd = cmd.NewClientCmd (mobileClient, out)
36
+ bindCmd = cmd.NewIntegrationCmd (scClient, k8Client)
37
+ serviceConfigCmd = cmd.NewServiceConfigCommand (k8Client)
38
+ clientCfgCmd = cmd.NewClientConfigCmd (k8Client)
39
+ clientBuilds = cmd.NewClientBuildsCmd ()
40
+ svcCmd = cmd.NewServicesCmd (scClient, k8Client, out)
41
+ // new command added here
42
+ myResource = cmd.NewMyResourceCmd (scClient,k8Client, out)
43
+ )
48
44
45
+ // create
46
+ {
47
+ createCmd := cmd.NewCreateCommand ()
48
+ createCmd.AddCommand (svcCmd.CreateServiceInstanceCmd ())
49
+ createCmd.AddCommand (bindCmd.CreateIntegrationCmd ())
50
+ createCmd.AddCommand (clientCmd.CreateClientCmd ())
51
+ createCmd.AddCommand (serviceConfigCmd.CreateServiceConfigCmd ())
52
+ createCmd.AddCommand (clientBuilds.CreateClientBuildsCmd ())
53
+ // sub command added here
54
+ createCmd.AddCommand (myResource.CreateMyResourceCmd ())
49
55
50
- // create
51
- {
52
- createCmd := cmd.NewCreateCommand()
53
- createCmd.AddCommand(svcCmd.CreateServiceInstanceCmd())
54
- createCmd.AddCommand(bindCmd.CreateIntegrationCmd())
55
- createCmd.AddCommand(clientCmd.CreateClientCmd())
56
- createCmd.AddCommand(serviceConfigCmd.CreateServiceConfigCmd())
57
- createCmd.AddCommand(clientBuilds.CreateClientBuildsCmd())
58
- // sub command added here
59
- createCmd.AddCommand(myResource.CreateMyResourceCmd())
60
-
61
- rootCmd.AddCommand(createCmd)
62
-
63
- }
64
-
56
+ rootCmd.AddCommand (createCmd)
57
+ }
65
58
```
66
59
67
60
68
- Example of adding a new verb command
61
+ Example of adding a new verb command:
69
62
70
63
71
- ```
64
+ ``` go
72
65
// twist
73
- {
74
-
75
- twistCmd := cmd.NewTwistCmd()
76
- twistCmd.AddCommand(myResource.TwistMyResourceCmd())
77
-
78
- //important to add it to the root command
79
- rootCmd.AddCommand(twistCmd)
80
-
81
- }
66
+ {
67
+ twistCmd := cmd.NewTwistCmd ()
68
+ twistCmd.AddCommand (myResource.TwistMyResourceCmd ())
69
+
70
+ // important to add it to the root command
71
+ rootCmd.AddCommand (twistCmd)
72
+ }
82
73
83
74
```
84
75
85
-
86
76
### Adding a new command to an existing resource type
87
77
88
78
You would do this when adding additional functionality to an existing resource type:
89
79
90
80
- First add a new method to the resource type under <resourceName.go>
91
- ```
92
81
82
+ ``` go
93
83
func (cbc *ClientBuildsCmd ) ListClientBuildsCmd () *cobra .Command {
94
84
cmd := &cobra.Command {
95
85
Use: " clientbuild" ,
@@ -100,15 +90,12 @@ func (cbc *ClientBuildsCmd) ListClientBuildsCmd() *cobra.Command {
100
90
}
101
91
return cmd
102
92
}
103
-
104
93
```
105
94
106
95
You should also then add the testcase for this command:
107
96
108
- ```
97
+ ``` go
109
98
func TestClientBuildsCmd_ListClientBuildsCmd (t *testing .T ) {
110
99
...
111
-
112
100
}
113
-
114
- ```
101
+ ```
0 commit comments