Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions go/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ else
GOBIN=$(shell go env GOBIN)
endif

default: help

.PHONY: help
help: ## list makefile targets
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

##@ Development

.PHONY: manifests
Expand Down
46 changes: 21 additions & 25 deletions go/cli/cmd/kagent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ func main() {
Use: "kagent",
Short: "kagent is a CLI and TUI for kagent",
Long: "kagent is a CLI and TUI for kagent",
Run: func(cmd *cobra.Command, args []string) {
runInteractive()
},
Run: runInteractive,
}

rootCmd.PersistentFlags().StringVar(&cfg.KAgentURL, "kagent-url", "http://localhost:8083", "KAgent URL")
Expand Down Expand Up @@ -101,8 +99,8 @@ func main() {
Short: "Generate a bug report",
Long: `Generate a bug report`,
Run: func(cmd *cobra.Command, args []string) {
if err := cli.CheckServerConnection(cfg.Client()); err != nil {
pf, err := cli.NewPortForward(ctx, cfg)
if err := cli.CheckServerConnection(cmd.Context(), cfg.Client()); err != nil {
pf, err := cli.NewPortForward(cmd.Context(), cfg)
if err != nil {
fmt.Fprintf(os.Stderr, "Error starting port-forward: %v\n", err)
return
Expand All @@ -118,15 +116,15 @@ func main() {
Short: "Print the kagent version",
Long: `Print the kagent version`,
Run: func(cmd *cobra.Command, args []string) {
if err := cli.CheckServerConnection(cfg.Client()); err != nil {
pf, err := cli.NewPortForward(ctx, cfg)
if err != nil {
fmt.Fprintf(os.Stderr, "Error starting port-forward: %v\n", err)
return
}
// print out kagent CLI version regardless if a port-forward to kagent server succeeds
// versions unable to obtain from the remote kagent will be reported as "unknown"
defer cli.VersionCmd(cfg)

if err := cli.CheckServerConnection(cmd.Context(), cfg.Client()); err != nil {
// silently error if kagent server is not reachable
pf, _ := cli.NewPortForward(cmd.Context(), cfg)
defer pf.Stop()
}
cli.VersionCmd(cfg)
},
}

Expand All @@ -135,7 +133,7 @@ func main() {
Short: "Open the kagent dashboard",
Long: `Open the kagent dashboard`,
Run: func(cmd *cobra.Command, args []string) {
cli.DashboardCmd(ctx, cfg)
cli.DashboardCmd(cmd.Context(), cfg)
},
}

Expand All @@ -155,8 +153,8 @@ func main() {
Short: "Get a session or list all sessions",
Long: `Get a session by ID or list all sessions`,
Run: func(cmd *cobra.Command, args []string) {
if err := cli.CheckServerConnection(cfg.Client()); err != nil {
pf, err := cli.NewPortForward(ctx, cfg)
if err := cli.CheckServerConnection(cmd.Context(), cfg.Client()); err != nil {
pf, err := cli.NewPortForward(cmd.Context(), cfg)
if err != nil {
fmt.Fprintf(os.Stderr, "Error starting port-forward: %v\n", err)
return
Expand All @@ -176,8 +174,8 @@ func main() {
Short: "Get an agent or list all agents",
Long: `Get an agent by name or list all agents`,
Run: func(cmd *cobra.Command, args []string) {
if err := cli.CheckServerConnection(cfg.Client()); err != nil {
pf, err := cli.NewPortForward(ctx, cfg)
if err := cli.CheckServerConnection(cmd.Context(), cfg.Client()); err != nil {
pf, err := cli.NewPortForward(cmd.Context(), cfg)
if err != nil {
return
}
Expand All @@ -196,8 +194,8 @@ func main() {
Short: "Get tools",
Long: `List all available tools`,
Run: func(cmd *cobra.Command, args []string) {
if err := cli.CheckServerConnection(cfg.Client()); err != nil {
pf, err := cli.NewPortForward(ctx, cfg)
if err := cli.CheckServerConnection(cmd.Context(), cfg.Client()); err != nil {
pf, err := cli.NewPortForward(cmd.Context(), cfg)
if err != nil {
fmt.Fprintf(os.Stderr, "Error starting port-forward: %v\n", err)
return
Expand Down Expand Up @@ -313,7 +311,7 @@ Examples:
Run: func(cmd *cobra.Command, args []string) {
deployCfg.ProjectDir = args[0]

if err := cli.DeployCmd(ctx, deployCfg); err != nil {
if err := cli.DeployCmd(cmd.Context(), deployCfg); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
Expand Down Expand Up @@ -343,21 +341,19 @@ Examples:

}

func runInteractive() {
func runInteractive(cmd *cobra.Command, args []string) {
cfg, err := config.Get()
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting config: %v\n", err)
os.Exit(1)
}

client := cfg.Client()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// Start port forward and ensure it is healthy.
var pf *cli.PortForward
if err := cli.CheckServerConnection(client); err != nil {
pf, err = cli.NewPortForward(ctx, cfg)
if err := cli.CheckServerConnection(cmd.Context(), client); err != nil {
pf, err = cli.NewPortForward(cmd.Context(), cfg)
if err != nil {
fmt.Fprintf(os.Stderr, "Error starting port-forward: %v\n", err)
return
Expand Down
3 changes: 1 addition & 2 deletions go/cli/internal/cli/agent/invoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ type InvokeCfg struct {
}

func InvokeCmd(ctx context.Context, cfg *InvokeCfg) {

clientSet := cfg.Config.Client()

if err := CheckServerConnection(clientSet); err != nil {
if err := CheckServerConnection(ctx, clientSet); err != nil {
// If a connection does not exist, start a short-lived port-forward.
pf, err := NewPortForward(ctx, cfg.Config)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions go/cli/internal/cli/agent/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ var (
ErrServerConnection = fmt.Errorf("error connecting to server. Please run 'install' command first")
)

func CheckServerConnection(client *client.ClientSet) error {
func CheckServerConnection(ctx context.Context, client *client.ClientSet) error {
// Only check if we have a valid client
if client == nil {
return ErrServerConnection
}

ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
ctx, cancel := context.WithTimeout(ctx, time.Second*5)
defer cancel()
_, err := client.Version.GetVersion(ctx)
if err != nil {
Expand Down Expand Up @@ -50,7 +50,7 @@ func NewPortForward(ctx context.Context, cfg *config.Config) (*PortForward, erro
client := cfg.Client()
var err error
for i := 0; i < 10; i++ {
err = CheckServerConnection(client)
err = CheckServerConnection(ctx, client)
if err == nil {
// Connection successful, port-forward is working
return &PortForward{
Expand Down
Loading