-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c04d4f
commit 5e9ae58
Showing
7 changed files
with
146 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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,16 @@ | ||
package app | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"github.com/steveiliop56/runtipi-cli-go/cmd/app/start" | ||
) | ||
|
||
func AppCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "app", | ||
Short: "App commands", | ||
Long: "Control your Runtipi apps through the CLI", | ||
} | ||
cmd.AddCommand(start.StartAppCmd) | ||
return cmd | ||
} |
This file contains 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,39 @@ | ||
package start | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/steveiliop56/runtipi-cli-go/internal/api" | ||
"github.com/steveiliop56/runtipi-cli-go/internal/spinner" | ||
) | ||
|
||
var StartAppCmd = &cobra.Command{ | ||
Use: "start [app]", | ||
Short: "Start an app using the Runtipi API", | ||
Long: "This command starts 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/start", args[0]) | ||
|
||
// Start Spinner | ||
spinner.SetMessage("Starting app") | ||
spinner.Start() | ||
|
||
// Start app | ||
err := api.ApiRequest(path, "POST") | ||
|
||
if err != nil { | ||
spinner.Fail("Failed to start app") | ||
spinner.Stop() | ||
fmt.Printf("Error: %s\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
// Succeed | ||
spinner.Succeed("App started succeessfully") | ||
spinner.Stop() | ||
}, | ||
} |
This file contains 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 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 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 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,69 @@ | ||
package api | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/golang-jwt/jwt/v5" | ||
"github.com/steveiliop56/runtipi-cli-go/internal/env" | ||
) | ||
|
||
func GenerateJWT() (string, error) { | ||
secret, envErr := env.GetEnvValue("JWT_SECRET") | ||
|
||
if envErr != nil { | ||
return "", envErr | ||
} | ||
|
||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ | ||
"skill": "issue", | ||
}) | ||
|
||
tokenString, tokenErr := token.SignedString([]byte(secret)) | ||
|
||
if tokenErr != nil { | ||
return "", tokenErr | ||
} | ||
|
||
return tokenString, nil | ||
} | ||
|
||
func ApiRequest(path string, method string) (error) { | ||
token, tokenErr := GenerateJWT() | ||
|
||
if tokenErr != nil { | ||
return tokenErr | ||
} | ||
|
||
port, portErr := env.GetEnvValue("NGINX_PORT") | ||
|
||
if portErr != nil { | ||
return 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 | ||
} | ||
|
||
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token)) | ||
|
||
client := &http.Client{ | ||
Timeout: 5 * time.Minute, | ||
} | ||
|
||
response, clientErr := client.Do(request) | ||
|
||
if clientErr != nil { | ||
return clientErr | ||
} | ||
|
||
defer response.Body.Close() | ||
|
||
return nil | ||
} |
This file contains 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