diff --git a/apps/cli/cmd/agent_status.go b/apps/cli/cmd/agent_status.go new file mode 100644 index 0000000..e1739bb --- /dev/null +++ b/apps/cli/cmd/agent_status.go @@ -0,0 +1,78 @@ +package cmd + +import ( + "fmt" + "os" + + "github.com/Open-Source-Kigali/docksight/apps/cli/cmd/internal/agent/install" + "github.com/Open-Source-Kigali/docksight/apps/cli/cmd/internal/system" + "github.com/Open-Source-Kigali/docksight/apps/cli/cmd/internal/ui" + + "github.com/spf13/cobra" +) + +var agentStatusCMD = &cobra.Command{ + Use: "status", + Short: "Show the agent service status", + Long: "Report whether the agent is installed and running, which platform " + + "URL it targets, and whether an identity has been issued. Exits non-zero " + + "when the agent is not installed or not running.", + + RunE: func(cmd *cobra.Command, args []string) error { + layout := agentLayout() + + if _, err := os.Stat(layout.ConfigPath()); err != nil { + if os.IsNotExist(err) { + return fmt.Errorf( + "agent is not installed (no config at %s)", + layout.ConfigPath(), + ) + } + return err + } + + manager := install.NewController(layout) + unit := layout.ServiceName + + active, err := manager.IsActive(cmd.Context(), unit) + if err != nil { + if isPermissionError(err) { + return fmt.Errorf( + "could not query %s status: %w\n %s", + unit, + err, + system.ElevationHint(), + ) + } + return fmt.Errorf("could not query %s status: %w", unit, err) + } + + serverURL, err := install.ReadServerURL(layout) + if err != nil { + serverURL = "(unavailable: " + err.Error() + ")" + } + + identity := "not issued" + if install.IdentityExists(layout) { + identity = "issued" + } + + if active { + ui.Success(unit + ": running") + } else { + ui.Warning(unit + ": not running") + } + ui.Info("Platform URL: " + serverURL) + ui.Info("Identity: " + identity) + ui.Info("Follow logs: " + layout.LogsCommand()) + + if !active { + return fmt.Errorf("%s is not running", unit) + } + return nil + }, +} + +func init() { + agentCMD.AddCommand(agentStatusCMD) +} diff --git a/apps/cli/cmd/agent_stubs.go b/apps/cli/cmd/agent_stubs.go index 49801a3..a9288a5 100644 --- a/apps/cli/cmd/agent_stubs.go +++ b/apps/cli/cmd/agent_stubs.go @@ -15,27 +15,22 @@ var agentPlaceholders = []struct { }{ {"uninstall", "Remove the DockSight Agent from this host"}, // start / stop / restart are real commands in agent_service.go - {"status", "Show the agent service status"}, + // status is a real command in agent_status.go {"logs", "Show the agent service logs"}, } func init() { - for _, placeholder := range agentPlaceholders { - command := &cobra.Command{ Use: placeholder.use, Short: placeholder.short, - RunE: func(cmd *cobra.Command, args []string) error { - return fmt.Errorf( "docksight agent %s will be ready soon", cmd.Name(), ) }, } - agentCMD.AddCommand(command) } }