github.com/criteo/command-launcher@v0.0.0-20230407142452-fb616f546e98/gh-pages/content/en/docs/quickstart/first-command.md (about) 1 --- 2 title: "Quick Start" 3 description: "Integrate your first command to command launcher" 4 lead: "In this tutorial, we will integrate our first command to command launcher" 5 date: 2022-10-04T17:01:29+02:00 6 lastmod: 2022-10-04T17:01:29+02:00 7 draft: false 8 images: [] 9 menu: 10 docs: 11 parent: "quickstart" 12 identifier: "first-command-884b250e24cf7581b3266bc090a7cfd1" 13 weight: 130 14 toc: true 15 --- 16 17 ## Preparation 18 19 If you want to use the default command launcher binary name (`cola`): install command launcher following the guide [Binary install](../binary-install). Or if you prefer a different binary name, build command launcher from source: [Build from source](../build-from-source) 20 21 > In Criteo, we call it `cdt`, (Criteo Dev Toolkit) 22 23 In this tutorial, we will use the binary name `cola`. 24 25 To check your command launcher installation, run following command: 26 27 ```bash 28 cola config 29 ``` 30 31 It should list all your configurations. The important ones for this tutorial is the following: 32 33 ```json 34 dropin_folder : [command launcher home]/dropins 35 local_command_repository_dirname : [command launcher home]/current 36 ``` 37 38 Default command launcher home: 39 {{< details "MacOS" >}} 40 _/Users/[user_name]/[.binary_name]_ 41 {{< /details >}} 42 {{< details "Linux" >}} 43 _/home/[user_name]/[.binary_name]_ 44 {{< /details >}} 45 {{< details "Windows" >}} 46 _C:\Users\\[user_name]\\[.binary_name]_ 47 {{< /details >}} 48 49 For example, on my Macbook (with user `criteo`), my command launcher home is `/Users/criteo/.cola` 50 51 Command launcher also provide auto-completion feature to all managed commands, you only need to setup auto-completion once: [setup auto-completion](../binary-install/#setup-auto-completion) 52 53 ## Let's build a command 54 55 If you already have a command you can skip this step. Command launcher is technology agnostic to your command. You can build your command in any tech stack that suits your use case. In this tutorial, let's build a simple "Hello World" command line tool with bash scripts. 56 57 In command launcher, commands are packaged into `package`s. Let's first create a package for our newly created command: 58 59 ```bash 60 cd $HOME/.cola/dropins 61 mkdir my-first-package 62 cd my-first-package 63 ``` 64 65 Create a script file `first-command-launcher-cmd.sh`, which prints greeting message according to the language 66 67 ```bash 68 #!/bin/bash 69 70 LANG=${LANG:-en} 71 72 if [ $LANG == "fr" ]; then 73 echo "Bonjour! $1" 74 exit 0 75 else 76 echo "Hello! $1" 77 exit 0 78 fi 79 ``` 80 81 You can run it directly as a normal bash script: 82 83 ```bash 84 $ ./first-command-launcher-cmd.sh "command launcher" 85 Hello! command launcher 86 87 $ LANG=fr ./first-command-launcher-cmd.sh "command launcher" 88 Bonjour! command launcher 89 ``` 90 91 ## Prepare a minimal manifest.mf 92 93 Now we have a working command/script. To make command launcher aware of it, we need to create a manifest file (in JSON or YAML format) at the root folder of the package: 94 95 _manifest.mf_ 96 97 ```yaml 98 pkgName: my-first-package 99 version: 0.0.1 100 cmds: 101 - name: greeting 102 type: executable 103 executable: "{{.PackageDir}}/first-command-launcher-cmd.sh" 104 ``` 105 106 That's it! Your command has been integrated to command launcher with a subcommand named `greeting`, to test it: 107 108 ```bash 109 $ cola greeting "command launcher" 110 Hello! command launcher 111 112 $ LANG=fr cola greeting "command launcher" 113 Bonjour! command launcher 114 ``` 115 116 Command launcher will pass all the environment variables, arguments to itself to your command. 117 118 ## Tell more about your command to command launcher 119 120 We can go even further to turn our bash scripts into a native-like program. Let's add extra information in the manifest, and make some improvements of its user interface: 121 122 - the short and long description 123 - some examples 124 - use a flag to take language input instead of environment variable `LANG` 125 126 ```yaml 127 pkgName: my-first-package 128 version: 0.0.2 129 cmds: 130 name: greeting 131 type: executable 132 short: Simple greeting command 133 executable: "{{.PackageDir}}/first-command-launcher-cmd.sh" 134 requiredFlags: 135 - "language\t greeting language" 136 examples: 137 - scenario: Greeting with default language 138 cmd: greeting [name] 139 - scenario: Specify the greeting language 140 cmd: greeting --language fr [name] 141 checkFlags: true 142 ``` 143 144 The above manifest tells command launcher that the `greeting` command requires a flags called `language`, and let command launcher to check the flags before calling the script 145 146 Now when you run the greeting command with `-h` or `--help`, you will get a nice help message like a native command: 147 148 ```shell 149 $ cola greeting -h 150 Usage: 151 cola greeting [flags] 152 153 Examples: 154 # Greeting with default language 155 greeting [name] 156 157 # Specify the greeting language 158 greeting --language fr [name] 159 160 161 Flags: 162 -h, --help help for greeting 163 --language string greeting language 164 ``` 165 166 But how can our script get the language passed from the `--language` flag? Command launcher passes an environment variable to your script with the name `CDT_FLAG_LANGUAGE` (More details see [checkFlags](../../overview/manifest/#checkflags)). You can modify your script to get the language from it like so: 167 168 ```bash 169 #!/bin/bash 170 171 LANG=${COLA_FLAG_LANGUAGE:-en} 172 173 if [ $LANG == "fr" ]; then 174 echo "Bonjour! $1" 175 exit 0 176 else 177 echo "Hello! $1" 178 exit 0 179 fi 180 ``` 181 182 Now we have turned our bash script into a native-like command. 183 184 ## Auto-completion 185 186 Command launcher will automatically enable auto-completion for your command. Subcommand, flags, arguments will will be auto-completed when you type `[TAB][TAB]`, for example: 187 188 ```shell 189 $ cola g[TAB][TAB] 190 greeting - Simple greeting command 191 grepx - Enhanced grep command 192 193 $ cola greeting --[TAB][TAB] 194 $ cola greeting --language 195 ```