Files
oh-my-posh/src/dsc/cli.go
T
ClaudeandJan De Dobbeleer 2a59ed1e50 refactor(cli): replace cobra and pflag with minimal internal packages
The CLI framework was the largest remaining third-party chunk (545 kB)
and most of its surface went unused - completions are explicitly
disabled, templates and command groups never used. Replace both with
internal packages mirroring the exact API subset in use: a command tree
with persistent flags and nearest-hook semantics, POSIX flag parsing
(--flag=value, --flag value, shorthands and grouping, the -- terminator,
interspersed positionals, unknown-flag allowlisting for the argocd
segment), positional validators, the implicit help command, and the
Windows Explorer double-click guard.

Help, usage and error output were verified byte-identical against the
previous binary across a 47-case golden battery: every command and
subcommand help screen, error formats and exit codes, flag styles
including flags before the subcommand, and init/print rendering.

Shrinks the stripped linux/amd64 binary by 586 kB (13.50 MB -> 12.91 MB;
23.7% below the original 16.93 MB).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Qiyvpiy5jR2tyzwZ3zUki5
2026-07-31 07:40:24 +02:00

87 lines
1.6 KiB
Go

package dsc
import (
"fmt"
"os"
"github.com/jandedobbeleer/oh-my-posh/src/cache"
"github.com/jandedobbeleer/oh-my-posh/src/cmdtree"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
var (
state string
)
type resource interface {
Load()
Save()
Resolve()
ToJSON() string
Schema() string
Apply(schema string) error
Test(input string) error
}
func Command(r resource) *cmdtree.Command {
cmd := &cmdtree.Command{
Use: "dsc",
Short: "Manage Oh My Posh DSC (Desired State Configuration)",
Long: "Manage Oh My Posh DSC (Desired State Configuration).",
ValidArgs: []string{"get", "set", "test", "schema", "export"},
Run: func(cmd *cmdtree.Command, args []string) {
if len(args) == 0 {
_ = cmd.Help()
return
}
env := &runtime.Terminal{}
env.Init(&runtime.Flags{})
cache.Init(os.Getenv("POSH_SHELL"), cache.Persist)
defer func() {
cache.Close()
}()
var err error
switch args[0] {
case "get", "export":
r.Load()
r.Resolve()
fmt.Print(r.ToJSON())
case "set":
if state == "" {
err = newError("please provide a state configuration to set")
break
}
r.Load()
err = r.Apply(state)
case "schema":
fmt.Print(r.Schema())
case "test":
if state == "" {
err = newError("please provide a state configuration to test")
break
}
r.Load()
err = r.Test(state)
default:
_ = cmd.Help()
return
}
if err != nil {
fmt.Println(err.Error())
return
}
},
}
cmd.Flags().StringVar(&state, "state", "", "State configuration to set")
return cmd
}