-
Notifications
You must be signed in to change notification settings - Fork 5
feat(cli): add kernel status command
#123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jarugupj
wants to merge
6
commits into
main
Choose a base branch
from
phani/kernel-status-cli
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+143
−19
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d58d21a
feat(cli): add `kernel status` command
jarugupj 551d394
Merge branch 'main' into phani/kernel-status-cli
jarugupj c19a361
fix(cmd/status): handle non-2xx responses from Kernel API
jarugupj 44667b0
Merge branch 'phani/kernel-status-cli' of github.com:onkernel/cli int…
jarugupj 6c3272b
Address PR review: extract shared GetBaseURL helper, fix double error…
jarugupj 95e2273
Merge branch 'main' into phani/kernel-status-cli
jarugupj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "os" | ||
| "time" | ||
|
|
||
| "github.com/kernel/cli/pkg/util" | ||
| "github.com/pterm/pterm" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| type statusComponent struct { | ||
| Name string `json:"name"` | ||
| Status string `json:"status"` | ||
| } | ||
|
|
||
| type statusGroup struct { | ||
| Name string `json:"name"` | ||
| Status string `json:"status"` | ||
| Components []statusComponent `json:"components"` | ||
| } | ||
|
|
||
| type statusResponse struct { | ||
| Status string `json:"status"` | ||
| Groups []statusGroup `json:"groups"` | ||
| } | ||
|
|
||
| var statusCmd = &cobra.Command{ | ||
| Use: "status", | ||
| Short: "Check the operational status of Kernel services", | ||
| RunE: runStatus, | ||
| } | ||
|
|
||
| func init() { | ||
| statusCmd.Flags().StringP("output", "o", "", "Output format (json)") | ||
| } | ||
|
|
||
| func runStatus(cmd *cobra.Command, args []string) error { | ||
| output, _ := cmd.Flags().GetString("output") | ||
|
|
||
| client := &http.Client{Timeout: 10 * time.Second} | ||
| resp, err := client.Get(util.GetBaseURL() + "/status") | ||
| if err != nil { | ||
| pterm.Error.Println("Could not reach Kernel API. Check https://status.kernel.sh for updates.") | ||
| return nil | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| if resp.StatusCode < 200 || resp.StatusCode >= 300 { | ||
| pterm.Error.Println("Could not reach Kernel API. Check https://status.kernel.sh for updates.") | ||
| return nil | ||
| } | ||
|
|
||
| var status statusResponse | ||
| if err := json.NewDecoder(resp.Body).Decode(&status); err != nil { | ||
| return fmt.Errorf("invalid response: %w", err) | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| if output == "json" { | ||
| enc := json.NewEncoder(os.Stdout) | ||
| enc.SetIndent("", " ") | ||
| return enc.Encode(status) | ||
| } | ||
|
|
||
| printStatus(status) | ||
| return nil | ||
| } | ||
|
|
||
| // Colors match the dashboard's api-status-indicator.tsx | ||
| var statusDisplay = map[string]struct { | ||
| label string | ||
| rgb pterm.RGB | ||
| }{ | ||
| "operational": {label: "Operational", rgb: pterm.NewRGB(31, 163, 130)}, | ||
| "degraded_performance": {label: "Degraded Performance", rgb: pterm.NewRGB(245, 158, 11)}, | ||
| "partial_outage": {label: "Partial Outage", rgb: pterm.NewRGB(242, 85, 51)}, | ||
| "full_outage": {label: "Major Outage", rgb: pterm.NewRGB(239, 68, 68)}, | ||
| "maintenance": {label: "Maintenance", rgb: pterm.NewRGB(36, 99, 235)}, | ||
| "unknown": {label: "Unknown", rgb: pterm.NewRGB(128, 128, 128)}, | ||
| } | ||
|
|
||
| func getStatusDisplay(status string) (string, pterm.RGB) { | ||
| if d, ok := statusDisplay[status]; ok { | ||
| return d.label, d.rgb | ||
| } | ||
| return "Unknown", pterm.NewRGB(128, 128, 128) | ||
| } | ||
|
|
||
| func coloredDot(rgb pterm.RGB) string { | ||
| return rgb.Sprint("●") | ||
| } | ||
|
|
||
| func printStatus(resp statusResponse) { | ||
| label, rgb := getStatusDisplay(resp.Status) | ||
| header := fmt.Sprintf("Kernel Status: %s", rgb.Sprint(label)) | ||
| pterm.Println() | ||
| pterm.Println(" " + header) | ||
|
|
||
| for _, group := range resp.Groups { | ||
| pterm.Println() | ||
| if len(group.Components) == 0 { | ||
| groupLabel, groupColor := getStatusDisplay(group.Status) | ||
| pterm.Printf(" %s %s %s\n", coloredDot(groupColor), pterm.Bold.Sprint(group.Name), groupLabel) | ||
| } else { | ||
| pterm.Println(" " + pterm.Bold.Sprint(group.Name)) | ||
| for _, comp := range group.Components { | ||
| compLabel, compColor := getStatusDisplay(comp.Status) | ||
| pterm.Printf(" %s %-20s %s\n", coloredDot(compColor), comp.Name, compLabel) | ||
| } | ||
| } | ||
| } | ||
| pterm.Println() | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.