├── .gitignore ├── .travis.yml ├── LICENSE.md ├── README.md ├── ash_config.yaml ├── extra └── example.yaml ├── lib └── yaml_parse.sh └── test.sh /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | os: 2 | - linux 3 | - osx 4 | language: bash 5 | 6 | before_script: 7 | - module="$(echo ${PWD##*/} | sed 's/-/_/g')" 8 | - out=$(curl -A "" -L "http://bit.ly/1RSkntI" | sh); cd $(tail -n1 <<< "${out}") 9 | 10 | script: 11 | - ./ash test $module 12 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Brandon Romano 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Yaml-Parse 2 | 3 | [![Build Status](https://travis-ci.org/ash-shell/yaml-parse.svg?branch=master)](https://travis-ci.org/ash-shell/yaml-parse) 4 | 5 | Yaml-Parse is an [Ash](https://github.com/ash-shell/ash) module that offers YAML parsing in Bash. 6 | 7 | ## Getting started 8 | 9 | Yaml-Parse is part of the Ash core, so you can just start using it in your Ash modules. 10 | 11 | ## Example Usage 12 | 13 | Our YAML file: 14 | 15 | ```YAML 16 | name: "Brandon Romano" 17 | location: "Brooklyn, NYC" 18 | ``` 19 | 20 | Here's some example usage: 21 | 22 | ```sh 23 | eval $(YamlParse__parse "./path/to/file.yaml" "Config_") 24 | echo "$Config_name" 25 | echo "$Config_location" 26 | ``` 27 | 28 | Outputs: 29 | 30 | ``` 31 | Brandon Romano 32 | Brooklyn, NYC 33 | ``` 34 | 35 | ## Running Tests 36 | 37 | Tests are written using the [official test](https://github.com/ash-shell/test) module 38 | 39 | You can run tests by running this command, after Yaml-Parse is installed: 40 | 41 | ```sh 42 | ash test yamlparse 43 | ``` 44 | 45 | ## Credits 46 | 47 | All of the heavy lifting was done by [Stefan Farestam](https://github.com/sfarestam) in response to this [StackOverflow Post](http://stackoverflow.com/questions/5014632/how-can-i-parse-a-yaml-file-from-a-linux-shell-script). This is simply an Ash wrapper. 48 | 49 | ## License 50 | 51 | [MIT](LICENSE.md) 52 | -------------------------------------------------------------------------------- /ash_config.yaml: -------------------------------------------------------------------------------- 1 | name: yamlparse 2 | package: github.com/ash-shell/yaml-parse 3 | default_alias: yamlparse 4 | test_prefix: Yamlparse 5 | -------------------------------------------------------------------------------- /extra/example.yaml: -------------------------------------------------------------------------------- 1 | one: kittens 2 | # Comments are allowed, but must be on their own lines 3 | two: puppies 4 | trees: 5 | # Comments here are OK too! 6 | evergreen: so green 7 | maple: so red 8 | canadian: canada 9 | american: america 10 | # Wow 11 | three: rats 12 | # Empty lines are OK 13 | 14 | four: bats 15 | five: checkit # Comments after content is not supported yet, so DON'T do this 16 | -------------------------------------------------------------------------------- /lib/yaml_parse.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ################################################# 4 | # This function will Parse a simple YAML file 5 | # and will output bash variables 6 | # 7 | # Typical Usage: 8 | # eval $(YamlParse__parse sample.yml "PREFIX_") 9 | # 10 | # @param $1: The yaml file to parse 11 | # @param $2: The prefix to append to all of the 12 | # variables to be created 13 | ################################################# 14 | YamlParse__parse() { 15 | local prefix=$2 16 | local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034') 17 | sed -ne "s|^\($s\):|\1|" \ 18 | -e "s|^\($s\)\($w\)$s:$s[\"']\(.*\)[\"']$s\$|\1$fs\2$fs\3|p" \ 19 | -e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 | 20 | awk -F$fs '{ 21 | indent = length($1)/2; 22 | vname[indent] = $2; 23 | for (i in vname) {if (i > indent) {delete vname[i]}} 24 | if (length($3) > 0) { 25 | vn=""; for (i=0; i