1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-17 00:07:37 +02:00
Files
jc/jc/jc.py

50 lines
1.1 KiB
Python
Raw Normal View History

2019-10-15 15:06:09 -07:00
#!/usr/bin/env python3
"""jc - JSON CLI output utility
Main input module
"""
import sys
import json
2019-10-16 17:23:18 -07:00
import jc.parsers.ifconfig
import jc.parsers.ls
import jc.parsers.netstat
2019-10-18 13:19:39 -07:00
import jc.parsers.ps
2019-10-15 15:06:09 -07:00
2019-10-18 09:57:22 -07:00
2019-10-16 15:03:34 -07:00
def main():
pretty = False
data = sys.stdin.read()
if len(sys.argv) < 2:
2019-10-18 09:57:22 -07:00
print('Error: jc')
print(' Must specify parser. (e.g. --ls, --netstat, --ifconfig, etc.)')
2019-10-16 15:03:34 -07:00
print(' Use -p to pretty print')
2019-10-18 09:57:22 -07:00
print('Example: ls -al | jc --ls -p\n')
2019-10-16 15:03:34 -07:00
exit()
arg = sys.argv[1]
if len(sys.argv) > 2:
if sys.argv[2] == '-p':
pretty = True
if arg == '--ifconfig':
2019-10-16 17:23:18 -07:00
result = jc.parsers.ifconfig.parse(data)
2019-10-16 15:03:34 -07:00
elif arg == '--ls':
2019-10-16 17:23:18 -07:00
result = jc.parsers.ls.parse(data)
2019-10-16 15:03:34 -07:00
elif arg == '--netstat':
2019-10-16 17:23:18 -07:00
result = jc.parsers.netstat.parse(data)
2019-10-18 13:19:39 -07:00
elif arg == '--ps':
result = jc.parsers.ps.parse(data)
2019-10-16 15:03:34 -07:00
# output resulting dictionary as json
if pretty:
print(json.dumps(result, indent=2))
else:
print(json.dumps(result))
2019-10-18 09:57:22 -07:00
2019-10-16 15:03:34 -07:00
if __name__ == '__main__':
main()