From d1092e260f75fda7aa3c98ba87a35e9b79644d87 Mon Sep 17 00:00:00 2001 From: Jamie Tanna Date: Mon, 25 Sep 2023 20:43:53 +0100 Subject: [PATCH] Add CLI option to override shell used 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. Closes #117. --- demo.go | 8 ++++++++ run.go | 12 +++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/demo.go b/demo.go index 25b8eb6..532f257 100644 --- a/demo.go +++ b/demo.go @@ -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. @@ -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 } diff --git a/run.go b/run.go index a020a79..371d709 100644 --- a/run.go +++ b/run.go @@ -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") @@ -47,6 +43,7 @@ type Options struct { HideDescriptions bool Immediate bool SkipSteps int + Shell string } func emptyFn() error { return nil } @@ -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), } } @@ -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 } @@ -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) cmd.Stderr = s.r.out cmd.Stdout = s.r.out