-
Notifications
You must be signed in to change notification settings - Fork 290
/
create_cmd.go
148 lines (121 loc) · 3.47 KB
/
create_cmd.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
package cli
import (
"context"
"os"
"path/filepath"
"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 cmds.
//
// The name is unfortunate.
//
// (as opposed to the machine-friendly CLIs of create -f or apply -f)
type createCmdCmd struct {
helper *createHelper
dir string
env []string
filewatches []string
}
var _ tiltCmd = &createCmdCmd{}
func newCreateCmdCmd(streams genericclioptions.IOStreams) *createCmdCmd {
helper := newCreateHelper(streams)
return &createCmdCmd{
helper: helper,
}
}
func (c *createCmdCmd) name() model.TiltSubcommand { return "create" }
func (c *createCmdCmd) register() *cobra.Command {
cmd := &cobra.Command{
Use: "cmd NAME COMMAND [ARG...]",
DisableFlagsInUseLine: true,
Short: "Create a local command in a running tilt session",
Long: `Create a local command in a running tilt session.
Intended to compose with other Tilt APIs that
can restart the command or monitor its status.
COMMAND should be an executable. A shell script will not work.
To run a shell script, use 'sh -c' (as shown in the examples).
`,
Args: cobra.MinimumNArgs(2),
Example: `
tilt create cmd my-cmd echo hello world
tilt create cmd my-cmd sh -c "echo hi && echo bye"
`,
}
// Interpret any flags after the object name as part of the
// command args.
cmd.Flags().SetInterspersed(false)
cmd.Flags().StringVarP(&c.dir, "workdir", "w", "",
"Working directory of the command. If not specified, uses the current working directory.")
cmd.Flags().StringArrayVarP(&c.env, "env", "e", nil,
"Set environment variables in the form NAME=VALUE.")
cmd.Flags().StringSliceVar(&c.filewatches, "filewatch", nil,
("Re-run the command whenever the named filewatches detect a change. " +
"See 'tilt create filewatch' for more."))
c.helper.addFlags(cmd)
return cmd
}
func (c *createCmdCmd) run(ctx context.Context, args []string) error {
a := analytics.Get(ctx)
cmdTags := engineanalytics.CmdTags(map[string]string{})
a.Incr("cmd.create-cmd", cmdTags.AsMap())
defer a.Flush(time.Second)
err := c.helper.interpretFlags(ctx)
if err != nil {
return err
}
cmd, err := c.object(args)
if err != nil {
return err
}
return c.helper.create(ctx, cmd)
}
// Interprets the flags specified on the commandline to the Cmd to create.
func (c *createCmdCmd) object(args []string) (*v1alpha1.Cmd, error) {
name := args[0]
command := args[1:]
dir, err := c.workdir()
if err != nil {
return nil, err
}
env := c.env
restartOn := c.restartOn()
cmd := v1alpha1.Cmd{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: v1alpha1.CmdSpec{
Args: command,
Dir: dir,
Env: env,
RestartOn: restartOn,
},
}
return &cmd, nil
}
// Determine the working directory of the command.
func (c *createCmdCmd) workdir() (string, error) {
cwd, err := os.Getwd()
if err != nil {
return "", err
}
if c.dir == "" {
return cwd, nil
}
if filepath.IsAbs(c.dir) {
return c.dir, nil
}
return filepath.Join(cwd, c.dir), nil
}
// Determine the restart conditions of the command.
func (c *createCmdCmd) restartOn() *v1alpha1.RestartOnSpec {
return &v1alpha1.RestartOnSpec{
FileWatches: c.filewatches,
}
}