mirror of
https://github.com/arl/gitmux.git
synced 2026-09-10 07:26:18 -04:00
Add -timeout option (#37)
`-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
This commit is contained in:
parent
247481f189
commit
845f4165fe
31
gitmux.go
31
gitmux.go
|
|
@ -4,6 +4,7 @@ import (
|
|||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/arl/gitstatus"
|
||||
"gopkg.in/yaml.v2"
|
||||
|
|
@ -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.
|
||||
`
|
||||
|
||||
|
|
@ -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() {
|
||||
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue