Skip to content

Commit

Permalink
feat: add system commands
Browse files Browse the repository at this point in the history
  • Loading branch information
steveiliop56 committed Sep 10, 2024
1 parent cf6ac7a commit 648a331
Show file tree
Hide file tree
Showing 15 changed files with 188 additions and 27 deletions.
4 changes: 3 additions & 1 deletion cmd/app/subcommands/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var ResetAppCmd = &cobra.Command{
spinner.Start()

// Reset app
err := api.ApiRequest(path, "POST", 5 * time.Minute)
response, err := api.ApiRequest(path, "POST", 5 * time.Minute)

if err != nil {
spinner.Fail("Failed to reset app")
Expand All @@ -33,6 +33,8 @@ var ResetAppCmd = &cobra.Command{
os.Exit(1)
}

defer response.Body.Close()

// Succeed
spinner.Succeed("App reset succeessfully")
spinner.Stop()
Expand Down
4 changes: 3 additions & 1 deletion cmd/app/subcommands/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var RestartAppCmd = &cobra.Command{
spinner.Start()

// Restart app
err := api.ApiRequest(path, "POST", 5 * time.Minute)
response, err := api.ApiRequest(path, "POST", 5 * time.Minute)

if err != nil {
spinner.Fail("Failed to restart app")
Expand All @@ -33,6 +33,8 @@ var RestartAppCmd = &cobra.Command{
os.Exit(1)
}

defer response.Body.Close()

// Succeed
spinner.Succeed("App restarted succeessfully")
spinner.Stop()
Expand Down
4 changes: 3 additions & 1 deletion cmd/app/subcommands/start-all.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var StartAllCmd = &cobra.Command{
spinner.Start()

// Start apps
err := api.ApiRequest(path, "POST", 15 * time.Minute)
response, err := api.ApiRequest(path, "POST", 15 * time.Minute)

if err != nil {
spinner.Fail("Failed to start apps")
Expand All @@ -33,6 +33,8 @@ var StartAllCmd = &cobra.Command{
os.Exit(1)
}

defer response.Body.Close()

// Succeed
spinner.Succeed("Apps succeessfully")
spinner.Stop()
Expand Down
4 changes: 3 additions & 1 deletion cmd/app/subcommands/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var StartAppCmd = &cobra.Command{
spinner.Start()

// Start app
err := api.ApiRequest(path, "POST", 5 * time.Minute)
response, err := api.ApiRequest(path, "POST", 5 * time.Minute)

if err != nil {
spinner.Fail("Failed to start app")
Expand All @@ -33,6 +33,8 @@ var StartAppCmd = &cobra.Command{
os.Exit(1)
}

defer response.Body.Close()

// Succeed
spinner.Succeed("App started succeessfully")
spinner.Stop()
Expand Down
4 changes: 3 additions & 1 deletion cmd/app/subcommands/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var StopAppCmd = &cobra.Command{
spinner.Start()

// Stop app
err := api.ApiRequest(path, "POST", 5 * time.Minute)
response, err := api.ApiRequest(path, "POST", 5 * time.Minute)

if err != nil {
spinner.Fail("Failed to stop app")
Expand All @@ -33,6 +33,8 @@ var StopAppCmd = &cobra.Command{
os.Exit(1)
}

defer response.Body.Close()

// Succeed
spinner.Succeed("App stopped succeessfully")
spinner.Stop()
Expand Down
4 changes: 3 additions & 1 deletion cmd/app/subcommands/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var UninstallAppCmd = &cobra.Command{
spinner.Start()

// Uninstall app
err := api.ApiRequest(path, "POST", 5 * time.Minute)
response, err := api.ApiRequest(path, "POST", 5 * time.Minute)

if err != nil {
spinner.Fail("Failed to uninstall app")
Expand All @@ -33,6 +33,8 @@ var UninstallAppCmd = &cobra.Command{
os.Exit(1)
}

defer response.Body.Close()

// Succeed
spinner.Succeed("App uninstalled succeessfully")
spinner.Stop()
Expand Down
4 changes: 3 additions & 1 deletion cmd/app/subcommands/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var UpdateAppCmd = &cobra.Command{
spinner.Start()

// Updating app
err := api.ApiRequest(path, "POST", 15 * time.Minute)
response, err := api.ApiRequest(path, "POST", 15 * time.Minute)

if err != nil {
spinner.Fail("Failed to update app")
Expand All @@ -33,6 +33,8 @@ var UpdateAppCmd = &cobra.Command{
os.Exit(1)
}

defer response.Body.Close()

// Succeed
spinner.Succeed("App updated succeessfully")
spinner.Stop()
Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/spf13/cobra"
"github.com/steveiliop56/runtipi-cli-go/cmd/app"
"github.com/steveiliop56/runtipi-cli-go/cmd/system"
)

var noPermissions bool
Expand All @@ -29,4 +30,5 @@ func Execute() {
func init() {
fmt.Println("Welcome to Runtipi CLI in Go ✨")
rootCmd.AddCommand(app.AppCmd())
rootCmd.AddCommand(system.SystemCmd())
}
41 changes: 41 additions & 0 deletions cmd/system/subcommands/healthcheck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package subcommands

import (
"fmt"
"os"
"time"

"github.com/spf13/cobra"
"github.com/steveiliop56/runtipi-cli-go/internal/api"
"github.com/steveiliop56/runtipi-cli-go/internal/spinner"
)

var HealthCheckCmd = &cobra.Command{
Use: "healthcheck",
Short: "Checks if the Runtipi system is up and running use the API",
Long: "Checks if the Runtipi system is up and running using the worker API",
Run: func(cmd *cobra.Command, args []string) {
// Define Path
path := "healthcheck"

// Start Spinner
spinner.SetMessage("Checking system")
spinner.Start()

// Do Check
response, err := api.ApiRequest(path, "GET", 1 * time.Minute)

if err != nil {
spinner.Fail("Failed to check system, is runtipi running?")
spinner.Stop()
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}

defer response.Body.Close()

// Succeed
spinner.Succeed("Runtipi system up and running")
spinner.Stop()
},
}
62 changes: 62 additions & 0 deletions cmd/system/subcommands/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package subcommands

import (
"encoding/json"
"fmt"
"os"
"time"

"github.com/spf13/cobra"
"github.com/steveiliop56/runtipi-cli-go/internal/api"
"github.com/steveiliop56/runtipi-cli-go/internal/constants"
"github.com/steveiliop56/runtipi-cli-go/internal/schemas"
"github.com/steveiliop56/runtipi-cli-go/internal/spinner"
)

var StatusCmd = &cobra.Command{
Use: "status",
Short: "Gets system readings from the Runtipi API",
Long: "Shows the system readings (e.g. cpu usage, disk usage) from the Runtipi worker API",
Run: func(cmd *cobra.Command, args []string) {
// Define Path
path := "system-status"

// Start Spinner
spinner.SetMessage("Getting system status")
spinner.Start()

// Do Check
response, err := api.ApiRequest(path, "GET", 1 * time.Minute)

if err != nil {
spinner.Fail("Failed to get system status, is runtipi running?")
spinner.Stop()
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}

// Parse Json

status := new(schemas.SystemStatusApi)

jsonErr := json.NewDecoder(response.Body).Decode(&status)

if jsonErr != nil {
spinner.Fail("Failed to decode system status json")
spinner.Stop()
fmt.Printf("Error: %s\n", jsonErr)
os.Exit(1)
}

defer response.Body.Close()

// Succeed
spinner.Succeed("Successfully got system status")
spinner.Stop()

// Print status
fmt.Printf("Your CPU usage is %s %% \n", constants.Blue(fmt.Sprintf("%.2f", status.Data.CpuLoad)))
fmt.Printf("Your Disk size is %s GB, you are using %s GB which is %s %% \n", constants.Blue(status.Data.DiskSize), constants.Blue(status.Data.DiskUsed), constants.Blue(status.Data.PercentUsed))
fmt.Printf("Your Memory size is %s GB and you are using %s %% \n", constants.Blue(status.Data.MemoryTotal), constants.Blue(status.Data.PercentUsedMemory))
},
}
17 changes: 17 additions & 0 deletions cmd/system/system.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package system

import (
"github.com/spf13/cobra"
"github.com/steveiliop56/runtipi-cli-go/cmd/system/subcommands"
)

func SystemCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "system",
Short: "System commands",
Long: "Control your Runtipi system through the CLI",
}
cmd.AddCommand(subcommands.HealthCheckCmd)
cmd.AddCommand(subcommands.StatusCmd)
return cmd
}
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ var versionCmd = &cobra.Command{
Long: "This command prints the current Runtipi CLI version",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("Current Runtipi version: %s\n", constants.Blue(constants.RuntipiVersion))
fmt.Print("Current CLI version: %s\n", constants.Blue(constants.CliVersion))
fmt.Printf("Current CLI version: %s\n", constants.Blue(constants.CliVersion))
},
}
16 changes: 7 additions & 9 deletions internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,25 @@ func GenerateJWT() (string, error) {
return tokenString, nil
}

func ApiRequest(path string, method string, timeout time.Duration) (error) {
func ApiRequest(path string, method string, timeout time.Duration) (*http.Response, error) {
token, tokenErr := GenerateJWT()

if tokenErr != nil {
return tokenErr
return nil, tokenErr
}

port, portErr := env.GetEnvValue("NGINX_PORT")

if portErr != nil {
return portErr
return nil, portErr
}

apiUrl := fmt.Sprintf("http://localhost:%s/worker-api/%s", port, path)

request, requestErr := http.NewRequest(method, apiUrl, bytes.NewBuffer([]byte("")))

if requestErr != nil {
return requestErr
return nil, requestErr
}

request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
Expand All @@ -60,10 +60,8 @@ func ApiRequest(path string, method string, timeout time.Duration) (error) {
response, clientErr := client.Do(request)

if clientErr != nil {
return clientErr
return nil, clientErr
}

defer response.Body.Close()

return nil
}
return response, nil
}
10 changes: 3 additions & 7 deletions internal/release/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@ import (
"strconv"
"strings"

"github.com/steveiliop56/runtipi-cli-go/internal/schemas"
"github.com/steveiliop56/runtipi-cli-go/internal/system"
)

type GithubRelease struct {
TagName string `json:"tag_name"`
Status string `json:"status"`
}

func IsMajorBump(newVersion string, currentVersion string) (bool, error) {
newVersionMajor := strings.Split(strings.Replace(newVersion, "v", "", 1), ".")[0]
currentVersionMajor := strings.Split(strings.Replace(currentVersion, "v", "", 1), ".")[0]
Expand Down Expand Up @@ -53,7 +49,7 @@ func GetLatestVersion() (string, error) {

defer response.Body.Close()

release := new(GithubRelease)
release := new(schemas.GithubRelease)

jsonErr := json.NewDecoder(response.Body).Decode(&release)

Expand All @@ -75,7 +71,7 @@ func ValidateVersion(version string) (bool, error) {

defer response.Body.Close()

release := new(GithubRelease)
release := new(schemas.GithubRelease)

jsonErr := json.NewDecoder(response.Body).Decode(&release)

Expand Down
Loading

0 comments on commit 648a331

Please sign in to comment.