@@ -2,7 +2,9 @@ package cli
22
33import (
44 "fmt"
5+ "os"
56
7+ "github.com/LAA-Software-Engineering/agentic-control-plane/internal/render"
68 "github.com/spf13/cobra"
79)
810
@@ -11,18 +13,25 @@ var Version = "0.0.0-dev"
1113
1214// Execute runs the root command.
1315func Execute () error {
14- return newRootCmd ().Execute ()
16+ return NewRootCmd ().Execute ()
1517}
1618
17- func newRootCmd () * cobra.Command {
19+ // NewRootCmd builds the agentctl command tree (exposed for tests).
20+ func NewRootCmd () * cobra.Command {
21+ global = Global {}
1822 root := & cobra.Command {
19- Use : "agentctl" ,
20- Short : "Declarative control plane for agent systems" ,
21- Long : "agentctl validates, plans, applies, and runs declarative agent systems defined as YAML." ,
23+ Use : "agentctl" ,
24+ Short : "Declarative control plane for agent systems" ,
25+ Long : "agentctl validates, plans, applies, and runs declarative agent systems defined as YAML." ,
26+ SilenceErrors : true ,
2227 Run : func (cmd * cobra.Command , args []string ) {
2328 _ = cmd .Help ()
2429 },
30+ PersistentPreRunE : func (cmd * cobra.Command , args []string ) error {
31+ return ValidateGlobals ()
32+ },
2533 }
34+ BindPersistentFlags (root )
2635 root .AddCommand (newVersionCmd ())
2736 return root
2837}
@@ -31,8 +40,36 @@ func newVersionCmd() *cobra.Command {
3140 return & cobra.Command {
3241 Use : "version" ,
3342 Short : "Print version information" ,
34- Run : func (cmd * cobra.Command , args []string ) {
35- fmt .Println (Version )
43+ RunE : func (cmd * cobra.Command , args []string ) error {
44+ g := Globals ()
45+ out := cmd .OutOrStdout ()
46+ switch g .Output {
47+ case render .FormatJSON :
48+ payload := struct {
49+ Version string `json:"version"`
50+ }{Version : Version }
51+ if err := render .WriteJSON (out , payload ); err != nil {
52+ return err
53+ }
54+ case render .FormatYAML :
55+ if err := render .WriteYAML (out , map [string ]string {"version" : Version }); err != nil {
56+ return err
57+ }
58+ default :
59+ if _ , err := render .Fprintf (out , "%s\n " , Version ); err != nil {
60+ return err
61+ }
62+ }
63+ return nil
3664 },
3765 }
3866}
67+
68+ // Main is an optional entrypoint that maps errors to exit codes and writes diagnostics to stderr.
69+ func Main () int {
70+ if err := Execute (); err != nil {
71+ fmt .Fprintln (os .Stderr , err )
72+ return ExitCodeOf (err )
73+ }
74+ return ExitSuccess
75+ }
0 commit comments