-
Notifications
You must be signed in to change notification settings - Fork 290
/
cli.go
125 lines (103 loc) · 2.68 KB
/
cli.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package cli
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/windmilleng/tilt/internal/tracer"
"github.com/spf13/cobra"
"github.com/windmilleng/tilt/internal/logger"
)
var debug bool
var verbose bool
var trace bool
func logLevel(verbose, debug bool) logger.Level {
if debug {
return logger.DebugLvl
} else if verbose {
return logger.VerboseLvl
} else {
return logger.InfoLvl
}
}
func Execute() {
rootCmd := &cobra.Command{
Use: "tilt",
Short: "tilt creates Kubernetes Live Deploys that reflect changes seconds after they’re made",
}
err := initAnalytics(rootCmd)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
addCommand(rootCmd, &upCmd{})
addCommand(rootCmd, &downCmd{})
addCommand(rootCmd, &demoCmd{})
addCommand(rootCmd, &versionCmd{})
rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "Enable debug logging")
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Enable verbose logging")
rootCmd.PersistentFlags().BoolVar(&trace, "trace", false, "Enable tracing")
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
type tiltCmd interface {
register() *cobra.Command
run(ctx context.Context, args []string) error
}
func preCommand(ctx context.Context) (context.Context, func() error) {
cleanup := func() error { return nil }
l := logger.NewLogger(logLevel(verbose, debug), os.Stdout)
ctx = logger.WithLogger(ctx, l)
if trace {
var err error
cleanup, err = tracer.Init(ctx)
if err != nil {
log.Printf("Warning: unable to initialize tracer: %s", err)
}
}
// SIGNAL TRAPPING
ctx, cancel := context.WithCancel(ctx)
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
_ = <-sigs
cancel()
// If we get another SIGINT/SIGTERM, OR it takes too long for tilt to
// exit after cancelling context, just exit
select {
case <-sigs:
l.Debugf("force quitting...")
os.Exit(1)
case <-time.After(2 * time.Second):
l.Debugf("Context canceled but app still running; forcibly exiting.")
os.Exit(1)
}
}()
return ctx, cleanup
}
func addCommand(parent *cobra.Command, child tiltCmd) {
cobraChild := child.register()
cobraChild.Run = func(_ *cobra.Command, args []string) {
ctx, cleanup := preCommand(context.Background())
err := child.run(ctx, args)
err2 := cleanup()
// ignore cleanup errors if we have a real error
if err == nil {
err = err2
}
if err != nil {
// TODO(maia): this shouldn't print if we've already pretty-printed it
_, printErr := fmt.Fprintf(os.Stderr, "Error: %v\n", err)
if printErr != nil {
panic(printErr)
}
os.Exit(1)
}
}
parent.AddCommand(cobraChild)
}