Skip to content

Commit

Permalink
fix(lease/shell): command with multiple arguments/flag to be properly…
Browse files Browse the repository at this point in the history
… processed by cobra (#219)

if lease shell command contains flags, cobra will treat them as own
flags and mostly likely will fail. enclosing lease command into double
brackets prevents it. in command itself it will be properly tokenized
and honor inner double quotes if any

Signed-off-by: Artur Troian <[email protected]>
  • Loading branch information
troian authored Mar 21, 2024
1 parent 1eb358b commit 638994e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
23 changes: 17 additions & 6 deletions cmd/provider-services/cmd/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"sync"
"syscall"

"github.com/go-andiamo/splitter"
dockerterm "github.com/moby/term"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -66,16 +67,14 @@ func LeaseShellCmd() *cobra.Command {
}

func doLeaseShell(cmd *cobra.Command, args []string) error {
var stdin io.ReadCloser
var stdout io.Writer
var stderr io.Writer
stdout = os.Stdout
stderr = os.Stderr
var stdin io.Reader
stdout := cmd.OutOrStdout()
stderr := cmd.ErrOrStderr()
connectStdin := viper.GetBool(FlagStdin)
setupTty := viper.GetBool(FlagTty)
podIndex := viper.GetUint(FlagReplicaIndex)
if connectStdin || setupTty {
stdin = os.Stdin
stdin = cmd.InOrStdin()
}

var tty term.TTY
Expand Down Expand Up @@ -138,6 +137,18 @@ func doLeaseShell(cmd *cobra.Command, args []string) error {
service := args[0]
remoteCmd := args[1:]

if len(remoteCmd) == 1 {
spaceSplitter, err := splitter.NewSplitter(' ', splitter.DoubleQuotes)
if err != nil {
return err
}

remoteCmd, err = spaceSplitter.Split(remoteCmd[0])
if err != nil {
return err
}
}

var terminalResizes chan remotecommand.TerminalSize
wg := &sync.WaitGroup{}
ctx, cancel := context.WithCancel(ctx)
Expand Down
2 changes: 1 addition & 1 deletion gateway/rest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type Client interface {
LeaseLogs(ctx context.Context, id mtypes.LeaseID, services string, follow bool, tailLines int64) (*ServiceLogs, error)
ServiceStatus(ctx context.Context, id mtypes.LeaseID, service string) (*cltypes.ServiceStatus, error)
LeaseShell(ctx context.Context, id mtypes.LeaseID, service string, podIndex uint, cmd []string,
stdin io.ReadCloser,
stdin io.Reader,
stdout io.Writer,
stderr io.Writer,
tty bool,
Expand Down
9 changes: 1 addition & 8 deletions gateway/rest/client_shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var (
)

func (c *client) LeaseShell(ctx context.Context, lID mtypes.LeaseID, service string, podIndex uint, cmd []string,
stdin io.ReadCloser,
stdin io.Reader,
stdout io.Writer,
stderr io.Writer,
tty bool,
Expand Down Expand Up @@ -155,13 +155,6 @@ loop:

subcancel()

if stdin != nil {
err := stdin.Close()
if err != nil {
saveError("closing stdin", err)
}
}

// Check to see if the remote end returned an error
if remoteErrorData != nil {
if err = processRemoteError(remoteErrorData); err != nil {
Expand Down

0 comments on commit 638994e

Please sign in to comment.