github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/cli/create_ext.go (about)

     1  package cli
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/spf13/cobra"
     8  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     9  	"k8s.io/cli-runtime/pkg/genericclioptions"
    10  
    11  	"github.com/tilt-dev/tilt/internal/analytics"
    12  	engineanalytics "github.com/tilt-dev/tilt/internal/engine/analytics"
    13  	"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
    14  	"github.com/tilt-dev/tilt/pkg/model"
    15  )
    16  
    17  // A human-friendly CLI for creating extensions.
    18  type createExtCmd struct {
    19  	helper *createHelper
    20  	cmd    *cobra.Command
    21  
    22  	repoName string
    23  	repoPath string
    24  }
    25  
    26  var _ tiltCmd = &createExtCmd{}
    27  
    28  func newCreateExtCmd(streams genericclioptions.IOStreams) *createExtCmd {
    29  	helper := newCreateHelper(streams)
    30  	return &createExtCmd{
    31  		helper: helper,
    32  	}
    33  }
    34  
    35  func (c *createExtCmd) name() model.TiltSubcommand { return "create" }
    36  
    37  func (c *createExtCmd) register() *cobra.Command {
    38  	cmd := &cobra.Command{
    39  		Use:                   "ext NAME [ARG...]",
    40  		DisableFlagsInUseLine: true,
    41  		Short:                 "Register an extension.",
    42  		Long: `Register an extension with a running Tilt instance.
    43  
    44  An extension will load a set of services into your dev environment.
    45  
    46  These might be services you need to run your app, or servers
    47  that add functionality to Tilt itself.
    48  
    49  Assumes that an extension repo has already been registered
    50  with 'tilt create repo' or in the Tiltfile.
    51  `,
    52  		Args: cobra.MinimumNArgs(1),
    53  		Example: `
    54  # Installs the extension from the extension repo 'default' under the path './cancel'.
    55  tilt create ext cancel
    56  
    57  # Installs the extension from the extension repo 'default' under
    58  # and with custom argument '--namespaces=default' passed to the extension.
    59  tilt create ext my-kubefwd --path=./kubefwd -- --namespaces=default
    60  
    61  # Installs the extension from the extension repo 'dev' under the path './cancel'
    62  tilt create ext cancel --repo=dev
    63  `,
    64  	}
    65  
    66  	cmd.Flags().StringVar(&c.repoName, "repo", "default",
    67  		"The name of the extension repo (list existing repos with 'tilt get repo')")
    68  	cmd.Flags().StringVar(&c.repoPath, "path", "",
    69  		"The path of the extension. If not specified, defaults to the extension name.")
    70  
    71  	c.helper.addFlags(cmd)
    72  	c.cmd = cmd
    73  
    74  	return cmd
    75  }
    76  
    77  func (c *createExtCmd) run(ctx context.Context, args []string) error {
    78  	a := analytics.Get(ctx)
    79  	cmdTags := engineanalytics.CmdTags(map[string]string{})
    80  	a.Incr("cmd.create-ext", cmdTags.AsMap())
    81  	defer a.Flush(time.Second)
    82  
    83  	err := c.helper.interpretFlags(ctx)
    84  	if err != nil {
    85  		return err
    86  	}
    87  
    88  	name := args[0]
    89  	extArgs := []string{}
    90  	if c.cmd.ArgsLenAtDash() != -1 {
    91  		extArgs = args[c.cmd.ArgsLenAtDash():]
    92  	}
    93  
    94  	return c.helper.create(ctx, c.object(name, extArgs))
    95  }
    96  
    97  func (c *createExtCmd) object(name string, extArgs []string) *v1alpha1.Extension {
    98  	repoName := c.repoName
    99  	repoPath := c.repoPath
   100  	if repoPath == "" {
   101  		repoPath = name
   102  	}
   103  	return &v1alpha1.Extension{
   104  		ObjectMeta: metav1.ObjectMeta{
   105  			Name: name,
   106  		},
   107  		Spec: v1alpha1.ExtensionSpec{
   108  			RepoName: repoName,
   109  			RepoPath: repoPath,
   110  			Args:     extArgs,
   111  		},
   112  	}
   113  }