Note
Completely written by Ampcode: https://ampcode.com/threads/T-f7489d5a-7e5a-4b0c-a593-27fd687a3089
A Go library that parses command line output from common Linux utilities into JSON format, inspired by the jc tool.
- 25+ parsers for common Linux utilities
- Clean, extensible parser interface
- Comprehensive test coverage
- Support for most common command output formats
System Information:
uname- System informationuptime- System uptime and loadwho- Logged in usersw- User activityid- User and group IDsenv- Environment variables
Process & System Monitoring:
ps- Process listingfree- Memory usagevmstat- Virtual memory statistics
Network:
ping- Network connectivity testnetstat- Network connectionsarp- ARP tabledig- DNS lookups
Filesystem:
ls- File listingsdf- Disk usagedu- Directory usagemount- Mounted filesystemslsblk- Block devicesfind- File search resultsstat- File statistics
System Services:
systemctl- Systemd service status
Utilities:
date- Date/time informationwc- Word, line, character counts
Configuration Files:
hosts- /etc/hosts entriespasswd- /etc/passwd entries
go get github.com/yourusername/term-to-jsonpackage main
import (
"fmt"
"log"
"term-to-json/parsers"
)
func main() {
lsOutput := `total 48
drwxr-xr-x 3 user group 4096 Jan 15 10:30 docs
-rw-r--r-- 1 user group 1234 Jan 14 09:15 file.txt
lrwxrwxrwx 1 user group 8 Jan 13 14:20 link -> file.txt`
result, err := parsers.Parse("ls", lsOutput)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", result)
}# Build the tool
go build -o term-to-json
# Parse ls output
ls -l | ./term-to-json ls
# Parse ps output
ps aux | ./term-to-json ps
# Parse df output
df -h | ./term-to-json df
# Parse system info
uname -a | ./term-to-json uname
# Parse network connectivity
ping -c 3 google.com | ./term-to-json ping
# Parse memory usage
free -h | ./term-to-json freeInput:
drwxr-xr-x 3 user group 4096 Jan 15 10:30 docs
-rw-r--r-- 1 user group 1234 Jan 14 09:15 file.txt
lrwxrwxrwx 1 user group 8 Jan 13 14:20 link -> file.txt
Output:
[
{
"permissions": "drwxr-xr-x",
"links": 3,
"owner": "user",
"group": "group",
"size": 4096,
"modified": "2024-01-15T10:30:00Z",
"name": "docs",
"is_directory": true,
"is_symlink": false
},
{
"permissions": "-rw-r--r--",
"links": 1,
"owner": "user",
"group": "group",
"size": 1234,
"modified": "2024-01-14T09:15:00Z",
"name": "file.txt",
"is_directory": false,
"is_symlink": false
},
{
"permissions": "lrwxrwxrwx",
"links": 1,
"owner": "user",
"group": "group",
"size": 8,
"modified": "2024-01-13T14:20:00Z",
"name": "link",
"is_directory": false,
"is_symlink": true,
"link_target": "file.txt"
}
]Input:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 225316 9876 ? Ss Jan01 0:01 /sbin/init
user 1234 2.5 1.2 123456 12345 pts/0 R+ 10:30 0:05 python script.py
Output:
[
{
"pid": 1,
"user": "root",
"cpu_percent": 0.0,
"memory_percent": 0.1,
"vsz": 225316,
"rss": 9876,
"tty": "?",
"stat": "Ss",
"start": "Jan01",
"time": "0:01",
"command": "/sbin/init"
},
{
"pid": 1234,
"user": "user",
"cpu_percent": 2.5,
"memory_percent": 1.2,
"vsz": 123456,
"rss": 12345,
"tty": "pts/0",
"stat": "R+",
"start": "10:30",
"time": "0:05",
"command": "python script.py"
}
]Input:
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 20511312 123456 19365472 1% /
tmpfs 4096000 0 4096000 0% /tmp
Output:
[
{
"filesystem": "/dev/sda1",
"size": 20511312,
"used": 123456,
"available": 19365472,
"use_percent": 1,
"mount_point": "/",
"used_bytes": 126419456,
"avail_bytes": 19838439424,
"size_bytes": 20995583488
},
{
"filesystem": "tmpfs",
"size": 4096000,
"used": 0,
"available": 4096000,
"use_percent": 0,
"mount_point": "/tmp",
"used_bytes": 0,
"avail_bytes": 4194304000,
"size_bytes": 4194304000
}
]To add a new parser:
- Create a new file in the
parsers/directory (e.g.,parsers/mycommand.go) - Implement the
Parserinterface:
type MyCommandParser struct{}
func (p *MyCommandParser) Name() string {
return "mycommand"
}
func (p *MyCommandParser) Parse(input string) (interface{}, error) {
// Parse logic here
return result, nil
}- Add the parser to the switch statement in
parsers/parser.go - Add comprehensive tests in
parsers/mycommand_test.go
go test ./parsersMIT License