-
Notifications
You must be signed in to change notification settings - Fork 310
/
Copy pathcli.go
166 lines (137 loc) · 4.13 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package cli
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/spf13/cobra"
"github.com/tilt-dev/wmclient/pkg/analytics"
"go.opencensus.io/stats"
"github.com/tilt-dev/tilt/pkg/model"
tiltanalytics "github.com/tilt-dev/tilt/internal/analytics"
"github.com/tilt-dev/tilt/internal/output"
"github.com/tilt-dev/tilt/pkg/logger"
)
var debug bool
var verbose 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() {
err := readEnvDefaults()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
rootCmd := &cobra.Command{
Use: "tilt",
Short: "Multi-service development with no stress",
Long: `
Tilt helps you develop your microservices locally.
Run 'tilt up' to start working on your services in a complete dev environment
configured for your team.
Tilt watches your files for edits, automatically builds your container images,
and applies any changes to bring your environment
up-to-date in real-time. Think 'docker build && kubectl apply' or 'docker-compose up'.
`,
}
addCommand(rootCmd, &ciCmd{})
addCommand(rootCmd, &upCmd{})
addCommand(rootCmd, &dockerCmd{})
addCommand(rootCmd, &doctorCmd{})
addCommand(rootCmd, newDownCmd())
addCommand(rootCmd, &versionCmd{})
addCommand(rootCmd, &verifyInstallCmd{})
addCommand(rootCmd, &dockerPruneCmd{})
addCommand(rootCmd, newArgsCmd())
addCommand(rootCmd, &logsCmd{})
addCommand(rootCmd, newDescribeCmd())
addCommand(rootCmd, newGetCmd())
addCommand(rootCmd, newEditCmd())
addCommand(rootCmd, newApiresourcesCmd())
addCommand(rootCmd, newDeleteCmd())
addCommand(rootCmd, newApplyCmd())
addCommand(rootCmd, newCreateCmd())
rootCmd.AddCommand(analytics.NewCommand())
rootCmd.AddCommand(newDumpCmd(rootCmd))
rootCmd.AddCommand(newTriggerCmd())
rootCmd.AddCommand(newAlphaCmd())
globalFlags := rootCmd.PersistentFlags()
globalFlags.BoolVarP(&debug, "debug", "d", false, "Enable debug logging")
globalFlags.BoolVarP(&verbose, "verbose", "v", false, "Enable verbose logging")
globalFlags.IntVar(&klogLevel, "klog", 0, "Enable Kubernetes API logging. Uses klog v-levels (0-4 are debug logs, 5-9 are tracing logs)")
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
type tiltCmd interface {
name() model.TiltSubcommand
register() *cobra.Command
run(ctx context.Context, args []string) error
}
func preCommand(ctx context.Context, cmdName model.TiltSubcommand) (context.Context, func() error) {
l := logger.NewLogger(logLevel(verbose, debug), os.Stdout)
ctx = logger.WithLogger(ctx, l)
ctx, cleanup, err := initMetrics(ctx, cmdName)
if err != nil {
l.Errorf("Fatal error initializing metrics: %v", err)
os.Exit(1)
}
stats.Record(ctx, CommandCountMeasure.M(1))
a, err := wireAnalytics(l, cmdName)
if err != nil {
l.Errorf("Fatal error initializing analytics: %v", err)
os.Exit(1)
}
ctx = tiltanalytics.WithAnalytics(ctx, a)
initKlog(l.Writer(logger.InfoLvl))
// SIGNAL TRAPPING
ctx, cancel := context.WithCancel(ctx)
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
go func() {
<-sigs
cancel()
// If we get another signal, OR it takes too long for tilt to
// exit after canceling 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(), child.name())
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(output.OriginalStderr, "Error: %v\n", err)
if printErr != nil {
panic(printErr)
}
os.Exit(1)
}
}
parent.AddCommand(cobraChild)
}