Files
agent-lib/internal/cli/query.go
T
m3ta-chironandClaude Opus 5.5 ae316860e5 fix(client): harden label pinning and close review findings
- Resolve the Windows admin config via the ProgramData known folder
  instead of the user-controlled ProgramData variable, so a user cannot
  redirect the admin lookup and drop pinned labels
- vendor rename rewrites matching agent-lib.labels.json entries, so a
  renamed item keeps its labels instead of going to every client
- validate checks the labels file in repositories without a lockfile
- A kept modified copy of a deselected item is no longer drift in status,
  and the toast explains it as "no longer assigned to you"
- Labels parser rejects trailing data such as an extra closing brace
- Tests: admin pin wiring (config chain, --repo, admin as chosen config),
  reselect redeploy with byte-level no-op, status labels JSON, validate
  without lockfile, rename keeps labels
- README: document unreadable admin config, drift semantics, rename

Refs: agent_lib-fh4

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 11:00:41 +01:00

174 lines
4.6 KiB
Go

package cli
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"github.com/spf13/cobra"
"github.com/m3tam3re/agent-lib/internal/labels"
"github.com/m3tam3re/agent-lib/internal/lockfile"
"github.com/m3tam3re/agent-lib/internal/vendor"
)
func loadLockfileOrFail(cmd *cobra.Command) *lockfile.Lockfile {
lf, err := lockfile.Load(filepath.Join(".", lockfile.FileName))
if err != nil {
if err == lockfile.ErrNotExist {
fmt.Fprintln(os.Stderr, "error: no lockfile found — run `agent-lib vendor add` first")
} else {
fmt.Fprintln(os.Stderr, "error:", err)
}
os.Exit(1)
}
return lf
}
// loadLockfileForValidate is loadLockfileOrFail, except that a repository
// without a lockfile but with a labels file still gets its labels checked
// (own items only) instead of failing on the missing lockfile.
func loadLockfileForValidate(cmd *cobra.Command) *lockfile.Lockfile {
if _, err := os.Stat(filepath.Join(".", lockfile.FileName)); os.IsNotExist(err) {
if _, err := os.Stat(labels.FileName); err == nil {
return lockfile.New()
}
}
return loadLockfileOrFail(cmd)
}
func emitJSON(cmd *cobra.Command, v any) error {
enc := json.NewEncoder(cmd.OutOrStdout())
enc.SetIndent("", " ")
return enc.Encode(v)
}
func newValidateCmd() *cobra.Command {
var asJSON bool
cmd := &cobra.Command{
Use: "validate",
Short: "Check lockfile rules and external-area consistency offline",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
lf := loadLockfileForValidate(cmd)
result := vendor.Validate(".", lf)
if asJSON {
return emitJSON(cmd, result)
}
w := cmd.OutOrStdout()
if result.Valid {
fmt.Fprintf(w, "lockfile valid (%d sources)\n", len(lf.Sources))
if _, err := os.Stat(labels.FileName); err == nil {
fmt.Fprintf(w, "%s valid\n", labels.FileName)
}
return nil
}
for _, e := range result.Errors {
fmt.Fprintf(w, "%s: %s\n", e.Rule, e.Message)
}
cmd.SilenceErrors = true
os.Exit(1)
return nil
},
}
cmd.Flags().BoolVar(&asJSON, "json", false, "machine-readable output")
return cmd
}
func newVendorListCmd() *cobra.Command {
var asJSON bool
cmd := &cobra.Command{
Use: "list",
Short: "List all vendored sources with pin, mode and item counts",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
lf := loadLockfileOrFail(cmd)
rows := vendor.List(lf)
if asJSON {
return emitJSON(cmd, map[string]any{"sources": rows})
}
w := cmd.OutOrStdout()
if len(rows) == 0 {
fmt.Fprintln(w, "no sources vendored yet")
return nil
}
fmt.Fprintf(w, "%-16s %-6s %-9s %-8s %s\n", "SOURCE", "REF", "REV", "MODE", "URL")
for _, r := range rows {
rev := r.Rev
if len(rev) > 8 {
rev = rev[:8]
}
fmt.Fprintf(w, "%-16s %-6s %-9s %-8s %s\n", r.Name, r.Ref, rev, r.Mode, r.URL)
fmt.Fprintf(w, "%16s %s\n", "", countsLine(r.Counts))
}
return nil
},
}
cmd.Flags().BoolVar(&asJSON, "json", false, "machine-readable output")
return cmd
}
func countsLine(counts map[string]int) string {
line := ""
total := 0
for _, t := range lockfile.Types {
if n, ok := counts[t]; ok {
if line != "" {
line += ", "
}
line += fmt.Sprintf("%d %s", n, t)
total += n
}
}
return fmt.Sprintf("%d items: %s", total, line)
}
func newVendorInspectCmd() *cobra.Command {
var asJSON bool
cmd := &cobra.Command{
Use: "inspect <name>",
Short: "Show one source's full item inventory with metadata and warnings",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
lf := loadLockfileOrFail(cmd)
report, err := vendor.Inspect(".", args[0], lf)
if err != nil {
return err
}
if asJSON {
return emitJSON(cmd, report)
}
w := cmd.OutOrStdout()
fmt.Fprintf(w, "source %s\n", report.Name)
fmt.Fprintf(w, " url: %s\n", report.URL)
fmt.Fprintf(w, " ref: %s\n", report.Ref)
fmt.Fprintf(w, " rev: %s\n", report.Rev)
fmt.Fprintf(w, " mode: %s\n", report.Mode)
for _, t := range lockfile.Types {
items := report.Items[t]
if len(items) == 0 {
continue
}
fmt.Fprintf(w, " %s:\n", t)
for _, it := range items {
fmt.Fprintf(w, " %s", it.DeployedID)
if it.DeployedID != it.UpstreamID {
fmt.Fprintf(w, " (upstream: %s)", it.UpstreamID)
}
if it.Name != "" {
fmt.Fprintf(w, " name=%s", it.Name)
}
fmt.Fprintln(w)
for _, warn := range it.Warnings {
fmt.Fprintf(w, " warning: %s\n", warn)
}
}
}
return nil
},
}
cmd.Flags().BoolVar(&asJSON, "json", false, "machine-readable output")
return cmd
}