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

View File

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