-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
51 lines (45 loc) · 834 Bytes
/
main.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
43
44
45
46
47
48
49
50
51
package main
import (
"context"
"errors"
"fmt"
"k8s.io/cli-runtime/pkg/genericiooptions"
"os"
"os/signal"
"syscall"
"time"
"github.com/cqroot/prompt"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
streams := genericiooptions.IOStreams{
In: os.Stdin,
Out: os.Stdout,
ErrOut: os.Stderr,
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
cancel()
time.AfterFunc(
time.Second, func() {
os.Exit(4)
},
)
}()
cmd, _ := NewCommand(streams)
if err := cmd.ExecuteContext(ctx); err != nil {
if errors.Is(err, prompt.ErrUserQuit) {
os.Exit(2)
return
}
if errors.Is(err, context.Canceled) {
os.Exit(3)
return
}
_, _ = fmt.Fprintf(streams.ErrOut, "Error: %s\n", err)
os.Exit(1)
}
}