diff --git a/README.md b/README.md index 9500e29..fcacccd 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,7 @@ On macOS, the path is `~/Library/Application\ Support/viddy.toml`. ```toml [general] +no_shell = false shell = "zsh" shell_options = "" diff --git a/config.go b/config.go index 5b9775f..2cc0325 100644 --- a/config.go +++ b/config.go @@ -38,6 +38,7 @@ type runtimeConfig struct { } type general struct { + noShell bool shell string shellOptions string debug bool @@ -91,6 +92,7 @@ func newConfig(v *viper.Viper, args []string) (*config, error) { flagSet.BoolP("skip-empty-diffs", "s", false, "skip snapshots with no changes (+0 -0) in history") flagSet.BoolP("no-title", "t", false, "turn off header") flagSet.Bool("debug", false, "") + flagSet.Bool("no-shell", false, "do not use a shell even if --shell is set") flagSet.String("shell", "", "shell (default \"sh\")") flagSet.String("shell-options", "", "additional shell options") flagSet.Bool("unfold", false, "unfold") @@ -132,6 +134,10 @@ func newConfig(v *viper.Viper, args []string) (*config, error) { return nil, err } + if err := v.BindPFlag("general.no_shell", flagSet.Lookup("no-shell")); err != nil { + return nil, err + } + if err := v.BindPFlag("general.shell", flagSet.Lookup("shell")); err != nil { return nil, err } @@ -167,6 +173,7 @@ func newConfig(v *viper.Viper, args []string) (*config, error) { } conf.general.debug = v.GetBool("general.debug") + conf.general.noShell = v.GetBool("general.no_shell") conf.general.shell = v.GetString("general.shell") conf.general.shellOptions = v.GetString("general.shell_options") conf.general.bell, _ = flagSet.GetBool("bell") diff --git a/snapshot.go b/snapshot.go index f645a4d..45438be 100644 --- a/snapshot.go +++ b/snapshot.go @@ -24,6 +24,7 @@ type Snapshot struct { command string args []string + noShell bool shell string shellOpts string @@ -50,12 +51,13 @@ type Snapshot struct { // NewSnapshot returns Snapshot object. // //nolint:lll -func NewSnapshot(id int64, command string, args []string, shell string, shellOpts string, before *Snapshot, finish chan<- struct{}) *Snapshot { +func NewSnapshot(id int64, command string, args []string, noShell bool, shell string, shellOpts string, before *Snapshot, finish chan<- struct{}) *Snapshot { return &Snapshot{ id: id, command: command, args: args, + noShell: noShell, shell: shell, shellOpts: shellOpts, @@ -97,22 +99,25 @@ func (s *Snapshot) compareFromBefore() error { return nil } +//nolint:gosec func (s *Snapshot) prepareCommand(commands []string) *exec.Cmd { - var command *exec.Cmd - if runtime.GOOS == "windows" { cmdStr := strings.Join(commands, " ") compSec := os.Getenv("COMSPEC") - command = exec.Command(compSec, "/c", cmdStr) - } else { - var args []string - args = append(args, strings.Fields(s.shellOpts)...) - args = append(args, "-c") - args = append(args, strings.Join(commands, " ")) - command = exec.Command(s.shell, args...) //nolint:gosec + + return exec.Command(compSec, "/c", cmdStr) } - return command + if s.noShell { + return exec.Command(commands[0], commands[1:]...) + } + + var args []string + args = append(args, strings.Fields(s.shellOpts)...) + args = append(args, "-c") + args = append(args, strings.Join(commands, " ")) + + return exec.Command(s.shell, args...) } func isWhiteString(str string) bool { diff --git a/viddy.go b/viddy.go index 42e063a..f659dbf 100644 --- a/viddy.go +++ b/viddy.go @@ -101,7 +101,16 @@ func NewViddy(conf *config) *Viddy { begin := time.Now().UnixNano() newSnap := func(id int64, before *Snapshot, finish chan<- struct{}) *Snapshot { - return NewSnapshot(id, conf.runtime.cmd, conf.runtime.args, conf.general.shell, conf.general.shellOptions, before, finish) + return NewSnapshot( + id, + conf.runtime.cmd, + conf.runtime.args, + conf.general.noShell, + conf.general.shell, + conf.general.shellOptions, + before, + finish, + ) } var (