-
Notifications
You must be signed in to change notification settings - Fork 290
/
create_ext.go
113 lines (91 loc) · 2.9 KB
/
create_ext.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
package cli
import (
"context"
"time"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
"github.com/tilt-dev/tilt/internal/analytics"
engineanalytics "github.com/tilt-dev/tilt/internal/engine/analytics"
"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
"github.com/tilt-dev/tilt/pkg/model"
)
// A human-friendly CLI for creating extensions.
type createExtCmd struct {
helper *createHelper
cmd *cobra.Command
repoName string
repoPath string
}
var _ tiltCmd = &createExtCmd{}
func newCreateExtCmd(streams genericclioptions.IOStreams) *createExtCmd {
helper := newCreateHelper(streams)
return &createExtCmd{
helper: helper,
}
}
func (c *createExtCmd) name() model.TiltSubcommand { return "create" }
func (c *createExtCmd) register() *cobra.Command {
cmd := &cobra.Command{
Use: "ext NAME [ARG...]",
DisableFlagsInUseLine: true,
Short: "Register an extension.",
Long: `Register an extension with a running Tilt instance.
An extension will load a set of services into your dev environment.
These might be services you need to run your app, or servers
that add functionality to Tilt itself.
Assumes that an extension repo has already been registered
with 'tilt create repo' or in the Tiltfile.
`,
Args: cobra.MinimumNArgs(1),
Example: `
# Installs the extension from the extension repo 'default' under the path './cancel'.
tilt create ext cancel
# Installs the extension from the extension repo 'default' under
# and with custom argument '--namespaces=default' passed to the extension.
tilt create ext my-kubefwd --path=./kubefwd -- --namespaces=default
# Installs the extension from the extension repo 'dev' under the path './cancel'
tilt create ext cancel --repo=dev
`,
}
cmd.Flags().StringVar(&c.repoName, "repo", "default",
"The name of the extension repo (list existing repos with 'tilt get repo')")
cmd.Flags().StringVar(&c.repoPath, "path", "",
"The path of the extension. If not specified, defaults to the extension name.")
c.helper.addFlags(cmd)
c.cmd = cmd
return cmd
}
func (c *createExtCmd) run(ctx context.Context, args []string) error {
a := analytics.Get(ctx)
cmdTags := engineanalytics.CmdTags(map[string]string{})
a.Incr("cmd.create-ext", cmdTags.AsMap())
defer a.Flush(time.Second)
err := c.helper.interpretFlags(ctx)
if err != nil {
return err
}
name := args[0]
extArgs := []string{}
if c.cmd.ArgsLenAtDash() != -1 {
extArgs = args[c.cmd.ArgsLenAtDash():]
}
return c.helper.create(ctx, c.object(name, extArgs))
}
func (c *createExtCmd) object(name string, extArgs []string) *v1alpha1.Extension {
repoName := c.repoName
repoPath := c.repoPath
if repoPath == "" {
repoPath = name
}
return &v1alpha1.Extension{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: v1alpha1.ExtensionSpec{
RepoName: repoName,
RepoPath: repoPath,
Args: extArgs,
},
}
}