From 48dca8f3ee5802fb1a0f1d69f76675520879eaa4 Mon Sep 17 00:00:00 2001 From: Ondrej Sika Date: Sun, 3 Mar 2024 09:05:23 +0100 Subject: [PATCH] feat(cmd/sleep/random): Add --verbose flag --- cmd/sleep/random/random.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/cmd/sleep/random/random.go b/cmd/sleep/random/random.go index d5d9402c..1cece075 100644 --- a/cmd/sleep/random/random.go +++ b/cmd/sleep/random/random.go @@ -10,6 +10,7 @@ import ( var FlagMinTime int var FlagMaxTime int +var FlagVerbose bool var Cmd = &cobra.Command{ Use: "random", @@ -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) }, } @@ -36,4 +42,11 @@ func init() { 1000, // 1s "Maximum sleep time (in ms)", ) + Cmd.Flags().BoolVarP( + &FlagVerbose, + "verbose", + "v", + false, + "verbose output", + ) }