Documentation
¶
Index ¶
- Constants
- Variables
- func Execute(ctx context.Context) error
- func GetCurrentVersion() string
- func ListWorkspaces(cmd *cobra.Command, args []string) error
- func PromptForStackParameters(ctx context.Context, params *stacks.Parameters) error
- func SetupCommands(version string)
- type ColorMode
- type ExitCode
- type GlobalConfig
Constants ¶
const DEFANG_PORTAL_HOST = "portal.defang.io"
const SERVICE_PORTAL_URL = "https://" + DEFANG_PORTAL_HOST + "/service"
Variables ¶
var P = track.P
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 GetCurrentVersion ¶
func GetCurrentVersion() string
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 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:
- Command-line flags (highest priority)
- Environment variables (DEFANG_* prefix)
- 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