@@ -32,7 +32,7 @@ type rootFlags struct {
3232 logFile io.Closer
3333}
3434
35- func isCliPLugin () bool {
35+ func isDockerAgent () bool {
3636 cliPluginBinary := "docker-agent"
3737 if runtime .GOOS == "windows" {
3838 cliPluginBinary += ".exe"
@@ -110,7 +110,7 @@ func NewRootCmd() *cobra.Command {
110110 cmd .AddGroup (& cobra.Group {ID : "core" , Title : "Core Commands:" })
111111 cmd .AddGroup (& cobra.Group {ID : "advanced" , Title : "Advanced Commands:" })
112112
113- if isCliPLugin () {
113+ if isDockerAgent () {
114114 cmd .Use = "agent"
115115 cmd .Short = "create or run AI agents"
116116 cmd .Long = "create or run AI agents"
@@ -147,44 +147,55 @@ We collect anonymous usage data to help improve cagent. To disable:
147147 }
148148
149149 rootCmd := NewRootCmd ()
150-
151- // When no subcommand is given, default to "run" (which runs the default agent).
152- // Users can use "cagent --help" to see available commands.
153- args = defaultToRun (rootCmd , args )
154-
155- rootCmd .SetArgs (args )
156150 rootCmd .SetIn (stdin )
157151 rootCmd .SetOut (stdout )
158152 rootCmd .SetErr (stderr )
153+ setContextRecursive (ctx , rootCmd )
159154
160- if isCliPLugin () {
161- plugin .Run (func (dockerCli command.Cli ) * cobra.Command {
162- originalPreRun := rootCmd .PersistentPreRunE
163- rootCmd .PersistentPreRunE = func (cmd * cobra.Command , args []string ) error {
164- if err := plugin .PersistentPreRunE (cmd , args ); err != nil {
165- return err
166- }
167- if originalPreRun != nil {
168- if err := originalPreRun (cmd , args ); err != nil {
169- return processErr (ctx , err , stderr , rootCmd )
170- }
155+ if plugin .RunningStandalone () {
156+ // When no subcommand is given, default to "run".
157+ rootCmd .SetArgs (defaultToRun (rootCmd , args ))
158+
159+ if err := rootCmd .Execute (); err != nil {
160+ return processErr (ctx , err , stderr , rootCmd )
161+ }
162+ return nil
163+ }
164+
165+ // When no subcommand is given, default to "run".
166+ rootCmd .SetArgs (append (args [0 :1 ], defaultToRun (rootCmd , args [1 :])... ))
167+ os .Args = append (os .Args [0 :2 ], defaultToRun (rootCmd , os .Args [2 :])... )
168+
169+ plugin .Run (func (dockerCli command.Cli ) * cobra.Command {
170+ originalPreRun := rootCmd .PersistentPreRunE
171+ rootCmd .PersistentPreRunE = func (cmd * cobra.Command , args []string ) error {
172+ if err := plugin .PersistentPreRunE (cmd , args ); err != nil {
173+ return err
174+ }
175+ if originalPreRun != nil {
176+ if err := originalPreRun (cmd , args ); err != nil {
177+ return processErr (cmd .Context (), err , stderr , rootCmd )
171178 }
172- return nil
173179 }
174- rootCmd .SetContext (ctx )
175- return rootCmd
176- }, metadata.Metadata {
177- SchemaVersion : "0.1.0" ,
178- Vendor : "Docker Inc." ,
179- Version : version .Version ,
180- })
181- } else if err := rootCmd .ExecuteContext (ctx ); err != nil {
182- return processErr (ctx , err , stderr , rootCmd )
183- }
180+ return nil
181+ }
182+ return rootCmd
183+ }, metadata.Metadata {
184+ SchemaVersion : "0.1.0" ,
185+ Vendor : "Docker Inc." ,
186+ Version : version .Version ,
187+ })
184188
185189 return nil
186190}
187191
192+ func setContextRecursive (ctx context.Context , cmd * cobra.Command ) {
193+ cmd .SetContext (ctx )
194+ for _ , child := range cmd .Commands () {
195+ setContextRecursive (ctx , child )
196+ }
197+ }
198+
188199// defaultToRun prepends "run" to the argument list when no subcommand is
189200// specified so that bare "cagent" (or "cagent --debug", etc.) launches the
190201// default agent. Help flags (--help / -h) are left alone.
0 commit comments