-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cli: add tilt alpha create cmd (#4440)
- Loading branch information
Showing
7 changed files
with
282 additions
and
50 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package cli | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
|
||
"github.com/spf13/cobra" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
"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() *createCmdCmd { | ||
helper := newCreateHelper() | ||
return &createCmdCmd{ | ||
helper: helper, | ||
} | ||
} | ||
|
||
func (c *createCmdCmd) name() model.TiltSubcommand { return "cmd" } | ||
|
||
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 alpha create cmd my-cmd echo hello world | ||
tilt alpha 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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package cli | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"k8s.io/apimachinery/pkg/types" | ||
|
||
"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1" | ||
) | ||
|
||
func TestCreateCmd(t *testing.T) { | ||
f := newServerFixture(t) | ||
defer f.TearDown() | ||
|
||
out := bytes.NewBuffer(nil) | ||
|
||
cmd := newCreateCmdCmd() | ||
cmd.helper.streams.Out = out | ||
c := cmd.register() | ||
err := c.Flags().Parse([]string{ | ||
"--env", "COLOR=1", | ||
"-e", "USER=nick", | ||
}) | ||
require.NoError(t, err) | ||
|
||
err = cmd.run(f.ctx, []string{"my-cmd", "echo", "hello", "world"}) | ||
require.NoError(t, err) | ||
assert.Contains(t, out.String(), `cmd.tilt.dev/my-cmd created`) | ||
|
||
var myCmd v1alpha1.Cmd | ||
err = f.client.Get(f.ctx, types.NamespacedName{Name: "my-cmd"}, &myCmd) | ||
require.NoError(t, err) | ||
|
||
cwd, _ := os.Getwd() | ||
assert.Equal(t, cwd, myCmd.Spec.Dir) | ||
assert.Equal(t, []string{"echo", "hello", "world"}, myCmd.Spec.Args) | ||
assert.Equal(t, []string{"COLOR=1", "USER=nick"}, myCmd.Spec.Env) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package cli | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/cli-runtime/pkg/genericclioptions" | ||
"k8s.io/cli-runtime/pkg/printers" | ||
"k8s.io/client-go/dynamic" | ||
|
||
"github.com/tilt-dev/tilt-apiserver/pkg/server/builder/resource" | ||
) | ||
|
||
// Helper for human-friendly CLI for creating objects. | ||
// | ||
// See other create commands for usage examples. | ||
type createHelper struct { | ||
streams genericclioptions.IOStreams | ||
printFlags *genericclioptions.PrintFlags | ||
dynamicClient dynamic.Interface | ||
printer printers.ResourcePrinter | ||
} | ||
|
||
func newCreateHelper() *createHelper { | ||
streams := genericclioptions.IOStreams{Out: os.Stdout, ErrOut: os.Stderr, In: os.Stdin} | ||
return &createHelper{ | ||
streams: streams, | ||
printFlags: genericclioptions.NewPrintFlags("created"), | ||
} | ||
} | ||
|
||
func (h *createHelper) addFlags(cmd *cobra.Command) { | ||
h.printFlags.AddFlags(cmd) | ||
addConnectServerFlags(cmd) | ||
} | ||
|
||
func (h *createHelper) interpretFlags(ctx context.Context) error { | ||
printer, err := h.printFlags.ToPrinter() | ||
if err != nil { | ||
return err | ||
} | ||
h.printer = printer | ||
|
||
dynamicClient, err := h.createDynamicClient(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
h.dynamicClient = dynamicClient | ||
return nil | ||
} | ||
|
||
func (h *createHelper) create(ctx context.Context, resourceObj resource.Object) error { | ||
obj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(resourceObj) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
result, err := h.dynamicClient.Resource(resourceObj.GetGroupVersionResource()). | ||
Create(ctx, &unstructured.Unstructured{Object: obj}, metav1.CreateOptions{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return h.printer.PrintObj(result, h.streams.Out) | ||
} | ||
|
||
// Loads a dynamically typed tilt client. | ||
func (h *createHelper) createDynamicClient(ctx context.Context) (dynamic.Interface, error) { | ||
getter, err := wireClientGetter(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
config, err := getter.ToRESTConfig() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return dynamic.NewForConfig(config) | ||
} |