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)
|
||||
fmsg.Exit(0)
|
||||
case "template": // print full template configuration
|
||||
if s, err := json.MarshalIndent(fst.Template(), "", " "); err != nil {
|
||||
fmsg.Fatalf("cannot generate template: %v", err)
|
||||
panic("unreachable")
|
||||
} else {
|
||||
fmt.Println(string(s))
|
||||
}
|
||||
printJSON(fst.Template())
|
||||
fmsg.Exit(0)
|
||||
case "help": // print help message
|
||||
flag.CommandLine.Usage()
|
||||
|
27
print.go
27
print.go
@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
direct "os"
|
||||
"strings"
|
||||
"text/tabwriter"
|
||||
"time"
|
||||
@ -15,25 +16,22 @@ import (
|
||||
|
||||
func printShow(instance *state.State, config *fst.Config) {
|
||||
if flagJSON {
|
||||
v := interface{}(config)
|
||||
v := any(config)
|
||||
if instance != nil {
|
||||
v = instance
|
||||
}
|
||||
|
||||
if s, err := json.MarshalIndent(v, "", " "); err != nil {
|
||||
fmsg.Fatalf("cannot serialise as JSON: %v", err)
|
||||
panic("unreachable")
|
||||
} else {
|
||||
fmt.Println(string(s))
|
||||
printJSON(v)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
buf := new(strings.Builder)
|
||||
w := tabwriter.NewWriter(buf, 0, 1, 4, ' ', 0)
|
||||
|
||||
now := time.Now().UTC()
|
||||
w := tabwriter.NewWriter(direct.Stdout, 0, 1, 4, ' ', 0)
|
||||
|
||||
if instance != nil {
|
||||
fmt.Fprintf(w, "State\n")
|
||||
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")
|
||||
}
|
||||
|
||||
@ -138,6 +136,13 @@ func printShow(instance *state.State, config *fst.Config) {
|
||||
if err := w.Flush(); err != nil {
|
||||
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