Skip to content

Commit

Permalink
feat(cmd/sleep/random): Add --verbose flag
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejsika committed Mar 3, 2024
1 parent 1f3585d commit 48dca8f
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion cmd/sleep/random/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

var FlagMinTime int
var FlagMaxTime int
var FlagVerbose bool

var Cmd = &cobra.Command{
Use: "random",
Expand All @@ -18,7 +19,12 @@ var Cmd = &cobra.Command{
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
rand.Seed(time.Now().UnixNano())
time.Sleep(time.Duration(rand.Intn(FlagMaxTime-FlagMinTime)+FlagMinTime) * time.Millisecond)
sleepTimeInt := rand.Intn(FlagMaxTime-FlagMinTime) + FlagMinTime
if FlagVerbose {
c.Println("Sleep for", sleepTimeInt, "ms")

}
time.Sleep(time.Duration(sleepTimeInt) * time.Millisecond)
},
}

Expand All @@ -36,4 +42,11 @@ func init() {
1000, // 1s
"Maximum sleep time (in ms)",
)
Cmd.Flags().BoolVarP(
&FlagVerbose,
"verbose",
"v",
false,
"verbose output",
)
}

0 comments on commit 48dca8f

Please sign in to comment.