github.com/tilt-dev/tilt@v0.36.0/internal/cli/flags.go (about)

     1  package cli
     2  
     3  import (
     4  	"os"
     5  	"strconv"
     6  	"strings"
     7  
     8  	"github.com/pkg/errors"
     9  	"github.com/spf13/cobra"
    10  
    11  	"github.com/tilt-dev/tilt/internal/hud"
    12  	"github.com/tilt-dev/tilt/internal/k8s"
    13  	"github.com/tilt-dev/tilt/internal/tiltfile"
    14  	"github.com/tilt-dev/tilt/pkg/model"
    15  )
    16  
    17  var (
    18  	defaultWebHost       = "localhost"
    19  	defaultWebPort       = model.DefaultWebPort
    20  	defaultNamespace     = ""
    21  	defaultLogLevel      = ""
    22  	defaultLogSource     = "all"
    23  	webHostFlag          = ""
    24  	webPortFlag          = 0
    25  	snapshotViewPortFlag = 0
    26  	namespaceOverride    = ""
    27  )
    28  
    29  func readEnvDefaults() error {
    30  	envPort := os.Getenv("TILT_PORT")
    31  	if envPort != "" {
    32  		port, err := strconv.Atoi(envPort)
    33  		if err != nil {
    34  			return errors.Wrap(err, "parsing env TILT_PORT")
    35  		}
    36  		defaultWebPort = port
    37  	}
    38  
    39  	envHost := os.Getenv("TILT_HOST")
    40  	if envHost != "" {
    41  		defaultWebHost = envHost
    42  	}
    43  	return nil
    44  }
    45  
    46  // Common flags used across multiple commands.
    47  
    48  // s: address of the field to populate
    49  func addTiltfileFlag(cmd *cobra.Command, s *string) {
    50  	cmd.Flags().StringVarP(s, "file", "f", tiltfile.FileName, "Path to Tiltfile")
    51  }
    52  
    53  func addKubeContextFlag(cmd *cobra.Command) {
    54  	cmd.Flags().StringVar(&kubeContextOverride, "context", "", "Kubernetes context override. Equivalent to kubectl --context")
    55  }
    56  
    57  // For commands that talk to the web server.
    58  func addConnectServerFlags(cmd *cobra.Command) {
    59  	cmd.Flags().IntVar(&webPortFlag, "port", defaultWebPort, "Port for the Tilt HTTP server. Only necessary if you started Tilt with --port. Overrides TILT_PORT env variable.")
    60  	cmd.Flags().StringVar(&webHostFlag, "host", defaultWebHost, "Host for the Tilt HTTP server. Only necessary if you started Tilt with --host. Overrides TILT_HOST env variable.")
    61  }
    62  
    63  // For commands that start a web server.
    64  func addStartServerFlags(cmd *cobra.Command) {
    65  	cmd.Flags().IntVar(&webPortFlag, "port", defaultWebPort, "Port for the Tilt HTTP server. Set to 0 to disable. Overrides TILT_PORT env variable.")
    66  	cmd.Flags().StringVar(&webHostFlag, "host", defaultWebHost, "Host for the Tilt HTTP server and default host for any port-forwards. Set to 0.0.0.0 to listen on all interfaces. Overrides TILT_HOST env variable.")
    67  }
    68  
    69  // For commands that start a random snapshot view web server.
    70  func addStartSnapshotViewServerFlags(cmd *cobra.Command) {
    71  	cmd.Flags().IntVar(&snapshotViewPortFlag, "port", 0, "Port for the HTTP server. Defaults to a random port.")
    72  	cmd.Flags().StringVar(&webHostFlag, "host", defaultWebHost, "Host for the HTTP server and default host for any port-forwards. Set to 0.0.0.0 to listen on all interfaces. Overrides TILT_HOST env variable.")
    73  }
    74  
    75  func addDevServerFlags(cmd *cobra.Command) {
    76  	cmd.Flags().IntVar(&webDevPort, "webdev-port", DefaultWebDevPort, "Port for the Tilt Dev Webpack server. Only applies when using --web-mode=local")
    77  	cmd.Flags().Var(&webModeFlag, "web-mode", "Values: local, prod. Controls whether to use prod assets or a local dev server. (If flag not specified: if Tilt was built from source, it will use a local asset server; otherwise, prod assets.)")
    78  }
    79  
    80  func addNamespaceFlag(cmd *cobra.Command) {
    81  	cmd.Flags().StringVar(&namespaceOverride, "namespace", defaultNamespace, "Default namespace for Kubernetes resources (overrides default namespace from active context in kubeconfig)")
    82  }
    83  
    84  func addLogFilterResourcesFlag(cmd *cobra.Command) {
    85  	cmd.Flags().StringSliceVar(&logResourcesFlag, "log-resource", nil, `Specify one or more resources to print logs for, e.g. "(Tiltfile)", "nginx", etc. If not specified, prints all resources.`)
    86  }
    87  
    88  func addLogFilterFlags(cmd *cobra.Command, prefix string) {
    89  	cmd.Flags().StringVar(&logLevelFlag, prefix+"level", defaultLogLevel, `Specify a log level. One of "warn", "error"`)
    90  	_ = cmd.RegisterFlagCompletionFunc(
    91  		prefix+"level",
    92  		func(_ *cobra.Command, _ []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    93  			var completions []string
    94  			options := []string{"warn", "error"}
    95  			for _, option := range options {
    96  				if strings.Contains(option, strings.ToLower(toComplete)) {
    97  					completions = append(completions, option)
    98  				}
    99  			}
   100  
   101  			return completions, cobra.ShellCompDirectiveNoFileComp
   102  		},
   103  	)
   104  	cmd.Flags().StringVar(&logSourceFlag, prefix+"source", defaultLogSource, `Specify a log source. One of "all", "build", "runtime"`)
   105  	_ = cmd.RegisterFlagCompletionFunc(
   106  		prefix+"source",
   107  		func(_ *cobra.Command, _ []string, toComplete string) ([]string, cobra.ShellCompDirective) {
   108  			var completions []string
   109  			options := []string{
   110  				hud.FilterSourceAll.String(),
   111  				hud.FilterSourceBuild.String(),
   112  				hud.FilterSourceRuntime.String(),
   113  			}
   114  			for _, option := range options {
   115  				if strings.Contains(option, strings.ToLower(toComplete)) {
   116  					completions = append(completions, option)
   117  				}
   118  			}
   119  
   120  			return completions, cobra.ShellCompDirectiveNoFileComp
   121  		},
   122  	)
   123  }
   124  
   125  var kubeContextOverride string
   126  
   127  func ProvideKubeContextOverride() k8s.KubeContextOverride {
   128  	return k8s.KubeContextOverride(kubeContextOverride)
   129  }
   130  
   131  func ProvideNamespaceOverride() k8s.NamespaceOverride {
   132  	return k8s.NamespaceOverride(namespaceOverride)
   133  }