-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cmd/sleep/random_verbose): Add
slu sleep random-verbose
mainly…
… for my Kubernetes training
- Loading branch information
1 parent
05c3394
commit 5e3e809
Showing
3 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package random_verbose | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"time" | ||
|
||
sleep_cmd "github.com/sikalabs/slu/cmd/sleep" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var FlagMinTime int | ||
var FlagMaxTime int | ||
|
||
var Cmd = &cobra.Command{ | ||
Use: "random-verbose", | ||
Short: "Sleep random time with verbose output", | ||
Args: cobra.NoArgs, | ||
Run: func(c *cobra.Command, args []string) { | ||
rand.Seed(time.Now().UnixNano()) | ||
|
||
sleepTimeInSeconds := rand.Intn(FlagMaxTime-FlagMinTime) + FlagMinTime | ||
|
||
fmt.Printf("Sleep %d seconds\n", sleepTimeInSeconds) | ||
for i := 0; i < sleepTimeInSeconds; i++ { | ||
time.Sleep(time.Second) | ||
fmt.Printf("... %d/%d seconds\n", i, sleepTimeInSeconds) | ||
} | ||
fmt.Println("Done.") | ||
}, | ||
} | ||
|
||
func init() { | ||
sleep_cmd.Cmd.AddCommand(Cmd) | ||
Cmd.Flags().IntVar( | ||
&FlagMinTime, | ||
"min", | ||
0, | ||
"Minimum sleep time (in seconds)", | ||
) | ||
Cmd.Flags().IntVar( | ||
&FlagMaxTime, | ||
"max", | ||
10, // 10s | ||
"Maximum sleep time (in seconds)", | ||
) | ||
} |