Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
53 changes: 46 additions & 7 deletions cli/azd/cmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -697,39 +697,78 @@ func newEnvSetSecretAction(

func newEnvSelectCmd() *cobra.Command {
return &cobra.Command{
Use: "select <environment>",
Use: "select [<environment>]",
Short: "Set the default environment.",
Args: cobra.ExactArgs(1),
Args: cobra.MaximumNArgs(1),
}
}

type envSelectAction struct {
azdCtx *azdcontext.AzdContext
envManager environment.Manager
console input.Console
args []string
}

func newEnvSelectAction(azdCtx *azdcontext.AzdContext, envManager environment.Manager, args []string) actions.Action {
func newEnvSelectAction(
azdCtx *azdcontext.AzdContext,
envManager environment.Manager,
console input.Console,
args []string,
) actions.Action {
return &envSelectAction{
azdCtx: azdCtx,
envManager: envManager,
console: console,
args: args,
}
}

func (e *envSelectAction) Run(ctx context.Context) (*actions.ActionResult, error) {
_, err := e.envManager.Get(ctx, e.args[0])
var environmentName string

// If no argument provided, prompt the user to select an environment
if len(e.args) == 0 {
envs, err := e.envManager.List(ctx)
if err != nil {
return nil, fmt.Errorf("listing environments: %w", err)
}

if len(envs) == 0 {
return nil, fmt.Errorf("no environments found. You can create one with \"azd env new <environment-name>\"")
}

// Build list of environment names
envNames := make([]string, len(envs))
for i, env := range envs {
envNames[i] = env.Name
}

selection, err := e.console.Select(ctx, input.ConsoleOptions{
Message: "Select an environment:",
Options: envNames,
})
if err != nil {
return nil, fmt.Errorf("selecting environment: %w", err)
}

environmentName = envNames[selection]
} else {
environmentName = e.args[0]
}

_, err := e.envManager.Get(ctx, environmentName)
if errors.Is(err, environment.ErrNotFound) {
return nil, fmt.Errorf(
`environment '%s' does not exist. You can create it with "azd env new %s"`,
e.args[0],
e.args[0],
environmentName,
environmentName,
)
} else if err != nil {
return nil, fmt.Errorf("ensuring environment exists: %w", err)
}

if err := e.azdCtx.SetProjectState(azdcontext.ProjectState{DefaultEnvironment: e.args[0]}); err != nil {
if err := e.azdCtx.SetProjectState(azdcontext.ProjectState{DefaultEnvironment: environmentName}); err != nil {
return nil, fmt.Errorf("setting default environment: %w", err)
}

Expand Down
1 change: 1 addition & 0 deletions cli/azd/cmd/testdata/TestFigSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ const completionSpec: Fig.Spec = {
description: 'Set the default environment.',
args: {
name: 'environment',
isOptional: true,
generators: azdGenerators.listEnvironments,
},
},
Expand Down
2 changes: 1 addition & 1 deletion cli/azd/cmd/testdata/TestUsage-azd-env-select.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Set the default environment.

Usage
azd env select <environment> [flags]
azd env select [<environment>] [flags]

Global Flags
-C, --cwd string : Sets the current working directory.
Expand Down
2 changes: 1 addition & 1 deletion cli/azd/extensions/azure.ai.agents/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/google/uuid v1.6.0
github.com/mark3labs/mcp-go v0.41.1
github.com/spf13/cobra v1.10.1
github.com/spf13/pflag v1.0.10
go.yaml.in/yaml/v3 v3.0.4
google.golang.org/protobuf v1.36.10
gopkg.in/yaml.v3 v3.0.1
Expand Down Expand Up @@ -75,7 +76,6 @@ require (
github.com/rivo/uniseg v0.4.7 // indirect
github.com/sethvargo/go-retry v0.3.0 // indirect
github.com/spf13/cast v1.10.0 // indirect
github.com/spf13/pflag v1.0.10 // indirect
github.com/stretchr/testify v1.11.1 // indirect
github.com/theckman/yacspin v0.13.12 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
Expand Down
Loading