Skip to content

Commit

Permalink
Add CLI option to override shell used
Browse files Browse the repository at this point in the history
As noted in #117, we want to override the shell that's used.

To do so, we can continue using the default of `bash`, but allow
overriding it with the `--shell` flag.

We need to disable the gosec finding for the new shell argument:

  G204: Subprocess launched with a potential tainted input or cmd arguments (gosec)

As we're comfortable with the risk, as it runs on the user's machine.

Closes #117.
  • Loading branch information
jamietanna committed Oct 20, 2023
1 parent edb336c commit beb0dde
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
8 changes: 8 additions & 0 deletions demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ const (

// FlagSkipSteps is the flag for skipping n amount of steps.
FlagSkipSteps = "skip-steps"

// FlagShell is the flag for defining the shell that is used to execute the command(s).
FlagShell = "shell"
)

// New creates a new Demo instance.
Expand Down Expand Up @@ -90,6 +93,11 @@ func New() *Demo {
Aliases: []string{"s"},
Usage: "skip the amount of initial steps within the demo",
},
&cli.StringFlag{
Name: FlagShell,
Usage: "define the shell that is used to execute the command(s)",
DefaultText: "bash",
},
}

emptyFn := func(*cli.Context) error { return nil }
Expand Down
12 changes: 7 additions & 5 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ import (
"github.com/urfave/cli/v2"
)

// Every command will be executed in a new bash subshell. This is the only
// external dependency we need.
const bash = "bash"

// errOutputNil is the error returned if no output has been set.
var errOutputNil = errors.New("provided output is nil")

Expand Down Expand Up @@ -47,6 +43,7 @@ type Options struct {
HideDescriptions bool
Immediate bool
SkipSteps int
Shell string
}

func emptyFn() error { return nil }
Expand All @@ -72,6 +69,7 @@ func optionsFrom(ctx *cli.Context) Options {
HideDescriptions: ctx.Bool(FlagHideDescriptions),
Immediate: ctx.Bool(FlagImmediate),
SkipSteps: ctx.Int(FlagSkipSteps),
Shell: ctx.String(FlagShell),
}
}

Expand Down Expand Up @@ -117,6 +115,10 @@ func (r *Run) Run(ctx *cli.Context) error {

// RunWithOptions executes the run with the provided Options.
func (r *Run) RunWithOptions(opts Options) error {
if opts.Shell == "" {
opts.Shell = "bash"
}

if err := r.setup(); err != nil {
return err
}
Expand Down Expand Up @@ -207,7 +209,7 @@ func (s *step) echo(current, max int) {

func (s *step) execute() error {
joinedCommand := strings.Join(s.command, " ")
cmd := exec.Command(bash, "-c", joinedCommand)
cmd := exec.Command(s.r.options.Shell, "-c", joinedCommand) //nolint:gosec we're purposefully running user-provided code

cmd.Stderr = s.r.out
cmd.Stdout = s.r.out
Expand Down

0 comments on commit beb0dde

Please sign in to comment.