mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-06-19 00:17:51 +02:00
add df, env, and free parsers
This commit is contained in:
73
README.md
73
README.md
@ -70,6 +70,79 @@ Options:
|
|||||||
- `-p` specifies whether to pretty format the JSON output
|
- `-p` specifies whether to pretty format the JSON output
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
### df
|
||||||
|
```
|
||||||
|
$ df | jc --df -p
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"Filesystem": "/dev/disk1s1",
|
||||||
|
"512-blocks": "976490576",
|
||||||
|
"Used": "268326664",
|
||||||
|
"Available": "702568152",
|
||||||
|
"Capacity": "28%",
|
||||||
|
"iused": "1395740",
|
||||||
|
"ifree": "9223372036853380067",
|
||||||
|
"%iused": "0%",
|
||||||
|
"Mounted": "/"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Filesystem": "devfs",
|
||||||
|
"512-blocks": "680",
|
||||||
|
"Used": "680",
|
||||||
|
"Available": "0",
|
||||||
|
"Capacity": "100%",
|
||||||
|
"iused": "1178",
|
||||||
|
"ifree": "0",
|
||||||
|
"%iused": "100%",
|
||||||
|
"Mounted": "/dev"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Filesystem": "map",
|
||||||
|
"512-blocks": "auto_home",
|
||||||
|
"Used": "0",
|
||||||
|
"Available": "0",
|
||||||
|
"Capacity": "0",
|
||||||
|
"iused": "100%",
|
||||||
|
"ifree": "0",
|
||||||
|
"%iused": "0",
|
||||||
|
"Mounted": "100%",
|
||||||
|
"on": "/home"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
```
|
||||||
|
### env
|
||||||
|
```
|
||||||
|
$ env | jc --env -p
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"TERM": "xterm-256color"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"SHELL": "/bin/bash"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"USER": "root"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"PATH": "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"PWD": "/bin"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"LANG": "en_US.UTF-8"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"HOME": "/root"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"_": "/usr/bin/env"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
```
|
||||||
|
### df
|
||||||
|
```
|
||||||
|
```
|
||||||
### ifconfig
|
### ifconfig
|
||||||
```
|
```
|
||||||
$ ifconfig | jc --ifconfig -p
|
$ ifconfig | jc --ifconfig -p
|
||||||
|
@ -1,5 +1,10 @@
|
|||||||
jc changelog
|
jc changelog
|
||||||
|
|
||||||
|
2019xxxx v0.x.x
|
||||||
|
- Add env parser
|
||||||
|
- Add df parser
|
||||||
|
- Add free parser
|
||||||
|
|
||||||
20191021 v0.6.4
|
20191021 v0.6.4
|
||||||
- Flatten netstat parser output
|
- Flatten netstat parser output
|
||||||
- Clean up argument parsing
|
- Clean up argument parsing
|
||||||
|
17
jc/jc.py
17
jc/jc.py
@ -6,6 +6,9 @@ Main input module
|
|||||||
|
|
||||||
import sys
|
import sys
|
||||||
import json
|
import json
|
||||||
|
import jc.parsers.df
|
||||||
|
import jc.parsers.env
|
||||||
|
import jc.parsers.free
|
||||||
import jc.parsers.ifconfig
|
import jc.parsers.ifconfig
|
||||||
import jc.parsers.ls
|
import jc.parsers.ls
|
||||||
import jc.parsers.netstat
|
import jc.parsers.netstat
|
||||||
@ -20,7 +23,16 @@ def main():
|
|||||||
if '-p' in sys.argv:
|
if '-p' in sys.argv:
|
||||||
pretty = True
|
pretty = True
|
||||||
|
|
||||||
if '--ifconfig' in sys.argv:
|
if '--df' in sys.argv:
|
||||||
|
result = jc.parsers.df.parse(data)
|
||||||
|
|
||||||
|
elif '--env' in sys.argv:
|
||||||
|
result = jc.parsers.env.parse(data)
|
||||||
|
|
||||||
|
elif '--free' in sys.argv:
|
||||||
|
result = jc.parsers.free.parse(data)
|
||||||
|
|
||||||
|
elif '--ifconfig' in sys.argv:
|
||||||
result = jc.parsers.ifconfig.parse(data)
|
result = jc.parsers.ifconfig.parse(data)
|
||||||
|
|
||||||
elif '--ls' in sys.argv:
|
elif '--ls' in sys.argv:
|
||||||
@ -39,6 +51,9 @@ def main():
|
|||||||
print('jc: missing arguments', file=sys.stderr)
|
print('jc: missing arguments', file=sys.stderr)
|
||||||
print('Usage: jc [parser] [options]\n', file=sys.stderr)
|
print('Usage: jc [parser] [options]\n', file=sys.stderr)
|
||||||
print('Parsers:', file=sys.stderr)
|
print('Parsers:', file=sys.stderr)
|
||||||
|
print(' --df df parser', file=sys.stderr)
|
||||||
|
print(' --env env parser', file=sys.stderr)
|
||||||
|
print(' --free free parser', file=sys.stderr)
|
||||||
print(' --ifconfig iconfig parser', file=sys.stderr)
|
print(' --ifconfig iconfig parser', file=sys.stderr)
|
||||||
print(' --ls ls parser', file=sys.stderr)
|
print(' --ls ls parser', file=sys.stderr)
|
||||||
print(' --netstat netstat parser', file=sys.stderr)
|
print(' --netstat netstat parser', file=sys.stderr)
|
||||||
|
56
jc/parsers/df.py
Normal file
56
jc/parsers/df.py
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
"""jc - JSON CLI output utility df Parser
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
specify --df as the first argument if the piped input is coming from df
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
$ df | jc --df -p
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"Filesystem": "/dev/disk1s1",
|
||||||
|
"512-blocks": "976490576",
|
||||||
|
"Used": "268326664",
|
||||||
|
"Available": "702568152",
|
||||||
|
"Capacity": "28%",
|
||||||
|
"iused": "1395740",
|
||||||
|
"ifree": "9223372036853380067",
|
||||||
|
"%iused": "0%",
|
||||||
|
"Mounted": "/"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Filesystem": "devfs",
|
||||||
|
"512-blocks": "680",
|
||||||
|
"Used": "680",
|
||||||
|
"Available": "0",
|
||||||
|
"Capacity": "100%",
|
||||||
|
"iused": "1178",
|
||||||
|
"ifree": "0",
|
||||||
|
"%iused": "100%",
|
||||||
|
"Mounted": "/dev"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Filesystem": "map",
|
||||||
|
"512-blocks": "auto_home",
|
||||||
|
"Used": "0",
|
||||||
|
"Available": "0",
|
||||||
|
"Capacity": "0",
|
||||||
|
"iused": "100%",
|
||||||
|
"ifree": "0",
|
||||||
|
"%iused": "0",
|
||||||
|
"Mounted": "100%",
|
||||||
|
"on": "/home"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def parse(data):
|
||||||
|
|
||||||
|
# code adapted from Conor Heine at:
|
||||||
|
# https://gist.github.com/cahna/43a1a3ff4d075bcd71f9d7120037a501
|
||||||
|
|
||||||
|
cleandata = data.splitlines()
|
||||||
|
headers = [h for h in ' '.join(cleandata[0].strip().split()).split() if h]
|
||||||
|
raw_data = map(lambda s: s.strip().split(None, len(headers) - 1), cleandata[1:])
|
||||||
|
return [dict(zip(headers, r)) for r in raw_data]
|
53
jc/parsers/env.py
Normal file
53
jc/parsers/env.py
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
"""jc - JSON CLI output utility env Parser
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
specify --env as the first argument if the piped input is coming from env
|
||||||
|
|
||||||
|
Example:
|
||||||
|
$ env | jc --env -p
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"TERM": "xterm-256color"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"SHELL": "/bin/bash"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"USER": "root"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"PATH": "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"PWD": "/bin"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"LANG": "en_US.UTF-8"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"HOME": "/root"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"_": "/usr/bin/env"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def parse(data):
|
||||||
|
output = []
|
||||||
|
|
||||||
|
linedata = data.splitlines()
|
||||||
|
|
||||||
|
# Clear any blank lines
|
||||||
|
cleandata = list(filter(None, linedata))
|
||||||
|
|
||||||
|
if cleandata:
|
||||||
|
|
||||||
|
for entry in cleandata:
|
||||||
|
output_line = {}
|
||||||
|
parsed_line = entry.split('=', maxsplit=1)
|
||||||
|
output_line[parsed_line[0]] = parsed_line[1]
|
||||||
|
output.append(output_line)
|
||||||
|
|
||||||
|
return output
|
23
jc/parsers/free.py
Normal file
23
jc/parsers/free.py
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
"""jc - JSON CLI output utility free Parser
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
specify --free as the first argument if the piped input is coming from free
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def parse(data):
|
||||||
|
|
||||||
|
# code adapted from Conor Heine at:
|
||||||
|
# https://gist.github.com/cahna/43a1a3ff4d075bcd71f9d7120037a501
|
||||||
|
|
||||||
|
cleandata = data.splitlines()
|
||||||
|
headers = [h for h in ' '.join(cleandata[0].strip().split()).split() if h]
|
||||||
|
|
||||||
|
headers.insert(0, "type")
|
||||||
|
|
||||||
|
raw_data = map(lambda s: s.strip().split(None, len(headers) - 1), cleandata[1:])
|
||||||
|
return [dict(zip(headers, r)) for r in raw_data]
|
Reference in New Issue
Block a user