command

package
v0.0.0-...-db86d87 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 18, 2026 License: MIT Imports: 49 Imported by: 1

Documentation

Index

Constants

View Source
const DEFANG_PORTAL_HOST = "portal.defang.io"
View Source
const SERVICE_PORTAL_URL = "https://" + DEFANG_PORTAL_HOST + "/service"

Variables

View Source
var P = track.P
View Source
var RootCmd = &cobra.Command{
	SilenceUsage:  true,
	SilenceErrors: true,
	Use:           "defang",
	Args:          cobra.NoArgs,
	Short:         "Defang CLI is used to take your app from Docker Compose to a secure and scalable deployment on your favorite cloud in minutes.",
	PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) {

		if isCompletionCommand(cmd) {

			if cwd := getCwd(os.Args); cwd != "" {
				return os.Chdir(cwd)
			}
			return nil
		}

		track.Tracker = cli.Connect(global.Cluster, global.Tenant)

		ctx := cmd.Context()
		term.SetDebug(global.Debug)

		defer func() {
			var errString = ""
			if err != nil {
				errString = err.Error()
			}

			track.Cmd(cmd, "Invoked", P("args", args), P("err", errString), P("non-interactive", global.NonInteractive), P("provider", global.Stack.Provider))
		}()

		switch global.ColorMode {
		case ColorNever:
			term.ForceColor(false)
		case ColorAlways:
			term.ForceColor(true)
		}

		if cwd, _ := cmd.Flags().GetString("cwd"); cwd != "" {

			if err = os.Chdir(cwd); err != nil {
				return err
			}
		}

		global.Client, err = cli.ConnectWithTenant(ctx, global.Cluster, global.Tenant)
		if err != nil {
			if connect.CodeOf(err) != connect.CodeUnauthenticated {
				return err
			}
			term.Debug("Using existing token failed; continuing to allow login/ToS flow:", err)
		}

		track.Tracker = global.Client

		if v, err := global.Client.GetVersions(ctx); err == nil {
			version := cmd.Root().Version
			term.Debug("Fabric:", v.Fabric, "CLI:", version, "CLI-Min:", v.CliMin)
			if global.HasTty && isNewer(version, v.CliMin) && !isUpgradeCommand(cmd) {
				term.Warn("Your CLI version is outdated. Please upgrade to the latest version by running:\n\n  defang upgrade\n")
				global.HideUpdate = true
			}
		}

		if _, ok := cmd.Annotations[authNeeded]; !ok {
			return nil
		}

		if global.NonInteractive {
			err = global.Client.CheckLoginAndToS(ctx)
		} else {
			err = login.InteractiveRequireLoginAndToS(ctx, global.Client, global.Cluster)
		}

		return err
	},
	RunE: func(cmd *cobra.Command, args []string) error {
		if global.NonInteractive {
			return cmd.Help()
		}

		ctx := cmd.Context()
		err := login.InteractiveRequireLoginAndToS(ctx, global.Client, global.Cluster)
		if err != nil {
			return err
		}

		prompt := "Welcome to Defang. I can help you deploy your project to the cloud."
		ag, err := agent.New(ctx, global.Cluster, &global.Stack)
		if err != nil {
			return err
		}
		return ag.StartWithUserPrompt(ctx, prompt)
	},
}

Functions

func Execute

func Execute(ctx context.Context) error

func GetCurrentVersion

func GetCurrentVersion() string

func ListWorkspaces

func ListWorkspaces(cmd *cobra.Command, args []string) error

func PromptForStackParameters

func PromptForStackParameters(ctx context.Context, params *stacks.Parameters) error

func SetupCommands

func SetupCommands(version string)

SetupCommands initializes and configures the entire Defang CLI command structure. It registers all global flags that bind to GlobalConfig, sets up all subcommands with their specific flags, and establishes the command hierarchy.

Types

type ColorMode

type ColorMode string
const (
	// ColorNever disables color output.
	ColorNever ColorMode = "never"
	// ColorAuto enables color output only if the output is connected to a terminal.
	ColorAuto ColorMode = "auto"
	// ColorAlways enables color output.
	ColorAlways ColorMode = "always"
)

func (*ColorMode) Set

func (c *ColorMode) Set(value string) error

func (ColorMode) String

func (c ColorMode) String() string

func (ColorMode) Type

func (c ColorMode) Type() string

type ExitCode

type ExitCode int

func (ExitCode) Error

func (e ExitCode) Error() string

type GlobalConfig

type GlobalConfig struct {
	Client         *client.GrpcClient
	Cluster        string
	ColorMode      ColorMode
	Debug          bool
	HasTty         bool
	HideUpdate     bool
	ModelID        string // only for debug/generate; Pro users
	NonInteractive bool
	Stack          stacks.Parameters
	Tenant         types.TenantNameOrID // workspace
	Verbose        bool
}

GlobalConfig holds the global configuration options for the Defang CLI. These options can be configured through multiple sources with the following priority order:

  1. Command-line flags (highest priority)
  2. Environment variables (DEFANG_* prefix)
  3. Configuration files in the .defang directory (lowest priority)

Configuration Flow:

  • Default values are set when initializing the global variable
  • Environment variables are synced to struct fields (syncFlagsWithEnv)
  • Command-line flags take precedence over all other sources

Adding New Configuration Options: To add a new configuration option, you must update these components:

1. Add the field to this GlobalConfig struct with appropriate type and Go documentation

2. Set a default value in the global variable initialization (top of this file)

3. Register the command-line flag in SetupCommands() function (commands.go):

  • For boolean flags: use BoolVar() or BoolVarP()
  • For string flags: use StringVar() or StringVarP()
  • For custom types: use Var() or VarP() (type must implement pflag.Value interface)
  • Example: RootCmd.PersistentFlags().BoolVar(&global.NewFlag, "new-flag", global.NewFlag, "description")

4. Add environment variable synchronization in syncFlagsWithEnv() method:

  • Check if flag was changed by user with flags.Changed("flag-name")
  • If not changed, read from environment variable DEFANG_FLAG_NAME
  • Handle type conversion (strconv.ParseBool for bool, direct assignment for string, etc.)

Example pattern:

if !flags.Changed("new-flag") {
	if fromEnv, ok := os.LookupEnv("DEFANG_NEW_FLAG"); ok {
		global.NewFlag, err = strconv.ParseBool(fromEnv) // for bool
		if err != nil {
			return err
		}
	}
}

5. For non-flag environment variables (like HasTty, HideUpdate), add handling in syncNonFlagEnvVars()

Note: Ensure the flag name, environment variable name, and struct field name are consistent and follow the established naming conventions.

func NewGlobalConfig

func NewGlobalConfig() *GlobalConfig

func (*GlobalConfig) Interactive

func (global *GlobalConfig) Interactive() bool

func (*GlobalConfig) ToMap

func (global *GlobalConfig) ToMap() map[string]string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL