Skip to content

Commit

Permalink
Support options to set shell and the additoinal shell options
Browse files Browse the repository at this point in the history
  • Loading branch information
sachaos committed Aug 23, 2021
1 parent 77f7d29 commit 31341e6
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 13 deletions.
7 changes: 7 additions & 0 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ type Arguments struct {
isHelp bool
isVersion bool

shell string
shellOpts string

cmd string
args []string
}
Expand All @@ -42,6 +45,8 @@ func parseArguments(args []string) (*Arguments, error) {
flagSet.BoolVarP(&argument.isNoTitle, "no-title", "t", false, "turn off header")
flagSet.BoolVarP(&argument.isHelp, "help", "h", false, "display this help and exit")
flagSet.BoolVarP(&argument.isVersion, "version", "v", false, "output version information and exit")
flagSet.StringVar(&argument.shell, "shell", "sh", "shell (default \"sh\")")
flagSet.StringVar(&argument.shellOpts, "shell-options", "", "additional shell options")

flagSet.SetInterspersed(false)

Expand Down Expand Up @@ -88,6 +93,8 @@ Options:
-p, --precise attempt run command in precise intervals
-c, --clockwork run command in precise intervals forcibly
-t, --no-title turn off header
--shell shell (default "sh")
--shell-options additional shell options
-h, --help display this help and exit
-v, --version output version information and exit`)
Expand Down
14 changes: 8 additions & 6 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package main

import "time"

func ClockSnapshot(begin int64, name string, args []string, interval time.Duration) <-chan *Snapshot {
type newSnapFunc func(int64, *Snapshot, chan<-struct{}) *Snapshot

func ClockSnapshot(begin int64, newSnap newSnapFunc, interval time.Duration) <-chan *Snapshot {
c := make(chan *Snapshot)

go func() {
Expand All @@ -12,15 +14,15 @@ func ClockSnapshot(begin int64, name string, args []string, interval time.Durati
for now := range t {
finish := make(chan struct{})
id := (now.UnixNano() - begin) / int64(time.Millisecond)
s = NewSnapshot(id, name, args, s, finish)
s = newSnap(id, s, finish)
c <- s
}
}()

return c
}

func PreciseSnapshot(begin int64, name string, args []string, interval time.Duration) <-chan *Snapshot {
func PreciseSnapshot(begin int64, newSnap newSnapFunc, interval time.Duration) <-chan *Snapshot {
c := make(chan *Snapshot)

go func() {
Expand All @@ -31,7 +33,7 @@ func PreciseSnapshot(begin int64, name string, args []string, interval time.Dura
finish := make(chan struct{})
start := time.Now()
id := (start.UnixNano() - begin) / int64(time.Millisecond)
ns := NewSnapshot(id, name, args, s, finish)
ns := newSnap(id, s, finish)
s = ns
c <- ns
<-finish
Expand All @@ -48,7 +50,7 @@ func PreciseSnapshot(begin int64, name string, args []string, interval time.Dura
return c
}

func SequentialSnapshot(begin int64, name string, args []string, interval time.Duration) <-chan *Snapshot {
func SequentialSnapshot(begin int64, newSnap newSnapFunc, interval time.Duration) <-chan *Snapshot {
c := make(chan *Snapshot)

go func() {
Expand All @@ -58,7 +60,7 @@ func SequentialSnapshot(begin int64, name string, args []string, interval time.D
for {
finish := make(chan struct{})
id := (time.Now().UnixNano() - begin) / int64(time.Millisecond)
s = NewSnapshot(id, name, args, s, finish)
s = newSnap(id, s, finish)
c <- s
<-finish

Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func main() {
mode = ViddyIntervalModeSequential
}

v := NewViddy(arguments.interval, arguments.cmd, arguments.args, mode)
v := NewViddy(arguments.interval, arguments.cmd, arguments.args, arguments.shell, arguments.shellOpts, mode)
v.isDebug = arguments.isDebug
v.isNoTitle = arguments.isNoTitle
v.isShowDiff = arguments.isDiff
Expand Down
16 changes: 14 additions & 2 deletions snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ type Snapshot struct {
command string
args []string

shell string
shellOpts string

result []byte
start time.Time
end time.Time
Expand All @@ -43,12 +46,15 @@ type Snapshot struct {
finish chan<- struct{}
}

func NewSnapshot(id int64, command string, args []string, before *Snapshot, finish chan<- struct{}) *Snapshot {
func NewSnapshot(id int64, command string, args []string, shell string, shellOpts string, before *Snapshot, finish chan<- struct{}) *Snapshot {
return &Snapshot{
id: id,
command: command,
args: args,

shell: shell,
shellOpts: shellOpts,

before: before,
finish: finish,
}
Expand Down Expand Up @@ -99,7 +105,13 @@ func (s *Snapshot) run(finishedQueue chan<- int64) error {
if runtime.GOOS == "windows" {
command = exec.Command(os.Getenv("COMSPEC"), "/c", strings.Join(commands, " "))
} else {
command = exec.Command("sh", "-c", strings.Join(commands, " "))
var args []string
for _, o := range strings.Fields(s.shellOpts) {
args = append(args, o)
}
args = append(args, "-c")
args = append(args, strings.Join(commands, " "))
command = exec.Command(s.shell, args...)
}
command.Stdout = &b

Expand Down
12 changes: 8 additions & 4 deletions viddy.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,21 @@ var (
ViddyIntervalModeSequential ViddyIntervalMode = "sequential"
)

func NewViddy(duration time.Duration, cmd string, args []string, mode ViddyIntervalMode) *Viddy {
func NewViddy(duration time.Duration, cmd string, args []string, shell string, shellOpts string, mode ViddyIntervalMode) *Viddy {
begin := time.Now().UnixNano()

newSnap := func(id int64, before *Snapshot, finish chan<-struct{}) *Snapshot {
return NewSnapshot(id, cmd, args, shell, shellOpts, before, finish)
}

var snapshotQueue <-chan *Snapshot
switch mode {
case ViddyIntervalModeClockwork:
snapshotQueue = ClockSnapshot(begin, cmd, args, duration)
snapshotQueue = ClockSnapshot(begin, newSnap, duration)
case ViddyIntervalModeSequential:
snapshotQueue = SequentialSnapshot(begin, cmd, args, duration)
snapshotQueue = SequentialSnapshot(begin, newSnap, duration)
case ViddyIntervalModePrecise:
snapshotQueue = PreciseSnapshot(begin, cmd, args, duration)
snapshotQueue = PreciseSnapshot(begin, newSnap, duration)
}

return &Viddy{
Expand Down

0 comments on commit 31341e6

Please sign in to comment.