Skip to content

Commit

Permalink
feat: add app start command (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
steveiliop56 committed Sep 9, 2024
1 parent 5c04d4f commit 5e9ae58
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 8 deletions.
16 changes: 16 additions & 0 deletions cmd/app/app.go
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
}
39 changes: 39 additions & 0 deletions cmd/app/start/start.go
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()
},
}
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

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

var noPermissions bool
Expand All @@ -27,4 +28,5 @@ func Execute() {

func init() {
fmt.Println("Welcome to Runtipi CLI in Go ✨")
rootCmd.AddCommand(app.AppCmd())
}
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (

require (
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
Expand All @@ -21,6 +22,7 @@ require (
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
Expand All @@ -29,12 +31,13 @@ require (
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.step.sm/crypto v0.51.2 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/term v0.1.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/text v0.17.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHk
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
Expand Down Expand Up @@ -48,6 +50,8 @@ github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyua
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down Expand Up @@ -84,6 +88,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
go.step.sm/crypto v0.51.2 h1:5EiCGIMg7IvQTGmJrwRosbXeprtT80OhoS/PJarg60o=
go.step.sm/crypto v0.51.2/go.mod h1:QK7czLjN2k+uqVp5CHXxJbhc70kVRSP+0CQF3zsR5M0=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
Expand All @@ -99,10 +105,14 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.1.0 h1:g6Z6vPFA9dYBAF7DWcH6sCcOntplXsDKcliusYijMlw=
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
Expand Down
69 changes: 69 additions & 0 deletions internal/api/api.go
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
}
11 changes: 5 additions & 6 deletions internal/seed/seed.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package seed

import (
"crypto/rand"
"encoding/base64"
"errors"
"os"
"path"

"go.step.sm/crypto/randutil"
)

func GenerateSeed(rootFolder string) (error) {
if _, err := os.Stat(path.Join(rootFolder, "state", "seed")); errors.Is(err, os.ErrNotExist) {
b := make([]byte, 32)
if _, err := rand.Read(b); err != nil {
return err
seed, seedErr := randutil.Alphanumeric(32)
if seedErr != nil {
return seedErr
}
seed := base64.URLEncoding.EncodeToString(b)[:32]
writeErr := os.WriteFile(path.Join(rootFolder, "state", "seed"), []byte(seed), 0644)
if writeErr != nil {
return writeErr
Expand Down

0 comments on commit 5e9ae58

Please sign in to comment.