From 87d38a43e01065b2a4e350cb4d623d50807a6a61 Mon Sep 17 00:00:00 2001 From: Moritz Clasmeier Date: Wed, 29 Oct 2025 21:32:43 +0100 Subject: [PATCH] Implement 'logs operator' command. --- cmd/logs.go | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++ cmd/main.go | 1 + 2 files changed, 120 insertions(+) create mode 100644 cmd/logs.go diff --git a/cmd/logs.go b/cmd/logs.go new file mode 100644 index 00000000..cb7b51d8 --- /dev/null +++ b/cmd/logs.go @@ -0,0 +1,119 @@ +package main + +import ( + "bufio" + "fmt" + "io" + "os" + "os/exec" + "strings" + + "github.com/fatih/color" + "github.com/spf13/cobra" +) + +var ( + followLogs bool +) + +func newLogsCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "logs", + Short: "View logs for ACS components", + Long: `View logs for various ACS components.`, + } + + cmd.AddCommand(newLogsOperatorCmd()) + + return cmd +} + +func newLogsOperatorCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "operator", + Short: "View logs for the RHACS operator", + Long: `View logs for the RHACS operator running in the rhacs-operator-system namespace.`, + RunE: runLogsOperator, + } + + cmd.Flags().BoolVarP(&followLogs, "follow", "f", false, "Follow log output (stream logs)") + + return cmd +} + +func runLogsOperator(cmd *cobra.Command, args []string) error { + // Build kubectl command + kubectlArgs := []string{ + "-n", "rhacs-operator-system", + "logs", + "-l", "app=rhacs-operator", + } + + if followLogs { + kubectlArgs = append(kubectlArgs, "-f") + } + + // Create kubectl command + kubectlCmd := exec.Command("kubectl", kubectlArgs...) + + // Get stdout pipe for streaming + stdout, err := kubectlCmd.StdoutPipe() + if err != nil { + return fmt.Errorf("failed to create stdout pipe: %w", err) + } + + // Pass through stderr directly + kubectlCmd.Stderr = os.Stderr + + // Start the command + if err := kubectlCmd.Start(); err != nil { + return fmt.Errorf("failed to start kubectl: %w", err) + } + + // Process stdout with color coding + processLogStream(stdout) + + // Wait for command to complete + if err := kubectlCmd.Wait(); err != nil { + // Don't return error if kubectl was interrupted (e.g., Ctrl+C) + if _, ok := err.(*exec.ExitError); ok { + return nil + } + return fmt.Errorf("kubectl command failed: %w", err) + } + + return nil +} + +func processLogStream(reader io.Reader) { + scanner := bufio.NewScanner(reader) + + // Define colors for different log levels + infoColor := color.New(color.FgWhite) + debugColor := color.New(color.Faint) + errorColor := color.New(color.FgRed, color.Bold) + warnColor := color.New(color.FgYellow) + + for scanner.Scan() { + line := scanner.Text() + + // Determine log level and apply appropriate color + switch { + case strings.Contains(line, "ERROR"): + errorColor.Println(line) + case strings.Contains(line, "WARN"): + warnColor.Println(line) + case strings.Contains(line, "DEBUG"): + debugColor.Println(line) + case strings.Contains(line, "INFO"): + infoColor.Println(line) + default: + // Default to info color for unrecognized lines + infoColor.Println(line) + } + } + + if err := scanner.Err(); err != nil { + fmt.Fprintf(os.Stderr, "Error reading logs: %v\n", err) + } +} diff --git a/cmd/main.go b/cmd/main.go index 64aa6390..23c976bf 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -46,4 +46,5 @@ func init() { rootCmd.AddCommand(newTeardownCmd()) rootCmd.AddCommand(newVersionCmd()) rootCmd.AddCommand(newEnvCmd()) + rootCmd.AddCommand(newLogsCmd()) }