Skip to content

Commit

Permalink
Refactoring interval argument
Browse files Browse the repository at this point in the history
  • Loading branch information
sachaos committed Jun 25, 2022
1 parent 66ad7aa commit c8cade6
Showing 1 changed file with 9 additions and 19 deletions.
28 changes: 9 additions & 19 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,13 @@ type keymapping struct {
func newConfig(v *viper.Viper, args []string) (*config, error) {
flagSet := pflag.NewFlagSet("", pflag.ExitOnError)

defaultInterval := os.Getenv("WATCH_INTERVAL")
if defaultInterval == "" {
defaultInterval = "2s"
}

// runtimeConfig
flagSet.StringP("interval", "n", "2s", "seconds to wait between updates")
flagSet.StringP("interval", "n", defaultInterval, "seconds to wait between updates")
flagSet.BoolP("precise", "p", false, "attempt run command in precise intervals")
flagSet.BoolP("clockwork", "c", false, "run command in precise intervals forcibly")
flagSet.BoolP("help", "h", false, "display this help and exit")
Expand All @@ -97,13 +102,9 @@ func newConfig(v *viper.Viper, args []string) (*config, error) {

var conf config

intervalStr, _ := flagSet.GetString("interval")
isIntervalFlagSet := isFlagSet("interval", flagSet)
if !isIntervalFlagSet {
if value, ok := os.LookupEnv("WATCH_INTERVAL"); ok {
fmt.Println("ok")
intervalStr = fmt.Sprintf("%ss", value)
}
intervalStr, err := flagSet.GetString("interval")
if err != nil {
return nil, err
}

interval, err := parseInterval(intervalStr)
Expand Down Expand Up @@ -367,14 +368,3 @@ func keyOf(key string) (tcell.Key, error) {

return 0, keyNotFoundError{}
}

func isFlagSet(str string, flagSet *pflag.FlagSet) bool {
res := false
flagSet.Visit(func (f *pflag.Flag) {
if f.Name == str {
res = true
}
})

return res
}

0 comments on commit c8cade6

Please sign in to comment.