mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-05 20:09:32 -10:00
ls, images, tools, size, ports, network (and -g), network log (and --blocked), ssh-agent, git-protect, secret, history, space, retention and doctor take --json: one JSON object on stdout with "version": 1 first, messages on stderr (ui.DataOnly), lists [] when empty. Settings commands only show with --json and refuse a value (json_shows_only). doctor --json replaces --fixes: problems with key, fix (null without one) and message, warnings, docker and every backend's features; it prints nothing else and succeeds whatever is missing, and install.sh reads its problems with awk. TestJSONShapes holds each command's field names and types. Each shape is documented on its command's page, with the contract under Everyday commands' Output for scripts. Co-Authored-By: Claude Opus 5.5 <[email protected]>
88 lines
2.4 KiB
Go
88 lines
2.4 KiB
Go
package cli
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"strconv"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/w3cj/devvm/internal/msg"
|
|
"github.com/w3cj/devvm/internal/ui"
|
|
)
|
|
|
|
// jsonVersion is the version of every --json shape. A shape only gains
|
|
// fields; any other change to one is a new version.
|
|
const jsonVersion = 1
|
|
|
|
// jsonFlag adds --json to a command that shows or lists something.
|
|
func jsonFlag(c *cobra.Command, on *bool) {
|
|
c.Flags().BoolVar(on, "json", false, "print it as one JSON object, for scripts")
|
|
}
|
|
|
|
// wantsJSON reports whether the command was given --json.
|
|
func wantsJSON(cmd *cobra.Command) bool {
|
|
f := cmd.Flags().Lookup("json")
|
|
return f != nil && f.Changed && f.Value.String() == "true"
|
|
}
|
|
|
|
// showsOnly checks a command that shows a setting bare and changes it
|
|
// with a value: with --json it only shows, so a value (an argument, or
|
|
// one of the flags in sets) is refused before next checks the arguments.
|
|
func showsOnly(next cobra.PositionalArgs, sets ...string) cobra.PositionalArgs {
|
|
return func(cmd *cobra.Command, args []string) error {
|
|
if wantsJSON(cmd) {
|
|
given := len(args) > 0
|
|
for _, f := range sets {
|
|
given = given || cmd.Flags().Changed(f)
|
|
}
|
|
if given {
|
|
return msg.JSONShowsOnly(cmd.CommandPath())
|
|
}
|
|
}
|
|
if next == nil {
|
|
return nil
|
|
}
|
|
return next(cmd, args)
|
|
}
|
|
}
|
|
|
|
// printValue writes a setting's value on a line of its own, or with
|
|
// asJSON as an object with its key.
|
|
func printValue(u *ui.UI, asJSON bool, key, value string) error {
|
|
if asJSON {
|
|
return printJSON(u, map[string]string{key: value})
|
|
}
|
|
_, err := io.WriteString(u.Stdout(), value+"\n")
|
|
return err
|
|
}
|
|
|
|
// printJSON writes v, a struct, to stdout as one indented JSON object
|
|
// with "version" first. HTML characters are written as they are.
|
|
func printJSON(u *ui.UI, v any) error {
|
|
var buf bytes.Buffer
|
|
enc := json.NewEncoder(&buf)
|
|
enc.SetEscapeHTML(false)
|
|
if err := enc.Encode(v); err != nil {
|
|
return err
|
|
}
|
|
body := bytes.TrimSpace(buf.Bytes())
|
|
if len(body) < 2 || body[0] != '{' {
|
|
return errors.New("--json output isn't an object")
|
|
}
|
|
obj := []byte(`{"version":` + strconv.Itoa(jsonVersion))
|
|
if rest := bytes.TrimSpace(body[1:]); string(rest) != "}" {
|
|
obj = append(obj, ',')
|
|
}
|
|
obj = append(obj, body[1:]...)
|
|
var out bytes.Buffer
|
|
if err := json.Indent(&out, obj, "", " "); err != nil {
|
|
return err
|
|
}
|
|
out.WriteByte('\n')
|
|
_, err := u.Stdout().Write(out.Bytes())
|
|
return err
|
|
}
|