Files
w3cjandClaude Opus 5.5 02ba74e614 build(lint): stricter linters, and the fixes they need
golangci-lint now also runs govet's nilness, unusedwrite, hostport and
waitgroup analyzers, nilnesserr, durationcheck, canonicalheader,
errchkjson, forcetypeassert, contextcheck and gochecksumtype.

- errchkjson checks each JSON encoding's error where the value could
  fail and drops it where errchkjson proves it can't, and reports
  structs without exported fields; tests may drop theirs.
- forcetypeassert holds the code; tests are exempt.
- gochecksumtype: agentlogin's format is a sum type (//sumtype:decl),
  and a switch on one names every case, default or not.
- canonicalheader: WWW-Authenticate, X-Request-ID, X-Api-Key.
- contextcheck: ctx threaded through carry, peekClientHello and sniOf,
  and through inject and value, whose cached lookup takes
  WithTimeout(WithoutCancel(ctx)), so two forbidigo nolints go;
  openTransparent takes its dialer's ctx. nolints stay where
  contextcheck can't follow: ReverseProxy's Rewrite, WithoutCancel
  alone, cobra's cmd.Context, e2e's runner window.
- No-behaviour fixes: scrubList for redact's list, the loopback
  forward's ports kept with its listeners, upgradetest's Key keeps its
  public key, a CA's leaves read through leafOf.

Contributing's Go lint rules lists each one and the sum-type convention.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-15 00:17:50 -07:00

49 lines
1.3 KiB
Go

package settings
import (
"encoding/json"
"errors"
"io/fs"
"os"
"slices"
"github.com/devvmhq/devvm/internal/msg"
"github.com/devvmhq/devvm/internal/state"
)
// readSaved reads settings.json at path. A missing file sets nothing; a
// field devvm doesn't know is ignored, and one devvm can't use is a
// state.CorruptError naming the file and the field.
func readSaved(path string) (Values, error) {
data, err := os.ReadFile(path) //nolint:gosec // paths come from paths.Dirs
switch {
case errors.Is(err, fs.ErrNotExist):
return Values{}, nil
case err != nil:
return Values{}, err
}
var v Values
if err := json.Unmarshal(data, &v); err != nil {
return Values{}, &state.CorruptError{Path: path, Reason: err.Error()}
}
for k := range v.Retention {
if !slices.Contains(RetentionKeys(), k) {
delete(v.Retention, k)
}
}
if key, err := v.check(false); err != nil {
c := &state.CorruptError{Path: path, Reason: key + ": " + err.Error()}
if m, ok := errors.AsType[msg.M](err); ok {
c.Reason, c.Msg = key+": ", m
}
return Values{}, c
}
return v, nil
}
// writeSaved replaces settings.json at path with v, indented.
func writeSaved(path string, v Values) error {
data, _ := json.MarshalIndent(v, "", " ")
return state.WriteFile(path, append(data, '\n'))
}