Skip to content

Commit

Permalink
Add --no-shell option
Browse files Browse the repository at this point in the history
  • Loading branch information
sachaos committed Oct 15, 2023
1 parent 8641b5e commit 2293a23
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -134,6 +134,7 @@ On macOS, the path is `~/Library/Application\ Support/viddy.toml`.

```toml
[general]
no_shell = false
shell = "zsh"
shell_options = ""

Expand Down
7 changes: 7 additions & 0 deletions config.go
Expand Up @@ -38,6 +38,7 @@ type runtimeConfig struct {
}

type general struct {
noShell bool
shell string
shellOptions string
debug bool
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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")
Expand Down
27 changes: 16 additions & 11 deletions snapshot.go
Expand Up @@ -24,6 +24,7 @@ type Snapshot struct {
command string
args []string

noShell bool
shell string
shellOpts string

Expand All @@ -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,

Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 10 additions & 1 deletion viddy.go
Expand Up @@ -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 (
Expand Down

0 comments on commit 2293a23

Please sign in to comment.