Skip to content

Commit

Permalink
Merge pull request #44 from whiteinge/msysgit-empty-env
Browse files Browse the repository at this point in the history
Fixes to _format_json for msysgit
  • Loading branch information
whiteinge authored Jun 22, 2017
2 parents 18a6f37 + 41ae970 commit 2879185
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 10 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ BSD licensed.

## Setup

Authentication credentials are read from a `~/.netrc` file.
Authentication credentials are read from a `$HOME/.netrc` file on UNIX
machines or a `_netrc` file in `%HOME%` for UNIX environments under Windows.
[Generate the token on GitHub](https://github.com/settings/tokens) under
"Account Settings -> Applications".
Restrict permissions on that file with `chmod 600 ~/.netrc`!
Expand Down Expand Up @@ -83,6 +84,7 @@ Flags _must_ be the first argument to `ok.sh`, before `command`.
* [_all_funcs](#_all_funcs)
* [_log](#_log)
* [_helptext](#_helptext)
* [_awk_map](#_awk_map)
* [_format_json](#_format_json)
* [_format_urlencode](#_format_urlencode)
* [_filter_json](#_filter_json)
Expand Down Expand Up @@ -177,6 +179,16 @@ Input
* (stdin)
The text of a function body to parse.

### _awk_map

Invoke awk with a function that will empty the ENVIRON map

Positional arguments

* prg : `$1`

The body of an awk program to run

### _format_json

Create formatted JSON from name=value pairs
Expand Down
49 changes: 40 additions & 9 deletions ok.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
#
# ## Setup
#
# Authentication credentials are read from a `~/.netrc` file.
# Authentication credentials are read from a `$HOME/.netrc` file on UNIX
# machines or a `_netrc` file in `%HOME%` for UNIX environments under Windows.
# [Generate the token on GitHub](https://github.com/settings/tokens) under
# "Account Settings -> Applications".
# Restrict permissions on that file with `chmod 600 ~/.netrc`!
Expand Down Expand Up @@ -327,6 +328,35 @@ _helptext() {
# ### Request-response
# Functions for making HTTP requests and processing HTTP responses.

_awk_map() {
# Invoke awk with a function that will empty the ENVIRON map
#
# Positional arguments
#
local prg="${1:?awk program string required}"
# The body of an awk program to run

shift 1

local env_bin=$(command -v env)
local env_blacklist=$(env -i "$env_bin" | while read -r env_var; do
printf '%s\n' "${env_var%=*}"
done)
env -i "$@" "$awk_bin" \
-v env_blacklist="${env_blacklist}" \
'
function clear_envrion() {
for (name in ENVIRON) {
if (substr(name, 0, 3) == "AWK") delete ENVIRON[name]
}
split(env_blacklist, bl, "\n")
for (name in bl) { delete ENVIRON[bl[name]] }
}
'"$prg"
}
_format_json() {
# Create formatted JSON from name=value pairs
#
Expand All @@ -352,12 +382,13 @@ _format_json() {
_log debug "Formatting ${#} parameters as JSON."
env -i "$@" "$awk_bin" '
_awk_map '
function isnum(x){ return (x == x + 0) }
function isbool(x){ if (x == "true" || x == "false") return 1 }
BEGIN {
delete ENVIRON["AWKPATH"] # GNU addition.
delete ENVIRON["AWKLIBPATH"]
clear_envrion()
printf("{")
for (name in ENVIRON) {
Expand All @@ -376,7 +407,7 @@ _format_json() {
printf("}\n")
}
' | _filter_json
' "$@" | _filter_json
}
_format_urlencode() {
Expand All @@ -396,7 +427,7 @@ _format_urlencode() {
_log debug "Formatting ${#} parameters as urlencoded"
env -i "$@" "$awk_bin" '
_awk_map '
function escape(str, c, len, res) {
len = length(str)
res = ""
Expand All @@ -411,10 +442,10 @@ _format_urlencode() {
}
BEGIN {
clear_envrion()
for (i = 0; i <= 255; i += 1) ord[sprintf("%c", i)] = i;
delete ENVIRON["AWKPATH"] # GNU addition.
delete ENVIRON["AWKLIBPATH"]
for (name in ENVIRON) {
if (substr(name, 1, 1) == "_") continue
val = ENVIRON[name]
Expand All @@ -423,7 +454,7 @@ _format_urlencode() {
sep = "&"
}
}
'
' "$@"
}
_filter_json() {
Expand Down

0 comments on commit 2879185

Please sign in to comment.