| 1 | # How to add a new Command |
| 2 | |
| 3 | Before making any changes, please consult the [CLI style guide](https://github.com/urfave/cli)! |
| 4 | |
| 5 | ### Package Layout |
| 6 | |
| 7 | The [`urfave/cli`](https://github.com/urfave/cli) package that forms the basis |
| 8 | of Step CLI supports N-levels of command hierarchy. Each level of the hierarchy |
| 9 | should exist within its own package if possible. For example, `version` and |
| 10 | `help` exist inside their own packages inside the top-level `command` package. |
| 11 | |
| 12 | Any package used by a command but does not contain explicit business logic |
| 13 | directly related to the command should exist in the top-level of this |
| 14 | repository. For example, the `github.com/smallstep/cli/flags` and |
| 15 | `github.com/smallstep/cli/errs` package are used by many different commands and |
| 16 | contain functionality for defining flags and creating/manipulating errors. |
| 17 | |
| 18 | ### Adding a Command |
| 19 | |
| 20 | Once you figured out where to add the command inside the package hierarchy you |
| 21 | must register the command. This way the command *can* be made available if |
| 22 | desired inside the `cmd/step/main.go`. |
| 23 | |
| 24 | An example of defining a command and registering it: |
| 25 | |
| 26 | ```golang |
| 27 | package validate |
| 28 | |
| 29 | import ( |
| 30 | "github.com/urfave/cli" |
| 31 | |
| 32 | "github.com/smallstep/cli/command" |
| 33 | "github.com/smallstep/cli/flags" |
| 34 | ) |
| 35 | |
| 36 | func init() { |
| 37 | cmd := cli.Command{ |
| 38 | Name: "validate", |
| 39 | Usage: "Returns whether or not the provided token is valid", |
| 40 | Flags: []cli.Flag{ |
| 41 | flags.Token("The one-time token value to validate"), |
| 42 | }, |
| 43 | Action: validate, |
| 44 | } |
| 45 | |
| 46 | command.Register(validate) |
| 47 | } |
| 48 | ``` |
| 49 | |
| 50 | Once this is done, you then must import the pkg inside `cmd/step/main.go` so |
| 51 | the packages `init` method is run appropriately. This only needs to be done for |
| 52 | top-level commands. |
| 53 | |
| 54 | ```golang |
| 55 | package main |
| 56 | |
| 57 | import ( |
| 58 | "github.com/urfave/cli" |
| 59 | |
| 60 | _ "github.com/smallstep/cli/validate" |
| 61 | ) |
| 62 | ``` |
| 63 | |
| 64 | This will ensure that the `smallstep/cli/validate` package is initialized |
| 65 | and thus registered with the `smallstep/cli/command`. |
| 66 | |
| 67 | ### Usage, Flags, Errors, and Prompts |
| 68 | |
| 69 | There are three packages which contain functionality to make writing commands easier: |
| 70 | |
| 71 | - `github.com/smallstep/cli/usage` |
| 72 | - `github.com/smallstep/cli/flags` |
| 73 | - `github.com/smallstep/cli/prompts` |
| 74 | - `github.com/smallstep/cli/errs` |
| 75 | |
| 76 | The usage package is used to extend the default documentation provided by |
| 77 | `urfave/cli` by enabling us to document arguments, whether they are optional or |
| 78 | required, and ensuring they're printed out as a part of the `step help` or |
| 79 | `step <command> -h` flow. If you need to add a different type of annotation to |
| 80 | document an argument just add it to the `usage.Argument` struct! |
| 81 | |
| 82 | When you add a flag, look into the pre-existing ones inside the `flags` |
| 83 | package. Could you use one of the pre-existing flags in order to reduce |
| 84 | duplication? If not, make sure to add a flag so it could be used in future! |
| 85 | |
| 86 | The `errs` package contains functionality for defining and working with errors |
| 87 | to ensure they are mutated properly into a `urfave/cli.ExitError` which ensures |
| 88 | the process returns an appropriate exit code on termination. When you create an |
| 89 | error, consider whether or not it's general and could be predefined inside the |
| 90 | `errs` package. Errors that are specific to the command itself should exist |
| 91 | only inside that commands respective package. |
| 92 | |
| 93 | The `prompts` package is a small wrapper around the various different types of |
| 94 | prompts used by the commands. If you need a new prompt, consider adding a new |
| 95 | function to this package to tailor the prompt for the step cli. This way other |
| 96 | commands can adopt the step aesthetic as new functionality is introduced. |
| 97 | |
| 98 | ### Hiding a Command |
| 99 | |
| 100 | Sometimes it's desirable to prevent a command from showing up in the help menu |
| 101 | because it's been deprecated *or* it's not ready for users to leverage. This |
| 102 | can be achieved by setting the `Hidden` property on the `cli.Command` struct to |
| 103 | `true`. |