Skip to content

Commit

Permalink
Add -timeout option (#37)
Browse files Browse the repository at this point in the history
`-timeout DUR` simply exist `gitmux` is it still running after DUR
amount of time.
DUR is parsed as a time.Duration with the special exception that 0
means no timeout.
Defaults to 0 = _no timeout_

Closes #35
  • Loading branch information
arl authored Sep 1, 2020
1 parent 247481f commit 845f416
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions gitmux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"os"
"time"

"github.com/arl/gitstatus"
"gopkg.in/yaml.v2"
Expand All @@ -24,6 +25,7 @@ Options:
-cfg cfgfile use cfgfile when printing git status.
-printcfg prints default configuration file.
-dbg outputs Git status as JSON and print errors.
-timeout DUR exits if still running after given duration (ex: 2s, 500ms).
-V prints gitmux version and exits.
`

Expand All @@ -32,11 +34,35 @@ type Config struct{ Tmux tmux.Config }

var _defaultCfg = Config{Tmux: tmux.DefaultCfg}

// duration is time.Duration usable as command line flag.
type duration time.Duration

func (d duration) String() string {
if d == 0 {
return "none"
}

return time.Duration(d).String()
}

func (d *duration) Set(s string) error {
dur, err := time.ParseDuration(s)
if err != nil {
return err
}

*d = duration(dur)
return nil
}

func parseOptions() (dir string, dbg bool, cfg Config) {
dbgOpt := flag.Bool("dbg", false, "")
cfgOpt := flag.String("cfg", "", "")
printCfgOpt := flag.Bool("printcfg", false, "")
versionOpt := flag.Bool("V", false, "")
timeout := duration(0)
flag.Var(&timeout, "timeout", "")

flag.Bool("q", true, "") // unused, kept for retro-compatibility.
flag.String("fmt", "", "") // unused, kept for retro-compatibility.
flag.Usage = func() {
Expand Down Expand Up @@ -71,6 +97,11 @@ func parseOptions() (dir string, dbg bool, cfg Config) {
check(dec.Decode(&cfg), *dbgOpt)
}

if timeout != 0 {
// Exit after the given amount of time
time.AfterFunc(time.Duration(timeout), func() { os.Exit(1) })
}

return dir, *dbgOpt, cfg
}

Expand Down

0 comments on commit 845f416

Please sign in to comment.