Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust command aliases #1804

Merged
merged 10 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion cli/azd/cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import (
func authActions(root *actions.ActionDescriptor) *actions.ActionDescriptor {
group := root.Add("auth", &actions.ActionDescriptorOptions{
Command: &cobra.Command{
Hidden: true,
Use: "auth",
Short: "Authenticate with Azure.",
},
GroupingOptions: actions.CommandGroupOptions{
RootLevelHelp: actions.CmdGroupConfig,
},
})

Expand All @@ -21,5 +25,18 @@ func authActions(root *actions.ActionDescriptor) *actions.ActionDescriptor {
DefaultFormat: output.NoneFormat,
})

group.Add("login", &actions.ActionDescriptorOptions{
Command: newLoginCmd("auth"),
FlagsResolver: newLoginFlags,
ActionResolver: newLoginAction,
OutputFormats: []output.Format{output.JsonFormat, output.NoneFormat},
DefaultFormat: output.NoneFormat,
})

group.Add("logout", &actions.ActionDescriptorOptions{
Command: newLogoutCmd("auth"),
ActionResolver: newLogoutAction,
})

return group
}
11 changes: 10 additions & 1 deletion cli/azd/cmd/login.go → cli/azd/cmd/auth_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import (
"github.com/spf13/pflag"
)

// The parent of the login command.
const loginCmdParentAnnotation = "loginCmdParent"

type loginFlags struct {
onlyCheckStatus bool
useDeviceCode bool
Expand Down Expand Up @@ -120,7 +123,7 @@ func newLoginFlags(cmd *cobra.Command, global *internal.GlobalCommandOptions) *l
return flags
}

func newLoginCmd() *cobra.Command {
func newLoginCmd(parent string) *cobra.Command {
return &cobra.Command{
Use: "login",
Short: "Log in to Azure.",
Expand All @@ -133,6 +136,9 @@ func newLoginCmd() *cobra.Command {
To log in as a service principal, pass --client-id and --tenant-id as well as one of: --client-secret,
--client-certificate, --federated-credential, or --federated-credential-provider.
`),
Annotations: map[string]string{
loginCmdParentAnnotation: parent,
},
}
}

Expand All @@ -143,6 +149,7 @@ type loginAction struct {
authManager *auth.Manager
accountSubManager *account.SubscriptionsManager
flags *loginFlags
annotations CmdAnnotations
}

func newLoginAction(
weikanglim marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -152,6 +159,7 @@ func newLoginAction(
accountSubManager *account.SubscriptionsManager,
flags *loginFlags,
console input.Console,
annotations CmdAnnotations,
) actions.Action {
return &loginAction{
formatter: formatter,
Expand All @@ -160,6 +168,7 @@ func newLoginAction(
authManager: authManager,
accountSubManager: accountSubManager,
flags: flags,
annotations: annotations,
}
}

Expand Down
15 changes: 13 additions & 2 deletions cli/azd/cmd/logout.go → cli/azd/cmd/auth_logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ import (
"github.com/azure/azure-dev/cli/azd/cmd/actions"
"github.com/azure/azure-dev/cli/azd/pkg/account"
"github.com/azure/azure-dev/cli/azd/pkg/auth"
"github.com/azure/azure-dev/cli/azd/pkg/input"
"github.com/azure/azure-dev/cli/azd/pkg/output"
"github.com/spf13/cobra"
)

func newLogoutCmd() *cobra.Command {
func newLogoutCmd(parent string) *cobra.Command {
return &cobra.Command{
Use: "logout",
Short: "Log out of Azure.",
Long: "Log out of Azure",
Annotations: map[string]string{
loginCmdParentAnnotation: parent,
},
}
}
weikanglim marked this conversation as resolved.
Show resolved Hide resolved

Expand All @@ -27,17 +31,24 @@ type logoutAction struct {
accountSubManager *account.SubscriptionsManager
formatter output.Formatter
writer io.Writer
console input.Console
annotations CmdAnnotations
}

func newLogoutAction(
authManager *auth.Manager,
accountSubManager *account.SubscriptionsManager,
formatter output.Formatter, writer io.Writer) actions.Action {
formatter output.Formatter,
writer io.Writer,
console input.Console,
annotations CmdAnnotations) actions.Action {
return &logoutAction{
authManager: authManager,
accountSubManager: accountSubManager,
formatter: formatter,
writer: writer,
console: console,
annotations: annotations,
}
}

Expand Down
4 changes: 4 additions & 0 deletions cli/azd/cmd/cmd_help.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ func getCommandsDetails(cmd *cobra.Command) (result string) {
max := 0
var lines []string
for _, childCommand := range childrenCommands {
if !childCommand.IsAvailableCommand() {
weikanglim marked this conversation as resolved.
Show resolved Hide resolved
continue
}

commandName := fmt.Sprintf(" %s", childCommand.Name())
commandNameLen := len(commandName)
if commandNameLen > max {
Expand Down
13 changes: 9 additions & 4 deletions cli/azd/cmd/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ func registerCommonDependencies(container *ioc.NestedContainer) {
return envFlag{environmentName: envValue}
})

container.RegisterSingleton(func(cmd *cobra.Command) CmdAnnotations {
return cmd.Annotations
})

// Azd Context
container.RegisterSingleton(azdcontext.NewAzdContext)

Expand Down Expand Up @@ -334,8 +338,9 @@ func registerCommonDependencies(container *ioc.NestedContainer) {
// Required for nested actions called from composite actions like 'up'
registerActionInitializer[*initAction](container, "azd-init-action")
registerActionInitializer[*deployAction](container, "azd-deploy-action")
registerActionInitializer[*infraCreateAction](container, "azd-infra-create-action")
// Required for alias actions like 'provision' and 'down'
registerAction[*infraCreateAction](container, "azd-infra-create-action")
registerAction[*infraDeleteAction](container, "azd-infra-delete-action")
registerActionInitializer[*provisionAction](container, "azd-provision-action")

// Required for alias actions like 'infra create' and 'infra delete'
registerAction[*downAction](container, "azd-down-action")
registerAction[*provisionAction](container, "azd-provision-action")
}
104 changes: 91 additions & 13 deletions cli/azd/cmd/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,40 @@ import (

weikanglim marked this conversation as resolved.
Show resolved Hide resolved
"github.com/azure/azure-dev/cli/azd/cmd/actions"
"github.com/azure/azure-dev/cli/azd/internal"
"github.com/azure/azure-dev/cli/azd/pkg/account"
"github.com/azure/azure-dev/cli/azd/pkg/environment"
"github.com/azure/azure-dev/cli/azd/pkg/environment/azdcontext"
"github.com/azure/azure-dev/cli/azd/pkg/exec"
"github.com/azure/azure-dev/cli/azd/pkg/infra/provisioning"
"github.com/azure/azure-dev/cli/azd/pkg/input"
"github.com/azure/azure-dev/cli/azd/pkg/output"
"github.com/azure/azure-dev/cli/azd/pkg/project"
"github.com/azure/azure-dev/cli/azd/pkg/tools/azcli"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

type downFlags struct {
infraDeleteFlags
forceDelete bool
purgeDelete bool
global *internal.GlobalCommandOptions
envFlag
}

func newDownFlags(cmd *cobra.Command, infraDeleteFlags *infraDeleteFlags, global *internal.GlobalCommandOptions) *downFlags {
func (i *downFlags) Bind(local *pflag.FlagSet, global *internal.GlobalCommandOptions) {
local.BoolVar(&i.forceDelete, "force", false, "Does not require confirmation before it deletes resources.")
local.BoolVar(
&i.purgeDelete,
"purge",
false,
//nolint:lll
"Does not require confirmation before it permanently deletes resources that are soft-deleted by default (for example, key vaults).",
)
i.envFlag.Bind(local, global)
i.global = global
}

func newDownFlags(cmd *cobra.Command, global *internal.GlobalCommandOptions) *downFlags {
flags := &downFlags{}
flags.Bind(cmd.Flags(), global)

Expand All @@ -23,30 +48,83 @@ func newDownFlags(cmd *cobra.Command, infraDeleteFlags *infraDeleteFlags, global

func newDownCmd() *cobra.Command {
return &cobra.Command{
Use: "down",
Short: "Delete Azure resources for an application.",
Aliases: []string{"infra delete"},
Use: "down",
Short: "Delete Azure resources for an application.",
}
}

type downAction struct {
infraDelete *infraDeleteAction
flags *downFlags
accountManager account.Manager
azCli azcli.AzCli
azdCtx *azdcontext.AzdContext
env *environment.Environment
console input.Console
commandRunner exec.CommandRunner
projectConfig *project.ProjectConfig
}

func newDownAction(
downFlags *downFlags,
infraDelete *infraDeleteAction,
flags *downFlags,
accountManager account.Manager,
azCli azcli.AzCli,
azdCtx *azdcontext.AzdContext,
env *environment.Environment,
projectConfig *project.ProjectConfig,
console input.Console,
commandRunner exec.CommandRunner,
) actions.Action {
// Required to ensure the sub action flags are bound correctly to the actions
infraDelete.flags = &downFlags.infraDeleteFlags

return &downAction{
infraDelete: infraDelete,
flags: flags,
accountManager: accountManager,
azCli: azCli,
azdCtx: azdCtx,
env: env,
console: console,
commandRunner: commandRunner,
projectConfig: projectConfig,
}
}

func (a *downAction) Run(ctx context.Context) (*actions.ActionResult, error) {
return a.infraDelete.Run(ctx)
infraManager, err := provisioning.NewManager(
ctx,
a.env,
a.projectConfig.Path,
a.projectConfig.Infra,
a.console.IsUnformatted(),
a.azCli,
a.console,
a.commandRunner,
a.accountManager,
)

if err != nil {
return nil, fmt.Errorf("creating provisioning manager: %w", err)
}

deploymentPlan, err := infraManager.Plan(ctx)
if err != nil {
return nil, fmt.Errorf("planning destroy: %w", err)
}

destroyOptions := provisioning.NewDestroyOptions(a.flags.forceDelete, a.flags.purgeDelete)
destroyResult, err := infraManager.Destroy(ctx, &deploymentPlan.Deployment, destroyOptions)
if err != nil {
return nil, fmt.Errorf("destroying infrastructure: %w", err)
}

// Remove any outputs from the template from the environment since destroying the infrastructure
// invalidated them all.
for outputName := range destroyResult.Outputs {
delete(a.env.Values, outputName)
}

if err := a.env.Save(); err != nil {
return nil, fmt.Errorf("saving environment: %w", err)
}

return nil, nil
}

func getCmdDownHelpDescription(*cobra.Command) string {
Expand Down
6 changes: 2 additions & 4 deletions cli/azd/cmd/infra.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ import (
func infraActions(root *actions.ActionDescriptor) *actions.ActionDescriptor {
group := root.Add("infra", &actions.ActionDescriptorOptions{
Command: &cobra.Command{
Short: "Manage your Azure infrastructure.",
},
GroupingOptions: actions.CommandGroupOptions{
RootLevelHelp: actions.CmdGroupManage,
Short: "Manage your Azure infrastructure.",
Hidden: true,
},
})

Expand Down
Loading