mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 16:27:33 -10:00
~/.config/devvm/config.toml is the defaults you write by hand: backend,
network, ports, features, agents, [size] cpus/memory/disk and
[retention]'s six keys. devvm reads it and never writes it, strictly: an
unknown key (toml.MetaData.Undecoded, its line found by key) or a value
its check refuses (ParseCPUs, ParseGiB, ParsePorts, ask/on/off, a
backend's name, image.FeatureName, image.Agents, ParseDuration,
ParseSize, counts) fails naming the file and the line.
~/.local/share/devvm/settings.json holds what devvm setup, size -g,
ports -g, network -g and retention save (unknown fields ignored).
internal/settings owns both: Defaults (Value and List, each with its
Source: flag, config.toml, settings.json or built-in), Read, Save, which
refuses a key config.toml sets (set_in_config, with a doc link), and
the parsers and retention settings that moved from vm and state. A flag
wins over config.toml, config.toml over settings.json, settings.json
over the built-in default. Settings methods lose their files; setup
uses vm.NetworkChoice instead of its own copy; creation.plan reads one
snapshot (firstRun) after the first-run questions; the registry gets
the default backend through DefaultBackend; state's featureName copy
goes with base-features.
devvm size -g, ports -g, network -g and retention show each value's
source after a #, and in --json (source, sources); devvm doctor names
the file that sets its backend (doctor_backend_from) and adds
backend_source. devvm setup skips what config.toml answers
(setup_in_config) and refuses a flag that would change it.
ssh_config (and so the ~/.ssh/config Include line), known_hosts,
agent-keys.json and secrets/ move to the data folder. agent-keys.json is
{"keys": [{file, fingerprint, confirm, lifetime}]}, parsed once
(AddedKey); Keys formats KeyList.
Tests: settings' table tests (every key, unknown keys, bad values, an
empty file, precedence, Save's refusal and merge, settings.json's
fields), agent-keys.json, the registry's default backend, setup with
config.toml, SizeChoice with config.toml; CLI scripts for size -g,
ports -g, network -g, retention, setup and doctor's sources, each
refusal, a hand-edited config.toml with a typo, and ssh -G resolving
devvm-web through the Include line. Docs: settings.md's config.toml
section with a full example, linked from each command's page.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
104 lines
3.0 KiB
Go
104 lines
3.0 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/settings"
|
|
"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
|
|
}
|
|
|
|
// defaultOutput is a default's --json: its value by key, such as ports,
|
|
// and source, where it came from (a settings.Source).
|
|
type defaultOutput map[string]string
|
|
|
|
// printDefault writes a default in effect, v, as its value then a # and
|
|
// label, where it came from (settings.Source.Label), or with asJSON as a
|
|
// defaultOutput.
|
|
func printDefault(u *ui.UI, asJSON bool, key string, v settings.Value, label string) error {
|
|
if asJSON {
|
|
return printJSON(u, defaultOutput{key: v.Value, "source": string(v.Source)})
|
|
}
|
|
_, err := io.WriteString(u.Stdout(), v.Value+" # "+label+"\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
|
|
}
|