Skip to content

Commit

Permalink
feat(shell): Allow to specify command and pod_name
Browse files Browse the repository at this point in the history
  • Loading branch information
erebe committed Dec 19, 2023
1 parent 3308b25 commit f1f841f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 16 deletions.
19 changes: 19 additions & 0 deletions cmd/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ var shellCmd = &cobra.Command{
pkg.ExecShell(shellRequest)
},
}
var (
command []string
podName *string
podNameFlag string
)

func shellRequestWithoutArg() (*pkg.ShellRequest, error) {
useContext := false
Expand Down Expand Up @@ -108,6 +113,8 @@ func shellRequestFromSelect() (*pkg.ShellRequest, error) {
OrganizationID: orga.ID,
EnvironmentID: env.ID,
ClusterID: env.ClusterID,
PodName: podName,
Command: command,
}, nil
}

Expand Down Expand Up @@ -135,6 +142,8 @@ func shellRequestFromContext(currentContext utils.QoveryContext) (*pkg.ShellRequ
OrganizationID: currentContext.OrganizationId,
EnvironmentID: currentContext.EnvironmentId,
ClusterID: utils.Id(e.ClusterId),
PodName: podName,
Command: command,
}, nil
}

Expand Down Expand Up @@ -230,9 +239,19 @@ func shellRequestWithApplicationUrl(args []string) (*pkg.ShellRequest, error) {
EnvironmentID: environment.ID,
ServiceID: service.ID,
ClusterID: environment.ClusterID,
PodName: podName,
Command: command,
}, nil
}

func init() {
var shellCmd = shellCmd
shellCmd.Flags().StringSliceVarP(&command, "command", "c", []string{"sh"}, "command to launch inside the pod")
shellCmd.Flags().StringVarP(&podNameFlag, "pod name", "p", "", "pod where to exec into")

if podNameFlag != "" {
podName = &podNameFlag
}

rootCmd.AddCommand(shellCmd)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ require (
atomicgo.dev/cursor v0.1.1 // indirect
atomicgo.dev/keyboard v0.2.9 // indirect
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/appscode/go-querystring v0.0.0-20170504095604-0126cfb3f1dc // indirect
github.com/cenkalti/backoff/v3 v3.2.2 // indirect
github.com/chzyer/readline v1.5.1 // indirect
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDe
github.com/andybalholm/brotli v1.0.1/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/appscode/go-querystring v0.0.0-20170504095604-0126cfb3f1dc h1:LoL75er+LKDHDUfU5tRvFwxH0LjPpZN8OoG8Ll+liGU=
github.com/appscode/go-querystring v0.0.0-20170504095604-0126cfb3f1dc/go.mod h1:w648aMHEgFYS6xb0KVMMtZ2uMeemhiKCuD2vj6gY52A=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/atomicgo/cursor v0.0.1/go.mod h1:cBON2QmmrysudxNBFthvMtN32r3jxVRIvzkUiF/RuIk=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
Expand Down
33 changes: 17 additions & 16 deletions pkg/shell.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package pkg

import (
"fmt"
"github.com/appscode/go-querystring/query"
"net/http"
"net/url"
"regexp"

"github.com/containerd/console"
"github.com/gorilla/websocket"
Expand All @@ -14,12 +15,13 @@ import (
const StdinBufferSize = 4096

type ShellRequest struct {
ServiceID utils.Id
ApplicationID utils.Id
EnvironmentID utils.Id
ProjectID utils.Id
OrganizationID utils.Id
ClusterID utils.Id
ServiceID utils.Id `url:"service"`
EnvironmentID utils.Id `url:"environment"`
ProjectID utils.Id `url:"project"`
OrganizationID utils.Id `url:"organization"`
ClusterID utils.Id `url:"cluster"`
PodName *string `url:"pod_name,omitempty"`
Command []string `url:"command"`
}

func ExecShell(req *ShellRequest) {
Expand Down Expand Up @@ -58,19 +60,18 @@ func ExecShell(req *ShellRequest) {
}

func createWebsocketConn(req *ShellRequest) (*websocket.Conn, error) {
wsURL, err := url.Parse(fmt.Sprintf(
"wss://ws.qovery.com/shell/exec?service=%s&application=%s&cluster=%s&environment=%s&organization=%s&project=%s",
req.ServiceID,
req.ApplicationID,
req.ClusterID,
req.EnvironmentID,
req.OrganizationID,
req.ProjectID,
))
command, err := query.Values(req)
if err != nil {
return nil, err
}

wsURL, err := url.Parse("wss://ws.qovery.com/shell/exec")
if err != nil {
return nil, err
}
pattern := regexp.MustCompile("%5B([0-9]+)%5D=")
wsURL.RawQuery = pattern.ReplaceAllString(command.Encode(), "[${1}]=")

tokenType, token, err := utils.GetAccessToken()
if err != nil {
return nil, err
Expand Down

0 comments on commit f1f841f

Please sign in to comment.