github.com/criteo/command-launcher@v0.0.0-20230407142452-fb616f546e98/gh-pages/content/en/docs/overview/variable.md (about)

     1  ---
     2  title: "Variable in manifest"
     3  description: "Use variables in manifest.mf file"
     4  lead: "Use variables in manifest.mf file"
     5  date: 2022-10-02T21:36:35+02:00
     6  lastmod: 2022-10-02T21:36:35+02:00
     7  draft: false
     8  images: []
     9  menu:
    10    docs:
    11      parent: "overview"
    12      identifier: "variable-d940a4460a129d45a4ae1158e21b2130"
    13  weight: 999
    14  toc: true
    15  ---
    16  
    17  The two common use cases of integrating commands in command launcher are:
    18  
    19  1. Reference files that are located in the package itself
    20  2. Provide system/architecture-aware commands, for example, .sh script for linux, and .bat script for windows
    21  
    22  To cover these use cases, in certain fields of the manifest file, predefined variables can used in the field values.
    23  
    24  ## Available Variables
    25  
    26  | Variable Name   | Variable Description                                                   |
    27  |-----------------|------------------------------------------------------------------------|
    28  | PackageDir      | The absolute path of the package                                       |
    29  | Root            | Same as "PackageDir" variable                                          |
    30  | Cache           | Same as "PackageDir" variable                                          |
    31  | Os              | The OS, "windows", "linux", and "darwin"                               |
    32  | Arch            | The system architecture: "arm64", "amd64"                              |
    33  | Binary          | The binary file name of the command launcher                           |
    34  | Extension       | The system-aware binary extension, "" for linux, ".exe" for windows    |
    35  | ScriptExtension | The system-aware scritp extension, ".sh" for linux, ".bat" for windows |
    36  
    37  ## Fields that accepts variables
    38  
    39  The command fields: `executable`, `args`, and `validArgsCmd`
    40  
    41  ## How to use these variables
    42  
    43  You can reference them in form of `{{.Variable}}`. For example:
    44  
    45  ```json
    46  "cmds": [
    47    {
    48      "name": "variable-demo",
    49      "type": "executable",
    50      "executable": "{{.PackageDir}}/bin/script{{.ScripteExtension}}",
    51    }
    52  ]
    53  ```
    54  
    55  The executable on linux will be a script called `script.sh` located in the bin folder of the package. On windows, the executable will be a script called `script.bat`.
    56  
    57  ## If Else
    58  
    59  One common scenario is to have different path or file name according to different OS. You can use condition (if-else) in the fields that accept variables. For example:
    60  
    61  ```json
    62  "cmds": [
    63    {
    64      "name": "variable-demo",
    65      "type": "executable",
    66      "executable": "{{.PackageDir}}/bin/script{{if eq .Os \"windows\"}}.ps1{{else}}.sh{{end}}",
    67    }
    68  ]
    69  ```
    70  
    71  The executable on linux will be a script called `script.sh` located in the bin folder of the package. On windows, the executable will be a script called `script.ps1`.
    72  
    73  ## Advanced usage of variables
    74  
    75  See golang [text/template](https://pkg.go.dev/text/template) for advanced usage.