Skip to content

Instantly share code, notes, and snippets.

@benley
Created March 29, 2019 22:01
Show Gist options
  • Save benley/19785298251dee97b8fd47eb1d379093 to your computer and use it in GitHub Desktop.
Save benley/19785298251dee97b8fd47eb1d379093 to your computer and use it in GitHub Desktop.
local string = import "string.libsonnet";
local
parseKey(x) = string.strip(
local x_ = std.splitLimit(x, " ", 1);
if std.length(x_) == 1 then x
else x_[std.length(x_) - 1]
),
parseValue(x) = (
if std.startsWith(x, '"') then std.parseJson(x)
else if std.startsWith(x, "'") then error "LOL single quoted strings FIXME"
else x // hopefully this is an unquoted string and not something fancy
),
parseLine(x) = (
local tokens = std.splitLimit(x, "=", 1);
if std.startsWith("#", x) then {}
else if std.length(tokens) < 2 then {}
else { [parseKey(tokens[0])]: parseValue(tokens[1]) }
),
terrible_shell_vars_parser(data) = (
std.foldl(
function(x, y) (x + y),
[parseLine(x) for x in std.split(data, "\n")],
{}
)
);
local testdata = |||
#!/bin/sh
DB_USER=unquoted blarg this wouldn't work in an actual shell
export DB_PASSWORD=hurrrr
# A COMMENT
local ZENDESK_HOST="blahblah.zendesk.com"
export ZENDESK_HOST_GROMMITFARBLE="example-fleet-yeet.zendesk.com"
export ZENDESK_USER="mr_cringe@example.com/token"
readonly ZENDESK_TOKEN="yeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeet"
export PM_INTERNAL_API_TOKEN="yazzzzzzzzz"
export RAVEN_DSN="https://harfgarblarg:beepbeepbeep7asd7fase78@app.getsentry.com/12345"
export BAZAAR_INTERNAL_API_TOKEN="this is definitely a real password"
export AWS_DEFAULT_REGION="us-west-1"
export CS_MONITORING_S3_BUCKET="hurhurhurhurhurhurhurhurhurhuhuhuhuhu"
export TRITON_CONFIG_PATH="/example/etc/triton.yaml"
export PEGASUS_HOST="pegasus-api-stage.us-west-1.example.com"
|||;
terrible_shell_vars_parser(testdata)
@benley
Copy link
Author

benley commented Mar 29, 2019

I hope nobody needs to actually use this, but here it is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment