mirror of
https://github.com/aljazceru/nigiri.git
synced 2026-02-23 15:24:18 +01:00
colorize json output (#169)
This commit is contained in:
@@ -1,11 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"github.com/logrusorgru/aurora"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
@@ -34,7 +39,6 @@ var rpc = cli.Command{
|
||||
}
|
||||
|
||||
func rpcAction(ctx *cli.Context) error {
|
||||
|
||||
if isRunning, _ := nigiriState.GetBool("running"); !isRunning {
|
||||
return errors.New("nigiri is not running")
|
||||
}
|
||||
@@ -56,12 +60,77 @@ func rpcAction(ctx *cli.Context) error {
|
||||
}
|
||||
cmdArgs := append(rpcArgs, ctx.Args().Slice()...)
|
||||
bashCmd := exec.Command("docker", cmdArgs...)
|
||||
bashCmd.Stdout = os.Stdout
|
||||
// Create a pipe for the output of the "docker exec" command
|
||||
r, w := io.Pipe()
|
||||
bashCmd.Stdout = w
|
||||
bashCmd.Stderr = os.Stderr
|
||||
|
||||
if err := bashCmd.Run(); err != nil {
|
||||
return err
|
||||
}
|
||||
// Start a goroutine to run the "docker exec" command
|
||||
go func() {
|
||||
if err := bashCmd.Run(); err != nil {
|
||||
w.CloseWithError(err)
|
||||
} else {
|
||||
w.Close()
|
||||
}
|
||||
}()
|
||||
|
||||
// Read the output of the "docker exec" command from the pipe
|
||||
buf := new(bytes.Buffer)
|
||||
buf.ReadFrom(r)
|
||||
output := buf.Bytes()
|
||||
|
||||
// Use the json.Unmarshal function to parse the output of the
|
||||
// "docker exec" command and check if it is a valid JSON object
|
||||
var v interface{}
|
||||
if err := json.Unmarshal(output, &v); err == nil {
|
||||
// Use the json.Marshal function to convert the parsed JSON object
|
||||
// to a byte slice
|
||||
jsonBytes, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Use the bytes.Buffer type to create a buffer that we can use
|
||||
// to write the indented JSON string to
|
||||
var buf bytes.Buffer
|
||||
|
||||
// Use the json.Indent function to add indentation to the JSON byte slice
|
||||
// in the same way as if you were using the "jq" command
|
||||
if err := json.Indent(&buf, jsonBytes, "", " "); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Split the indented JSON string into individual lines
|
||||
lines := strings.Split(buf.String(), "\n")
|
||||
|
||||
// Loop through each line in the indented JSON string
|
||||
for _, line := range lines {
|
||||
// Check if the line starts with a "{" or a "["
|
||||
if strings.HasPrefix(line, "{") || strings.HasPrefix(line, "[") {
|
||||
// If the line starts with a "{" or a "[", it is the start of a JSON object
|
||||
// or array, so print it without any color
|
||||
fmt.Println(line)
|
||||
} else if strings.Contains(line, ":") {
|
||||
// If the line contains a ":", it is a key-value pair, so split the line
|
||||
// into the key and value parts and add the desired colors to each part
|
||||
parts := strings.SplitN(line, ":", 2)
|
||||
key := parts[0]
|
||||
value := parts[1]
|
||||
// Use the AnsiColorCode function from the "github.com/logrusorgru/aurora"
|
||||
// package to create ANSI escape codes for the "key" and "value" colors
|
||||
keyColor := aurora.BrightBlue(key)
|
||||
valueColor := aurora.BrightCyan(value)
|
||||
|
||||
fmt.Printf("%s: %s\n", keyColor.String(), valueColor.String())
|
||||
} else {
|
||||
// If the line does not start with a "{" or a "[" and does not contain a ":",
|
||||
// it is not a JSON object or array and does not contain a key-value pair, so
|
||||
// print it without any color
|
||||
fmt.Println(line)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
fmt.Println(string(output))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
1
go.mod
1
go.mod
@@ -6,6 +6,7 @@ require (
|
||||
github.com/btcsuite/btcd v0.21.0-beta // indirect
|
||||
github.com/btcsuite/btcutil v1.0.2
|
||||
github.com/compose-spec/compose-go v0.0.0-20210729195839-de56f4f0cb3c
|
||||
github.com/logrusorgru/aurora v2.0.3+incompatible // indirect
|
||||
github.com/urfave/cli/v2 v2.3.0
|
||||
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect
|
||||
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
|
||||
|
||||
2
go.sum
2
go.sum
@@ -90,6 +90,8 @@ github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFB
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8=
|
||||
github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
|
||||
github.com/marstr/guid v1.1.0/go.mod h1:74gB1z2wpxxInTG6yaqA7KrtM0NZ+RbrcqDvYHefzho=
|
||||
github.com/mattn/go-shellwords v1.0.12 h1:M2zGm7EW6UQJvDeQxo4T51eKPurbeFbe8WtebGE2xrk=
|
||||
github.com/mattn/go-shellwords v1.0.12/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y=
|
||||
|
||||
Reference in New Issue
Block a user