-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathslow.go
42 lines (36 loc) · 938 Bytes
/
slow.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package slow
import (
"context"
"fmt"
"io"
"log"
"time"
"github.com/DavidGamba/go-getoptions"
)
var Logger = log.New(io.Discard, "show ", log.LstdFlags)
var iterations int
// NewCommand - Populate Options definition
func NewCommand(parent *getoptions.GetOpt) *getoptions.GetOpt {
opt := parent.NewCommand("slow", "Run something in a very slow way (please cancel me with Ctrl-C)")
opt.IntVar(&iterations, "iterations", 5)
opt.SetCommandFn(Run)
return opt
}
// Run - Command entry point
func Run(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
Logger.Printf("args to slow: %v\n", args)
if opt.Called("iterations") {
fmt.Printf("iterations overriden with: %d\n", opt.Value("iterations"))
}
for i := 0; i < iterations; i++ {
select {
case <-ctx.Done():
fmt.Println("Cleaning up...")
return nil
default:
}
fmt.Printf("Sleeping: %d\n", i)
time.Sleep(1 * time.Second)
}
return nil
}