Skip to content

Commit

Permalink
feat: app api commands
Browse files Browse the repository at this point in the history
  • Loading branch information
steveiliop56 committed Sep 10, 2024
1 parent c0794f7 commit cf6ac7a
Show file tree
Hide file tree
Showing 10 changed files with 278 additions and 6 deletions.
25 changes: 25 additions & 0 deletions cleanup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

# CLI Build
rm -rf runtipi-cli-go
rm -rf runtipi-cli-go.bak

# CLI Created Files
rm -rf apps
rm -rf data
rm -rf app-data
rm -rf state
rm -rf repos
rm -rf media
rm -rf traefik
rm -rf user-config
rm -rf backups
rm -rf logs
rm -rf docker-compose.yml
rm -rf VERSION
rm -rf .env

# Test Files
# /internal/constants/assets/RUNTIPI_VERSION
# /internal/constants/assets/CLI_VERSION
# /.env.local
10 changes: 8 additions & 2 deletions cmd/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package app

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

func AppCmd() *cobra.Command {
Expand All @@ -11,6 +11,12 @@ func AppCmd() *cobra.Command {
Short: "App commands",
Long: "Control your Runtipi apps through the CLI",
}
cmd.AddCommand(start.StartAppCmd)
cmd.AddCommand(subcommands.StartAppCmd)
cmd.AddCommand(subcommands.StopAppCmd)
cmd.AddCommand(subcommands.RestartAppCmd)
cmd.AddCommand(subcommands.ResetAppCmd)
cmd.AddCommand(subcommands.UpdateAppCmd)
cmd.AddCommand(subcommands.UninstallAppCmd)
cmd.AddCommand(subcommands.StartAllCmd)
return cmd
}
40 changes: 40 additions & 0 deletions cmd/app/subcommands/reset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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 ResetAppCmd = &cobra.Command{
Use: "reset [app]",
Short: "Resets an app using the Runtipi API",
Long: "This command resets the specified app using the Runtipi worker API",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
// Define Path
path := fmt.Sprintf("apps/%s/reset", args[0])

// Start Spinner
spinner.SetMessage("Resetting app")
spinner.Start()

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

if err != nil {
spinner.Fail("Failed to reset app")
spinner.Stop()
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}

// Succeed
spinner.Succeed("App reset succeessfully")
spinner.Stop()
},
}
40 changes: 40 additions & 0 deletions cmd/app/subcommands/restart.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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 RestartAppCmd = &cobra.Command{
Use: "restart [app]",
Short: "Restart an app using the Runtipi API",
Long: "This command restarts the specified app using the Runtipi worker API",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
// Define Path
path := fmt.Sprintf("apps/%s/restart", args[0])

// Start Spinner
spinner.SetMessage("Restarting app")
spinner.Start()

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

if err != nil {
spinner.Fail("Failed to restart app")
spinner.Stop()
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}

// Succeed
spinner.Succeed("App restarted succeessfully")
spinner.Stop()
},
}
40 changes: 40 additions & 0 deletions cmd/app/subcommands/start-all.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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 StartAllCmd = &cobra.Command{
Use: "start-all",
Short: "Starts all apps using the Runtipi API",
Long: "This command starts all apps using the Runtipi worker API",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
// Define Path
path := "apps/start-all"

// Start Spinner
spinner.SetMessage("Starting apps")
spinner.Start()

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

if err != nil {
spinner.Fail("Failed to start apps")
spinner.Stop()
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}

// Succeed
spinner.Succeed("Apps succeessfully")
spinner.Stop()
},
}
5 changes: 3 additions & 2 deletions cmd/app/start/start.go → cmd/app/subcommands/start.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package start
package subcommands

import (
"fmt"
"os"
"time"

"github.com/spf13/cobra"
"github.com/steveiliop56/runtipi-cli-go/internal/api"
Expand All @@ -23,7 +24,7 @@ var StartAppCmd = &cobra.Command{
spinner.Start()

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

if err != nil {
spinner.Fail("Failed to start app")
Expand Down
40 changes: 40 additions & 0 deletions cmd/app/subcommands/stop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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 StopAppCmd = &cobra.Command{
Use: "stop [app]",
Short: "Stop an app using the Runtipi API",
Long: "This command stops the specified app using the Runtipi worker API",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
// Define Path
path := fmt.Sprintf("apps/%s/stop", args[0])

// Start Spinner
spinner.SetMessage("Stopping app")
spinner.Start()

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

if err != nil {
spinner.Fail("Failed to stop app")
spinner.Stop()
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}

// Succeed
spinner.Succeed("App stopped succeessfully")
spinner.Stop()
},
}
40 changes: 40 additions & 0 deletions cmd/app/subcommands/uninstall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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 UninstallAppCmd = &cobra.Command{
Use: "uninstall [app]",
Short: "Uninstalls an app using the Runtipi API",
Long: "This command uninstalls the specified app using the Runtipi worker API",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
// Define Path
path := fmt.Sprintf("apps/%s/uninstall", args[0])

// Start Spinner
spinner.SetMessage("Uninstalling app")
spinner.Start()

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

if err != nil {
spinner.Fail("Failed to uninstall app")
spinner.Stop()
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}

// Succeed
spinner.Succeed("App uninstalled succeessfully")
spinner.Stop()
},
}
40 changes: 40 additions & 0 deletions cmd/app/subcommands/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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 UpdateAppCmd = &cobra.Command{
Use: "update [app]",
Short: "Update an app using the Runtipi API",
Long: "This command updates the specified app using the Runtipi worker API",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
// Define Path
path := fmt.Sprintf("apps/%s/update", args[0])

// Start Spinner
spinner.SetMessage("Updating app")
spinner.Start()

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

if err != nil {
spinner.Fail("Failed to update app")
spinner.Stop()
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}

// Succeed
spinner.Succeed("App updated succeessfully")
spinner.Stop()
},
}
4 changes: 2 additions & 2 deletions internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func GenerateJWT() (string, error) {
return tokenString, nil
}

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

if tokenErr != nil {
Expand All @@ -54,7 +54,7 @@ func ApiRequest(path string, method string) (error) {
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))

client := &http.Client{
Timeout: 5 * time.Minute,
Timeout: timeout,
}

response, clientErr := client.Do(request)
Expand Down

0 comments on commit cf6ac7a

Please sign in to comment.