|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +## --------------------------------------------------------------------- |
| 4 | +## |
| 5 | +## app_epath.sh: Parse Erlang app.config with POSIX tools for automation |
| 6 | +## |
| 7 | +## Copyright (c) 2013 Basho Technologies, Inc. All Rights Reserved. |
| 8 | +## |
| 9 | +## This file is provided to you under the Apache License, |
| 10 | +## Version 2.0 (the "License"); you may not use this file |
| 11 | +## except in compliance with the License. You may obtain |
| 12 | +## a copy of the License at |
| 13 | +## |
| 14 | +## http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | +## |
| 16 | +## Unless required by applicable law or agreed to in writing, |
| 17 | +## software distributed under the License is distributed on an |
| 18 | +## "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 19 | +## KIND, either express or implied. See the License for the |
| 20 | +## specific language governing permissions and limitations |
| 21 | +## under the License. |
| 22 | +## |
| 23 | +## --------------------------------------------------------------------- |
| 24 | + |
| 25 | +## Example usage: |
| 26 | +# |
| 27 | +# #!/bin/sh |
| 28 | +# |
| 29 | +# # Load the functions |
| 30 | +# . path/to/app_epath.sh |
| 31 | +# |
| 32 | +# # Build the path info |
| 33 | +# epaths=`make_app_epaths path/to/app.config` |
| 34 | +# |
| 35 | +# # View all of the settings. Quotes are important. |
| 36 | +# echo "$epaths" |
| 37 | +# |
| 38 | +# # Grep around in the paths for various items. |
| 39 | +# echo "$epaths" | grep 'riak_core ring_creation_size' |
| 40 | +# echo "$epaths" | grep "lager handlers lager_file_backend" | grep info |
| 41 | +# |
| 42 | +# # Use the epath function to get directly at settings |
| 43 | +# epath 'riak_core ring_creation_size' "$epaths" |
| 44 | +# epath 'riak_core platform_bin_dir' "$epaths" |
| 45 | +# epath 'riak_kv storage_backend' "$epaths" |
| 46 | +# |
| 47 | +# # Use epath to view all of the riak_core settings |
| 48 | +# epath riak_core |
| 49 | +# |
| 50 | +## End example |
| 51 | + |
| 52 | +## Here is a command you can put in your path for general cli use. |
| 53 | +# |
| 54 | +# #!/bin/sh |
| 55 | +# |
| 56 | +# # $1 quoted eterm path for search: 'riak_core ring_creation_size' |
| 57 | +# # $2 path/to/app.config |
| 58 | +# |
| 59 | +# . path/to/app_epath.sh |
| 60 | +# |
| 61 | +# epaths=`make_app_epaths "$2"` |
| 62 | +# epath "$1" "$epaths" |
| 63 | +# |
| 64 | +## End epath command |
| 65 | + |
| 66 | +# make_app_epaths takes a path to an app.config file as its argument and |
| 67 | +# and returns (prints) a flattened text structure of configuration settings. |
| 68 | + |
| 69 | +make_app_epaths () { |
| 70 | + # Remove all lines containing comments |
| 71 | + # Remove all blank lines |
| 72 | + # Remove the first [ |
| 73 | + # Remove the last ]. |
| 74 | + # Remove all blank lines again (just in case) |
| 75 | + appconfig=`cat $1 \ |
| 76 | + | sed '/^[ \t]*%/d' \ |
| 77 | + | sed '/^[ \t]*$/d' \ |
| 78 | + | sed -e '/\[/{s///;:a' -e '$!N;$!ba' -e '}' \ |
| 79 | + | sed '$ s/\]\.//g' \ |
| 80 | + | sed '/^[ \t]*$/d'` |
| 81 | + |
| 82 | + STACK= |
| 83 | + INQUOTE=0 |
| 84 | + # The awk puts each char, spaces included, on their own line for parsing. |
| 85 | + echo "$appconfig" | awk '{gsub(//,"\n");print}' | while read i; do |
| 86 | + case "x${i}x" in |
| 87 | + "x\"x") |
| 88 | + # Flip the INQUOTE state and echo the quote |
| 89 | + if [ 1 -eq $INQUOTE ]; then |
| 90 | + INQUOTE=0 |
| 91 | + else |
| 92 | + INQUOTE=1 |
| 93 | + fi |
| 94 | + STACK="${STACK}${i}" |
| 95 | + ;; |
| 96 | + "xx") |
| 97 | + if [ 1 -eq $INQUOTE ]; then |
| 98 | + # If in quotes, keep this space |
| 99 | + STACK="${STACK} " |
| 100 | + fi |
| 101 | + ;; |
| 102 | + "x,x") |
| 103 | + if [ 1 -eq $INQUOTE ]; then |
| 104 | + # If in quotes, keep this comma |
| 105 | + STACK="${STACK}," |
| 106 | + else |
| 107 | + # Commas outside quotes means a new item is next |
| 108 | + STACK="${STACK} " |
| 109 | + fi |
| 110 | + ;; |
| 111 | + "x{x") |
| 112 | + if [ 1 -eq $INQUOTE ]; then |
| 113 | + # If in quotes, keep this bracket |
| 114 | + STACK="${STACK}{" |
| 115 | + else |
| 116 | + # Add brace to the stack; will pop off from here on next } |
| 117 | + STACK="${STACK} __EBRACE__ " |
| 118 | + fi |
| 119 | + ;; |
| 120 | + "x}x") |
| 121 | + if [ 1 -eq $INQUOTE ]; then |
| 122 | + # If in quotes, keep this bracket |
| 123 | + STACK="${STACK}}" |
| 124 | + else |
| 125 | + # We're only interested in printing leaves, not |
| 126 | + # intermediates like 'riak_core http', which contain lists. |
| 127 | + # See if the current stack ends with ] (end of list). |
| 128 | + echo $STACK | grep -E "__EBRACKET__$" >/dev/null 2>&1 |
| 129 | + if [ 1 -eq $? ]; then |
| 130 | + # If not, print the stack without all of the mess. |
| 131 | + echo "$STACK" | \ |
| 132 | + sed 's/ *__EBRACE__//g;s/ *__EBRACKET__//g;s/^ *//' |
| 133 | + fi |
| 134 | + # Pop off everything from the last brace on. |
| 135 | + STACK=`echo "$STACK" | sed 's/\(.*\) __EBRACE__.*/\1/g'` |
| 136 | + fi |
| 137 | + ;; |
| 138 | + "x[x") |
| 139 | + if [ 1 -eq $INQUOTE ]; then |
| 140 | + # If in quotes, keep this bracket |
| 141 | + STACK="${STACK}[" |
| 142 | + else |
| 143 | + # Add a placeholder to aid in determining whether or not to |
| 144 | + # print. That is, we don't want to print 'riak_core http'. |
| 145 | + STACK="${STACK} __EBRACKET__ " |
| 146 | + fi |
| 147 | + ;; |
| 148 | + "x]x") |
| 149 | + if [ 1 -eq $INQUOTE ]; then |
| 150 | + # If in quotes, keep this bracket |
| 151 | + STACK="${STACK}]" |
| 152 | + fi |
| 153 | + # Don't actually do anything with ], as the starting brackets |
| 154 | + # are instead removed with }. |
| 155 | + ;; |
| 156 | + *) |
| 157 | + # Anything else is just pushed. |
| 158 | + STACK="${STACK}${i}" |
| 159 | + ;; |
| 160 | + esac |
| 161 | + done |
| 162 | +} |
| 163 | + |
| 164 | +epath () { |
| 165 | + # arg1 - a pattern to search for |
| 166 | + # arg2 - output of make_app_epaths, passed in quoted |
| 167 | + # output - search of arg2 for arg1, trimming arg1 from the beginning |
| 168 | + # Note: there may be multiple lines of output. |
| 169 | + pat=$1 |
| 170 | + shift |
| 171 | + echo "$*" | grep "$pat " | sed "s/^${pat} *//" |
| 172 | +} |
| 173 | + |
0 commit comments