gramex.secrets

commandline()

Run secrets on the command line to save specific environment variables in a file.

Used in CI tools (e.g. Gitlab/Travis) to save env variables into .secrets.yaml. For example, to export $HOME and all variables starting with $HOME to .secrets.yaml, run:

secrets HOME > .secrets.yaml            # export HOME=... to .secrets.yaml
secrets HOME=/tmp > .secrets.yaml       # export $HOME. If $HOME is not defined, use /tmp
secrets HOME 'GOOGLE*' > .secrets.yaml  # export $HOME and all vars starting with $GOOGLE
Source code in gramex\secrets.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def commandline():
    '''Run `secrets` on the command line to save specific environment variables in a file.

    Used in CI tools (e.g. Gitlab/Travis) to save env variables into `.secrets.yaml`. For example,
    to export `$HOME` and all variables starting with `$HOME` to `.secrets.yaml`, run:

    ```shell
    secrets HOME > .secrets.yaml            # export HOME=... to .secrets.yaml
    secrets HOME=/tmp > .secrets.yaml       # export $HOME. If $HOME is not defined, use /tmp
    secrets HOME 'GOOGLE*' > .secrets.yaml  # export $HOME and all vars starting with $GOOGLE
    ```
    '''
    result = {}
    for pattern in sys.argv[1:]:
        if '=' in pattern:
            key, val = pattern.split('=', 1)
            result[key] = val
        else:
            for key, val in os.environ.items():
                if fnmatch(key, pattern):
                    result[key] = val
    if result:
        print(yaml.dump(result, default_flow_style=False, sort_keys=False).strip())  # noqa