@@ -38,9 +38,7 @@ func main() {
3838 Use : "kagent" ,
3939 Short : "kagent is a CLI and TUI for kagent" ,
4040 Long : "kagent is a CLI and TUI for kagent" ,
41- Run : func (cmd * cobra.Command , args []string ) {
42- runInteractive ()
43- },
41+ Run : runInteractive ,
4442 }
4543
4644 rootCmd .PersistentFlags ().StringVar (& cfg .KAgentURL , "kagent-url" , "http://localhost:8083" , "KAgent URL" )
@@ -101,8 +99,8 @@ func main() {
10199 Short : "Generate a bug report" ,
102100 Long : `Generate a bug report` ,
103101 Run : func (cmd * cobra.Command , args []string ) {
104- if err := cli .CheckServerConnection (cfg .Client ()); err != nil {
105- pf , err := cli .NewPortForward (ctx , cfg )
102+ if err := cli .CheckServerConnection (cmd . Context (), cfg .Client ()); err != nil {
103+ pf , err := cli .NewPortForward (cmd . Context () , cfg )
106104 if err != nil {
107105 fmt .Fprintf (os .Stderr , "Error starting port-forward: %v\n " , err )
108106 return
@@ -118,15 +116,15 @@ func main() {
118116 Short : "Print the kagent version" ,
119117 Long : `Print the kagent version` ,
120118 Run : func (cmd * cobra.Command , args []string ) {
121- if err := cli .CheckServerConnection (cfg .Client ()); err != nil {
122- pf , err := cli .NewPortForward (ctx , cfg )
123- if err != nil {
124- fmt .Fprintf (os .Stderr , "Error starting port-forward: %v\n " , err )
125- return
126- }
119+ // print out kagent CLI version regardless if a port-forward to kagent server succeeds
120+ // versions unable to obtain from the remote kagent will be reported as "unknown"
121+ defer cli .VersionCmd (cfg )
122+
123+ if err := cli .CheckServerConnection (cmd .Context (), cfg .Client ()); err != nil {
124+ // silently error if kagent server is not reachable
125+ pf , _ := cli .NewPortForward (cmd .Context (), cfg )
127126 defer pf .Stop ()
128127 }
129- cli .VersionCmd (cfg )
130128 },
131129 }
132130
@@ -135,7 +133,7 @@ func main() {
135133 Short : "Open the kagent dashboard" ,
136134 Long : `Open the kagent dashboard` ,
137135 Run : func (cmd * cobra.Command , args []string ) {
138- cli .DashboardCmd (ctx , cfg )
136+ cli .DashboardCmd (cmd . Context () , cfg )
139137 },
140138 }
141139
@@ -155,8 +153,8 @@ func main() {
155153 Short : "Get a session or list all sessions" ,
156154 Long : `Get a session by ID or list all sessions` ,
157155 Run : func (cmd * cobra.Command , args []string ) {
158- if err := cli .CheckServerConnection (cfg .Client ()); err != nil {
159- pf , err := cli .NewPortForward (ctx , cfg )
156+ if err := cli .CheckServerConnection (cmd . Context (), cfg .Client ()); err != nil {
157+ pf , err := cli .NewPortForward (cmd . Context () , cfg )
160158 if err != nil {
161159 fmt .Fprintf (os .Stderr , "Error starting port-forward: %v\n " , err )
162160 return
@@ -176,8 +174,8 @@ func main() {
176174 Short : "Get an agent or list all agents" ,
177175 Long : `Get an agent by name or list all agents` ,
178176 Run : func (cmd * cobra.Command , args []string ) {
179- if err := cli .CheckServerConnection (cfg .Client ()); err != nil {
180- pf , err := cli .NewPortForward (ctx , cfg )
177+ if err := cli .CheckServerConnection (cmd . Context (), cfg .Client ()); err != nil {
178+ pf , err := cli .NewPortForward (cmd . Context () , cfg )
181179 if err != nil {
182180 return
183181 }
@@ -196,8 +194,8 @@ func main() {
196194 Short : "Get tools" ,
197195 Long : `List all available tools` ,
198196 Run : func (cmd * cobra.Command , args []string ) {
199- if err := cli .CheckServerConnection (cfg .Client ()); err != nil {
200- pf , err := cli .NewPortForward (ctx , cfg )
197+ if err := cli .CheckServerConnection (cmd . Context (), cfg .Client ()); err != nil {
198+ pf , err := cli .NewPortForward (cmd . Context () , cfg )
201199 if err != nil {
202200 fmt .Fprintf (os .Stderr , "Error starting port-forward: %v\n " , err )
203201 return
@@ -313,7 +311,7 @@ Examples:
313311 Run : func (cmd * cobra.Command , args []string ) {
314312 deployCfg .ProjectDir = args [0 ]
315313
316- if err := cli .DeployCmd (ctx , deployCfg ); err != nil {
314+ if err := cli .DeployCmd (cmd . Context () , deployCfg ); err != nil {
317315 fmt .Fprintf (os .Stderr , "Error: %v\n " , err )
318316 os .Exit (1 )
319317 }
@@ -343,21 +341,19 @@ Examples:
343341
344342}
345343
346- func runInteractive () {
344+ func runInteractive (cmd * cobra. Command , args [] string ) {
347345 cfg , err := config .Get ()
348346 if err != nil {
349347 fmt .Fprintf (os .Stderr , "Error getting config: %v\n " , err )
350348 os .Exit (1 )
351349 }
352350
353351 client := cfg .Client ()
354- ctx , cancel := context .WithCancel (context .Background ())
355- defer cancel ()
356352
357353 // Start port forward and ensure it is healthy.
358354 var pf * cli.PortForward
359- if err := cli .CheckServerConnection (client ); err != nil {
360- pf , err = cli .NewPortForward (ctx , cfg )
355+ if err := cli .CheckServerConnection (cmd . Context (), client ); err != nil {
356+ pf , err = cli .NewPortForward (cmd . Context () , cfg )
361357 if err != nil {
362358 fmt .Fprintf (os .Stderr , "Error starting port-forward: %v\n " , err )
363359 return
0 commit comments