fortify: move json indent call
All checks were successful
Tests / Go tests (push) Successful in 36s
Nix / NixOS tests (push) Successful in 2m59s

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
Ophestra 2024-12-21 18:51:59 +09:00
parent df7f692e61
commit 4f4c690d38
Signed by: cat
SSH Key Fingerprint: SHA256:gQ67O0enBZ7UdZypgtspB2FDM1g3GVw8nX0XSdcFw8Q
2 changed files with 118 additions and 118 deletions

View File

@ -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()

View File

@ -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")
}
}