-
Notifications
You must be signed in to change notification settings - Fork 290
/
dump.go
312 lines (260 loc) · 7.5 KB
/
dump.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package cli
import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
"k8s.io/cli-runtime/pkg/genericclioptions"
"github.com/tilt-dev/tilt/internal/container"
"github.com/tilt-dev/tilt/internal/tiltfile"
"github.com/tilt-dev/tilt/pkg/model"
)
func newDumpCmd(rootCmd *cobra.Command, streams genericclioptions.IOStreams) *cobra.Command {
result := &cobra.Command{
Use: "dump",
Short: "Dump internal Tilt state",
Long: `Dumps internal Tilt state to stdout.
Intended to help Tilt developers inspect Tilt when things go wrong,
and figure out better ways to expose this info to Tilt users.
The format of the dump state does not make any API or compatibility promises,
and may change frequently.
`,
}
result.AddCommand(newDumpApiDocsCmd())
result.AddCommand(newDumpWebviewCmd())
result.AddCommand(newDumpEngineCmd())
result.AddCommand(newDumpLogStoreCmd())
result.AddCommand(newDumpCliDocsCmd(rootCmd))
result.AddCommand(newDumpImageDeployRefCmd())
addCommand(result, newOpenapiCmd(streams))
return result
}
func newDumpApiDocsCmd() *cobra.Command {
c := &apiDocsCmd{}
cmd := &cobra.Command{
Use: "api-docs",
Short: "dump the Tiltfile api documentation stub files",
Long: `Dumps the api documentation stub files to the provided directory.
The api stub files define the builtin functions, modules, and types used in Tiltfiles.
`,
Run: c.run,
Args: cobra.NoArgs,
}
cmd.Flags().StringVar(&c.dir, "dir", ".", "The directory to dump to")
return cmd
}
func newDumpWebviewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "webview",
Short: "dump the state backing the webview",
Long: `Dumps the state backing the webview to stdout.
The webview is the JSON used to render the React UX.
The format of the dump state does not make any API or compatibility promises,
and may change frequently.
`,
Run: dumpWebview,
Args: cobra.NoArgs,
}
addConnectServerFlags(cmd)
return cmd
}
func newDumpEngineCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "engine",
Short: "dump the engine state",
Long: `Dumps the state of the Tilt engine to stdout.
The engine state is the central store where Tilt keeps all information about
the build specification, build history, and deployed resources.
The format of the dump state does not make any API or compatibility promises,
and may change frequently.
Excludes logs.
`,
Run: dumpEngine,
Args: cobra.NoArgs,
}
addConnectServerFlags(cmd)
return cmd
}
func newDumpLogStoreCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "logstore",
Short: "dump the log store",
Long: `Dumps the state of the Tilt log store to stdout.
Every log of a Tilt-managed resource is aggregated into a central structured log
store before display. Dumps the JSON representation of this store.
The format of the dump state does not make any API or compatibility promises,
and may change frequently.
`,
Run: dumpLogStore,
Args: cobra.NoArgs,
}
addConnectServerFlags(cmd)
return cmd
}
type dumpCliDocsCmd struct {
rootCmd *cobra.Command
dir string
}
func newDumpCliDocsCmd(rootCmd *cobra.Command) *cobra.Command {
c := &dumpCliDocsCmd{rootCmd: rootCmd}
cmd := &cobra.Command{
Use: "cli-docs",
Short: "Dumps markdown docs of the CLI",
Args: cobra.NoArgs,
Run: c.run,
}
cmd.Flags().StringVar(&c.dir, "dir", ".", "The directory to dump to")
return cmd
}
func (c *dumpCliDocsCmd) filePrepender(path string) string {
return `---
title: Tilt CLI Reference
layout: docs
sidebar: reference
hideEditButton: true
---
`
}
func (c *dumpCliDocsCmd) linkHandler(link string) string {
if strings.HasSuffix(link, ".md") {
return strings.TrimSuffix(link, ".md") + ".html"
}
return link
}
func (c *dumpCliDocsCmd) run(cmd *cobra.Command, args []string) {
err := doc.GenMarkdownTreeCustom(c.rootCmd, c.dir, c.filePrepender, c.linkHandler)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Error generating CLI docs: %v", err)
os.Exit(1)
}
}
func newDumpImageDeployRefCmd() *cobra.Command {
return &cobra.Command{
Use: "image-deploy-ref REF",
Short: "Determine the name and tag with which Tilt will deploy the given image",
Long: `Determine the name and tag with which Tilt will deploy the given image.
This command is intended to be used with custom_build scripts.
Once the custom_build script has built the image at $EXPECTED_REF, it can
invoke:
echo $(tilt dump image-deploy-ref $EXPECTED_REF)
to print the deploy ref of the image. Tilt will read the image contents,
determine its hash, and create a content-based tag.
More info on custom build scripts: https://docs.tilt.dev/custom_build.html
`,
Example: "tilt dump image-deploy-ref $EXPECTED_REF",
Run: dumpImageDeployRef,
Args: cobra.ExactArgs(1),
}
}
func dumpImageDeployRef(cmd *cobra.Command, args []string) {
ctx := preCommand(context.Background(), "dump")
deps, err := wireDumpImageDeployRefDeps(ctx)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Initialization error: %v\n", err)
os.Exit(1)
}
// Assume that people with complex custom_build commands are using
// the kubernetes orchestrator.
deps.DockerClient.SetOrchestrator(model.OrchestratorK8s)
ref, err := deps.DockerBuilder.DumpImageDeployRef(ctx, args[0])
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
fmt.Printf("%s", container.FamiliarString(ref))
}
func dumpWebview(cmd *cobra.Command, args []string) {
body := apiGet("view")
err := dumpJSON(body)
if err != nil {
cmdFail(fmt.Errorf("dump webview: %v", err))
}
}
func dumpEngine(cmd *cobra.Command, args []string) {
body := apiGet("dump/engine")
defer func() {
_ = body.Close()
}()
result, err := decodeJSON(body)
if err != nil {
cmdFail(fmt.Errorf("dump engine: %v", err))
}
obj, ok := result.(map[string]interface{})
if ok {
delete(obj, "LogStore")
}
err = encodeJSON(os.Stdout, obj)
if err != nil {
cmdFail(fmt.Errorf("dump engine: %v", err))
}
}
func dumpLogStore(cmd *cobra.Command, args []string) {
body := apiGet("dump/engine")
defer func() {
_ = body.Close()
}()
result, err := decodeJSON(body)
if err != nil {
cmdFail(fmt.Errorf("dump LogStore: %v", err))
}
var logStore interface{}
obj, ok := result.(map[string]interface{})
if ok {
logStore, ok = obj["LogStore"]
}
if !ok {
cmdFail(fmt.Errorf("No LogStore in engine: %v", err))
}
err = encodeJSON(os.Stdout, logStore)
if err != nil {
cmdFail(fmt.Errorf("dump LogStore: %v", err))
}
}
type apiDocsCmd struct {
dir string
}
func (a *apiDocsCmd) run(cmd *cobra.Command, args []string) {
stat, err := os.Stat(a.dir)
if err != nil || !stat.IsDir() {
cmdFail(fmt.Errorf("Provided name %v doesn't exist or isn't a directory", a.dir))
}
err = tiltfile.DumpApiStubs(a.dir, tiltInfo(), func(path string, e error) {
if e == nil {
fmt.Printf("wrote %s\n", filepath.Join(a.dir, path))
}
})
if err != nil {
cmdFail(fmt.Errorf("dump api-docs: %v", err))
}
}
func dumpJSON(reader io.Reader) error {
result, err := decodeJSON(reader)
if err != nil {
return err
}
return encodeJSON(os.Stdout, result)
}
func decodeJSON(reader io.Reader) (interface{}, error) {
decoder := json.NewDecoder(reader)
var result interface{}
err := decoder.Decode(&result)
if err != nil {
return nil, errors.Wrap(err, "Could not decode")
}
return result, err
}
func encodeJSON(w io.Writer, result interface{}) error {
encoder := json.NewEncoder(w)
encoder.SetIndent("", " ")
err := encoder.Encode(result)
if err != nil {
return errors.Wrap(err, "Could not print")
}
return nil
}