github.com/kristofferahl/go-centry@v1.5.0/README.md (about) 1 # centry 2 3 Declarative **command line builder** for teams and one man bands 4 5 ## Use cases 6 7 - Build a feature rich CLI from scratch using only scripts and yaml 8 - Bundle existing scripts and tools as a single CLI 9 - Encode best practices and team conventions for using scripts and dev/ops tools 10 11 ## Feature highlights 12 13 - **Declarative**: Add new commands and options and run them at once. No need to re-compile. 14 - **Unified syntax**: Provides a standard for using commands and options. 15 - **Supports multi level commands**: `mycli get status` and `mycli get files` 16 - **Supports multi level options (flags)**: `mycli --dev get status --out json` 17 - **Contextual help**: `mycli get --help` 18 - **Autocomplete**: Bash-completions of commands and options 19 - **Highly configurable**: Sensible defaults, lots of choises 20 - **Easy setup**: Download centry, create a manifest file and you are good to go 21 - **Generated documentation**: Markdown documentation generated from your CLI 22 23 ## Full documentation 24 25 - v1 - [./docs/index.md](./docs/index.md) 26 27 ## Install 28 29 ### Mac 30 31 ```bash 32 curl -L https://github.com/kristofferahl/go-centry/releases/download/v1.0.0/go-centry_1.0.0_Darwin_x86_64.tar.gz | tar -xzv -C /usr/local/bin/ 33 ``` 34 35 ### Linux 36 37 ```bash 38 curl -L https://github.com/kristofferahl/go-centry/releases/download/v1.0.0/go-centry_1.0.0_Linux_x86_64.tar.gz | tar -xzv -C /usr/local/bin/ 39 ``` 40 41 ## Getting started 42 43 **The documentation and examples below assumes that** 44 45 1. You are running `bash` version 3.2 or later 46 1. You have "installed" the `go-centry_*` binary for your OS and made it available in your path as `mycli` (by renaming the file) 47 1. You have created an empty directory to hold your commands and manifest file 48 49 ## Setup 50 51 1. Create the manifest file for the CLI and name it `centry.yaml` by running the following command in your shell. 52 ```bash 53 echo "commands: [] 54 config: 55 name: mycli" > centry.yaml 56 ``` 57 2. Verify that it's working by running 58 ``` 59 mycli --help 60 ``` 61 This should display the contextual help for the cli and the name **mycli** at the top. 62 63 ## The manifest file 64 65 This is where you define root level commands and options, do configuration overrides and import scripts to be available for all your commands. 66 67 By default, `centry` will look for a `centry.yaml` file in the **current directory**. You may change the location and name of the manifest file but this requires you to let centry know where to find it. This can be done by setting the environment variable `CENTRY_FILE` or by way of passing `--centry-file <path>` as the **first** argument. 68 69 ## Commands 70 71 In `centry`, commands are simple shell scripts with a matching function name in it. 72 73 Let's start by creating a file called `hello.sh` with the following content. 74 75 _`// file: hello.sh`_ 76 77 ``` 78 #!/usr/bin/env bash 79 80 hello() { 81 echo 'Hello centry' 82 } 83 ``` 84 85 Before you can use the `hello` function as a command, you need to tell `centry` where to find it. Open `centry.yaml` in an editor of choise and modify it to look like this: 86 87 _`// file: centry.yaml`_ 88 89 ```yaml 90 commands: 91 - name: hello 92 path: ./hello.sh 93 description: Says hello 94 95 config: 96 name: mycli 97 ``` 98 99 You should now be able to able to run the command. 100 101 ```bash 102 mycli hello ↵ 103 Hello centry 104 ``` 105 106 ## Options 107 108 In `centry`, options are flags you use to pass named arguments to your command functions. This enables easier discovery of your cli and less friction for users. They may be specified in long (`--option`) or short (`-o`) form. 109 110 Let's add a `--name` option to the hello command. This is done by adding `annotations` in your script. Edit `hello.sh` to look like this. 111 112 _`// file: hello.sh`_ 113 114 ``` 115 #!/usr/bin/env bash 116 117 # centry.cmd[hello].option[name]/type=string 118 hello() { 119 echo "Hello ${NAME}" 120 } 121 ``` 122 123 Running the `hello` command again would look like this 124 125 ```bash 126 mycli hello ↵ 127 Hello 128 ``` 129 130 To pass a name to be echoed back to you, call the command with the `--name` option. 131 132 ```bash 133 mycli hello --name William ↵ 134 Hello William 135 ``` 136 137 If you want to add a description for the `--name` option you should add an additional annotation to the `hello.sh` file. 138 139 _`// file: hello.sh`_ 140 141 ``` 142 #!/usr/bin/env bash 143 144 # centry.cmd[hello].option[name]/type=string 145 # centry.cmd[hello].option[name]/description=Name to be greeted with 146 hello() { 147 echo "Hello ${NAME}" 148 } 149 ``` 150 151 Displaying the contextual help (using the `--help` option) should now look something like this. 152 153 ```bash 154 mycli hello --help ↵ 155 NAME: 156 mycli hello - Says hello 157 158 USAGE: 159 mycli hello [command options] [arguments...] 160 161 OPTIONS: 162 --name value Name to be greeted with 163 --help, -h Show help (default: false) 164 ``` 165 166 ## Arguments 167 168 A command may also accept any number of arguments. All arguments not matching an option of a command will be passed on to the function. 169 170 _`// file: hello.sh`_ 171 172 ``` 173 #!/usr/bin/env bash 174 175 # centry.cmd[hello].option[name]/type=string 176 # centry.cmd[hello].option[name]/description=Name to be greeted with 177 hello() { 178 echo "Hello ${NAME}" 179 echo "Arguments (${#*}): ${*}" 180 } 181 ``` 182 183 NOTE: Arguments must always be passed after the last option. 184 185 ```bash 186 mycli hello --name William arg1 arg2 ↵ 187 Hello William 188 Arguments (2): arg1 arg2 189 ``` 190 191 ## Autocomplete 192 193 **NOTE: Only available for Bash** 194 195 To make discovery of `mycli` easier, we may want to enable bash completions. Follow the steps below to set it up. 196 197 ```bash 198 curl -o bash_autocomplete https://raw.githubusercontent.com/kristofferahl/go-centry/master/bash_autocomplete 199 PROG=mycli source bash_autocomplete 200 ``` 201 202 Now, let try it out by typing `mycli` followed by a space and then hit `tab`. This will display any command available at the root level. If there is only one, the command name will be autocompleted. It works for options too. 203 204 ```bash 205 mycli -- ➡ 206 --centry-config-log-level --centry-quiet --help 207 ```