diff --git a/cmd/shell.go b/cmd/shell.go index 8f027b26..429f03cd 100644 --- a/cmd/shell.go +++ b/cmd/shell.go @@ -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 @@ -108,6 +113,8 @@ func shellRequestFromSelect() (*pkg.ShellRequest, error) { OrganizationID: orga.ID, EnvironmentID: env.ID, ClusterID: env.ClusterID, + PodName: podName, + Command: command, }, nil } @@ -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 } @@ -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) } diff --git a/go.mod b/go.mod index 6ed7eae0..b093fe04 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index db006176..8006359f 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/pkg/shell.go b/pkg/shell.go index 4134bbfe..bd1d48c5 100644 --- a/pkg/shell.go +++ b/pkg/shell.go @@ -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" @@ -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) { @@ -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