diff --git a/README.md b/README.md index a05de73..5cb47e7 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,7 @@ Key commands inside the shell: | `ftp download `| Download a file via FTP/FTPS. | | `help` | Print the command catalogue. | | `clear` | Clear the terminal and reprint the banner. | +| `htop` | Launch `htop` with the ServerCommander color theme. | | `exit` | Gracefully shut down ServerCommander. | > **Note:** When prompted for the authentication method during `session add`, enter `password` or `private_key`. Passwords are never stored—if you choose `password` you will be asked for it when connecting. diff --git a/docs/USAGE.md b/docs/USAGE.md index da68051..674e209 100644 --- a/docs/USAGE.md +++ b/docs/USAGE.md @@ -112,21 +112,15 @@ server-commander logs server_id This command shows the most recent logs from the specified server. -### 11. Process Management +### 11. System Monitoring with `htop` -To list running processes on the server: +Launch the interactive `htop` monitor using the ServerCommander color palette: ```bash -server-commander processes server_id +server-commander htop ``` -To kill a running process: - -```bash -server-commander kill-process server_id --pid -``` - -This allows you to monitor and manage processes on the server remotely. +This opens `htop` in the current terminal session. Press `q` to exit the monitor and return to the ServerCommander console. ## Additional Options diff --git a/src/cmd/assets/htoprc b/src/cmd/assets/htoprc new file mode 100644 index 0000000..6e43c21 --- /dev/null +++ b/src/cmd/assets/htoprc @@ -0,0 +1,19 @@ +# ServerCommander htop theme +# Generated configuration to favor cyan and green hues matching the console palette. +fields=0 48 17 18 38 39 46 47 +sort_key=46 +sort_direction=1 +hide_threads=0 +hide_kernel_threads=1 +hide_userland_threads=0 +shadow_other_users=0 +highlight_base_name=1 +highlight_megabytes=1 +highlight_threads=1 +tree_view=0 +header_margin=1 +color_scheme=5 +left_meters=AllCPUs2 Memory Swap +left_meter_modes=1 1 1 +right_meters=Tasks LoadAverage Uptime +right_meter_modes=2 2 2 diff --git a/src/cmd/htop.go b/src/cmd/htop.go new file mode 100644 index 0000000..6eceae2 --- /dev/null +++ b/src/cmd/htop.go @@ -0,0 +1,74 @@ +package cmd + +import ( + "errors" + "fmt" + "os" + "os/exec" + "path/filepath" + "runtime" + + "servercommander/src/utils" +) + +func init() { + RegisterCommand("htop", "Launch the system monitor with ServerCommander colors", htopCommand) +} + +func htopCommand(args []string) error { + if err := ensureUsage(args, 0, 0, "htop"); err != nil { + return err + } + + if runtime.GOOS == "windows" { + return errors.New("htop is not available on Windows. Please install an alternative process monitor.") + } + + binary, err := exec.LookPath("htop") + if err != nil { + return fmt.Errorf("htop executable not found in PATH: %w", err) + } + + themePath, cleanup, err := prepareHtopTheme() + if err != nil { + return err + } + if cleanup != nil { + defer cleanup() + } + + fmt.Printf("%sLaunching htop with ServerCommander theme. Press 'q' to return to the console.%s\n", utils.Green, utils.Reset) + + cmd := exec.Command(binary) + cmd.Stdin = os.Stdin + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + if themePath != "" { + cmd.Env = append(os.Environ(), fmt.Sprintf("HTOPRC=%s", themePath)) + } + + return cmd.Run() +} + +func prepareHtopTheme() (string, func(), error) { + if len(htopTheme) == 0 { + return "", nil, nil + } + + dir, err := os.MkdirTemp("", "servercommander-htop") + if err != nil { + return "", nil, fmt.Errorf("failed to create temporary configuration directory: %w", err) + } + + path := filepath.Join(dir, "htoprc") + if err := os.WriteFile(path, htopTheme, 0o600); err != nil { + os.RemoveAll(dir) + return "", nil, fmt.Errorf("failed to prepare htop theme: %w", err) + } + + cleanup := func() { + _ = os.RemoveAll(dir) + } + + return path, cleanup, nil +} diff --git a/src/cmd/htop_theme.go b/src/cmd/htop_theme.go new file mode 100644 index 0000000..c5e6657 --- /dev/null +++ b/src/cmd/htop_theme.go @@ -0,0 +1,6 @@ +package cmd + +import "embed" + +//go:embed assets/htoprc +var htopTheme []byte