|
| 1 | +# Adding a new CLI command |
| 2 | + |
| 3 | + |
| 4 | +The mobile CLI uses the [cobra](https://github.com/spf13/cobra) library to provide a consistent framework for building out the CLI tool. |
| 5 | + |
| 6 | + |
| 7 | +## Adding a new base command |
| 8 | +### What is a base command |
| 9 | +A base command is a command that works with an entirely new resource or that adds a new verb i.e (get, delete) |
| 10 | +### 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``` |
| 13 | +Once this is done, then you should follow the patterns in the other base commands: |
| 14 | + |
| 15 | +- Create a new type ```<resourceName>Cmd``` |
| 16 | +- Create new constructor ```New<resourceName>Cmd``` |
| 17 | +- Dependencies such as the kubernetes clients should be passed to this constructor as their interface types to allow for simpler testing |
| 18 | +- The cobra commands should then be returned from methods from this the base type. See below: |
| 19 | + |
| 20 | +``` |
| 21 | +type MyResourceCMD struct{} |
| 22 | +
|
| 23 | +func (mr *MyResourceCMD)ListMyResourceCMD()*cobra.Command{} |
| 24 | +
|
| 25 | +``` |
| 26 | + |
| 27 | +This command should then be wired up in ```main.go``` inside the ```cmd/mobile``` pkg. |
| 28 | + |
| 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 |
| 32 | + |
| 33 | +Example of adding a new resource command: |
| 34 | + |
| 35 | +``` |
| 36 | +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 | + ) |
| 48 | + |
| 49 | + |
| 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 | +
|
| 65 | +``` |
| 66 | + |
| 67 | + |
| 68 | +Example of adding a new verb command |
| 69 | + |
| 70 | + |
| 71 | +``` |
| 72 | +// 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 | + } |
| 82 | +
|
| 83 | +``` |
| 84 | + |
| 85 | + |
| 86 | +### Adding a new command to an existing resource type |
| 87 | + |
| 88 | +You would do this when adding additional functionality to an existing resource type: |
| 89 | + |
| 90 | +- First add a new method to the resource type under <resourceName.go> |
| 91 | +``` |
| 92 | +
|
| 93 | +func (cbc *ClientBuildsCmd) ListClientBuildsCmd() *cobra.Command { |
| 94 | + cmd := &cobra.Command{ |
| 95 | + Use: "clientbuild", |
| 96 | + Short: "get a specific clientbuild for a mobile client", |
| 97 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 98 | + ... |
| 99 | + }, |
| 100 | + } |
| 101 | + return cmd |
| 102 | +} |
| 103 | +
|
| 104 | +``` |
| 105 | + |
| 106 | +You should also then add the testcase for this command: |
| 107 | + |
| 108 | +``` |
| 109 | +func TestClientBuildsCmd_ListClientBuildsCmd(t *testing.T) { |
| 110 | +... |
| 111 | +
|
| 112 | +} |
| 113 | +
|
| 114 | +``` |
0 commit comments