2019-10-22 11:10:11 -07:00
|
|
|
"""jc - JSON CLI output utility env Parser
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
specify --env as the first argument if the piped input is coming from env
|
|
|
|
|
2019-11-04 15:03:16 -08:00
|
|
|
Examples:
|
2019-10-24 17:11:17 -07:00
|
|
|
|
2019-10-22 11:10:11 -07:00
|
|
|
$ env | jc --env -p
|
2019-11-04 15:03:16 -08:00
|
|
|
[
|
|
|
|
{
|
|
|
|
"name": "XDG_SESSION_ID",
|
|
|
|
"value": "1"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "HOSTNAME",
|
|
|
|
"value": "localhost.localdomain"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "TERM",
|
|
|
|
"value": "vt220"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "SHELL",
|
|
|
|
"value": "/bin/bash"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "HISTSIZE",
|
|
|
|
"value": "1000"
|
|
|
|
},
|
|
|
|
...
|
|
|
|
]
|
|
|
|
|
|
|
|
$ env | jc --env -p -r
|
2019-10-24 15:54:31 -07:00
|
|
|
{
|
|
|
|
"TERM": "xterm-256color",
|
|
|
|
"SHELL": "/bin/bash",
|
|
|
|
"USER": "root",
|
|
|
|
"PATH": "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin",
|
|
|
|
"PWD": "/root",
|
|
|
|
"LANG": "en_US.UTF-8",
|
|
|
|
"HOME": "/root",
|
|
|
|
"LOGNAME": "root",
|
|
|
|
"_": "/usr/bin/env"
|
|
|
|
}
|
2019-10-22 11:10:11 -07:00
|
|
|
"""
|
2019-11-05 22:42:48 -06:00
|
|
|
import jc
|
2019-10-22 11:10:11 -07:00
|
|
|
|
|
|
|
|
2019-11-04 15:03:16 -08:00
|
|
|
def process(proc_data):
|
|
|
|
'''schema:
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"name": string,
|
|
|
|
"value": string
|
|
|
|
}
|
|
|
|
]
|
|
|
|
'''
|
|
|
|
|
|
|
|
# rebuild output for added semantic information
|
|
|
|
processed = []
|
|
|
|
for k, v in proc_data.items():
|
|
|
|
proc_line = {}
|
|
|
|
proc_line['name'] = k
|
|
|
|
proc_line['value'] = v
|
|
|
|
processed.append(proc_line)
|
|
|
|
|
|
|
|
return processed
|
|
|
|
|
|
|
|
|
|
|
|
def parse(data, raw=False):
|
2019-11-05 22:42:48 -06:00
|
|
|
# compatible options: linux, darwin, cygwin, win32, aix, freebsd
|
|
|
|
jc.jc.compatibility(__name__,
|
|
|
|
['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd'])
|
|
|
|
|
2019-11-04 15:03:16 -08:00
|
|
|
raw_output = {}
|
2019-10-22 11:10:11 -07:00
|
|
|
|
|
|
|
linedata = data.splitlines()
|
|
|
|
|
|
|
|
# Clear any blank lines
|
|
|
|
cleandata = list(filter(None, linedata))
|
|
|
|
|
|
|
|
if cleandata:
|
|
|
|
|
|
|
|
for entry in cleandata:
|
|
|
|
parsed_line = entry.split('=', maxsplit=1)
|
2019-11-04 15:03:16 -08:00
|
|
|
raw_output[parsed_line[0]] = parsed_line[1]
|
2019-10-22 11:10:11 -07:00
|
|
|
|
2019-11-04 15:03:16 -08:00
|
|
|
if raw:
|
|
|
|
return raw_output
|
|
|
|
else:
|
|
|
|
return process(raw_output)
|