fortify: move json indent call
Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
parent
df7f692e61
commit
4f4c690d38
7
main.go
7
main.go
@ -106,12 +106,7 @@ func main() {
|
|||||||
fmt.Println(license)
|
fmt.Println(license)
|
||||||
fmsg.Exit(0)
|
fmsg.Exit(0)
|
||||||
case "template": // print full template configuration
|
case "template": // print full template configuration
|
||||||
if s, err := json.MarshalIndent(fst.Template(), "", " "); err != nil {
|
printJSON(fst.Template())
|
||||||
fmsg.Fatalf("cannot generate template: %v", err)
|
|
||||||
panic("unreachable")
|
|
||||||
} else {
|
|
||||||
fmt.Println(string(s))
|
|
||||||
}
|
|
||||||
fmsg.Exit(0)
|
fmsg.Exit(0)
|
||||||
case "help": // print help message
|
case "help": // print help message
|
||||||
flag.CommandLine.Usage()
|
flag.CommandLine.Usage()
|
||||||
|
27
print.go
27
print.go
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
direct "os"
|
||||||
"strings"
|
"strings"
|
||||||
"text/tabwriter"
|
"text/tabwriter"
|
||||||
"time"
|
"time"
|
||||||
@ -15,25 +16,22 @@ import (
|
|||||||
|
|
||||||
func printShow(instance *state.State, config *fst.Config) {
|
func printShow(instance *state.State, config *fst.Config) {
|
||||||
if flagJSON {
|
if flagJSON {
|
||||||
v := interface{}(config)
|
v := any(config)
|
||||||
if instance != nil {
|
if instance != nil {
|
||||||
v = instance
|
v = instance
|
||||||
}
|
}
|
||||||
|
|
||||||
if s, err := json.MarshalIndent(v, "", " "); err != nil {
|
printJSON(v)
|
||||||
fmsg.Fatalf("cannot serialise as JSON: %v", err)
|
return
|
||||||
panic("unreachable")
|
|
||||||
} else {
|
|
||||||
fmt.Println(string(s))
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
buf := new(strings.Builder)
|
now := time.Now().UTC()
|
||||||
w := tabwriter.NewWriter(buf, 0, 1, 4, ' ', 0)
|
w := tabwriter.NewWriter(direct.Stdout, 0, 1, 4, ' ', 0)
|
||||||
|
|
||||||
if instance != nil {
|
if instance != nil {
|
||||||
fmt.Fprintf(w, "State\n")
|
fmt.Fprintf(w, "State\n")
|
||||||
fmt.Fprintf(w, " Instance:\t%s (%d)\n", instance.ID.String(), instance.PID)
|
fmt.Fprintf(w, " Instance:\t%s (%d)\n", instance.ID.String(), instance.PID)
|
||||||
fmt.Fprintf(w, " Uptime:\t%s\n", time.Now().Sub(instance.Time).Round(time.Second).String())
|
fmt.Fprintf(w, " Uptime:\t%s\n", now.Sub(instance.Time).Round(time.Second).String())
|
||||||
fmt.Fprintf(w, "\n")
|
fmt.Fprintf(w, "\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,6 +136,13 @@ func printShow(instance *state.State, config *fst.Config) {
|
|||||||
if err := w.Flush(); err != nil {
|
if err := w.Flush(); err != nil {
|
||||||
fmsg.Fatalf("cannot flush tabwriter: %v", err)
|
fmsg.Fatalf("cannot flush tabwriter: %v", err)
|
||||||
}
|
}
|
||||||
fmt.Print(buf.String())
|
}
|
||||||
|
|
||||||
|
func printJSON(v any) {
|
||||||
|
encoder := json.NewEncoder(direct.Stdout)
|
||||||
|
encoder.SetIndent("", " ")
|
||||||
|
if err := encoder.Encode(v); err != nil {
|
||||||
|
fmsg.Fatalf("cannot serialise as JSON: %v", err)
|
||||||
|
panic("unreachable")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user