Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hbarcelos committed Mar 17, 2022
1 parent c492560 commit 4ddd642
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.tar
*.tar.gz
*.asc
pkg/
53 changes: 53 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
NAME=ces-shell-utils
VERSION=0.1.0

DIRS=bin
INSTALL_DIRS=`find $(DIRS) -type d 2>/dev/null`
INSTALL_FILES=`find $(DIRS) -type f 2>/dev/null`
DOC_FILES=*.md *.txt

PKG_DIR=pkg
PKG_NAME=$(NAME)-$(VERSION)
PKG=$(PKG_DIR)/$(PKG_NAME).tar.gz
SIG=$(PKG_DIR)/$(PKG_NAME).asc

PREFIX?=$(HOME)/.local
DOC_DIR=$(PREFIX)/share/doc/$(PKG_NAME)

pkg:
mkdir -p $(PKG_DIR)

$(PKG): pkg
git archive --output=$(PKG) --prefix=$(PKG_NAME)/ HEAD

build: $(PKG)

$(SIG): $(PKG)
gpg --sign --detach-sign --armor $(PKG)

sign: $(SIG)

clean:
rm -f $(PKG) $(SIG)

all: $(PKG) $(SIG)

test:

tag:
git tag v$(VERSION)
git push --tags

release: $(PKG) $(SIG) tag

install:
for dir in $(INSTALL_DIRS); do mkdir -p $(PREFIX)/$$dir; done
for file in $(INSTALL_FILES); do cp $$file $(PREFIX)/$$file; done
mkdir -p $(DOC_DIR)
cp -r $(DOC_FILES) $(DOC_DIR)/

uninstall:
for file in $(INSTALL_FILES); do rm -rf $(PREFIX)/$$file; done
rm -rf $(DOC_DIR)

.PHONY: build sign clean test tag release install uninstall all
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# CES Shell Utils

Home for Collateral Engineering Services utility shell scripts.

## Installation

## Commands

### `json-to-env`

```
USAGE
json-to-env [-hx] [-f] <file>
json-to-env [-hx] [-f] -
json-to-env [-hx]
DESCRIPTION
Converts a JSON file into environment variables.
Each \`key\`: \`value\` pair will be converted to a \`key=\$value\` statement.
If <file> is not provided or is \`-\`, it will read from stdin.
OPTIONS
-h, --help
Displays this help text.
-f, --file
The path to the file to read from.
-x, --export
Prepend 'export' to the generated environment variables.
```

Examples:

```bash
# Regular usage
json-to-env /path/to/file.json
cat /path/to/file.json | json-to-env

# With `export`:
json-to-env -x /path/to/file.json

# -f param is optional:
json-to-env -f /path/to/file.json # is equivalent to
json-to-env /path/to/file.json

# Don't have a file? No problem:
json-to-env <<< "{"VAR":"VALUE"}" # outputs VAR=VALUE
```
88 changes: 88 additions & 0 deletions bin/json-to-env
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/usr/bin/env bash

usage() {
cat >&2 << EOF
USAGE
$0 [-x] [-f] <file>
$0 [-x] [-f] -
$0 [-x]
DESCRIPTION
Converts a JSON file into environment variables.
Each \`key\`: \`value\` pair will be converted to a \`key=\$value\` statement.
If <file> is not provided or is \`-\`, it will read from stdin.
OPTIONS
-h, --help
Displays this help text.
-f, --file
The path to the file to read from.
-x, --export
Prepend 'export' to the generated environment variables.
EOF
}

json-to-env() {
local options
local file
local exports=false

options=$(getopt -o hxf: -l help,export,file: --name "$0" -- "$@")
eval set -- "$options"

while true; do
case "$1" in
-h|--help)
usage
exit 0
;;
-x|--export)
exports=true
;;
-f|--file)
shift
file="$1"
;;
--)
shift
break
;;
esac
shift
done

# If -f/--file is not used, <file> is the first positional argument
# If it's not given or empty, then the script should read from stdin
file="${file:-${1:-/dev/stdin}}"
local output=$(
jq -r 'to_entries | map(.key + "|" + (.value | tostring)) | .[]' "$file" |
while IFS='|' read -r key value; do
# If it's an address, make sure it's checksummed
if [[ "$value" =~ '^0x[0-9a-fA-F]{40}$' ]]; then
PAIR="${key}=$(seth --to-checksum-address "${value}")"
else
PAIR="${key}=${value}"
fi
echo "${PAIR}"
done
)

for pair in $output; do
if [ "$exports" == 'true' ]; then
echo "export ${pair}"
else
echo "${pair}"
fi
done
}

# Executes the function if it's been called as a script.
# This will evaluate to false if this script is sourced by other script.
if [ "$0" = "$BASH_SOURCE" ]; then
set -eo pipefail
json-to-env $@
fi

0 comments on commit 4ddd642

Please sign in to comment.