项目作者: gbevan

项目描述 :
YAML Parsed to environment variables in #! for shell
高级语言: Go
项目地址: git://github.com/gbevan/yamlsh.git
创建时间: 2018-06-25T19:43:36Z
项目社区:https://github.com/gbevan/yamlsh

开源协议:MIT License

下载


yamlsh - YAML Parsed in #! for shell

(I’m sure there is already a util out there that does this (I couldn’t find one,
though admittedly I didn’t search very hard), but I’m learning the
Go language and needed the practice, so…)

Parse a yaml file and pass as environment variables to a script from #!.

You can download a prebuilt binary from the project’s releases page
here.

Build and install

Requires GOPATH setup and this project cloned out to src/

  1. go get github.com/golang/dep/cmd/dep
  2. dep ensure -v --vendor-only
  3. go build && sudo cp yamlsh /usr/local/bin

Usage

To use within a shell script replace the #! line with:

  1. #!/usr/local/bin/yamlsh --yaml=${MYFILE}
  2. # ...rest of your script goes here...

${MYFILE} will be substituted with the environment variable MYFILE, e.g.:

  1. MYFILE=test.yml ./test.sh

The test.sh script and test.yml file produces this output:

  1. IN SCRIPT
  2. YAMLSH_DICT1_NEST1_FloatNum=3.14
  3. YAMLSH_VAR1=my var 1
  4. YAMLSH_DICT1_NEST1_MultiLinePp=aaaaaaaaaaaaaaaaaaaaaa\nbbbbbbbbbbbbbbbbbbbbbb\ncccccccccccccccccccccc\n
  5. YAMLSH_DICT1_NEST1_MultiLineGt=aaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbb cccccccccccccccccccccc\n
  6. YAMLSH_DICT1_NEST1_ARRAY2_1=array
  7. YAMLSH_DICT1_NEST1_ARRAY2_0=an
  8. YAMLSH_DICT1_NEST1_Number=100
  9. YAMLSH_PREFIX=YAMLSH
  10. YAMLSH_DICT1_NEST1_NEST2=value of NEST2
  11. YAMLSH_ARRAY1_0=val1
  12. YAMLSH_ARRAY1_1=val2

As can be seen, complex yaml structures are flattened for easy use in shell
scripts.

By default variables passed are prefixed with YAMLSH_. To change this use
the YAMLSH_PREFIX environment variable, e.g.:

  1. MYFILE=test.yml YAMLSH_PREFIX=FOO ./test.sh

The shell used defaults to /bin/bash, but you can override to use your
preferred shell via environment variable YAMLSH_SHELL

Likewise you can also set the YAML file to preload using environment
variable YAMLSH_YAMLFILE. However, the --yaml=file.yml will take
precedence.