Skip to content

Commit

Permalink
Validate environment exists when running azd env select (#2471)
Browse files Browse the repository at this point in the history
Also, update the help text for `azd env new` to call out that the new
environment is set as the default environment once it is created.

Fixes #2306
  • Loading branch information
ellismg committed Jul 5, 2023
1 parent 38a93b9 commit 8647e5c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
11 changes: 10 additions & 1 deletion cli/azd/cmd/env.go
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"io"
"os"

"github.com/azure/azure-dev/cli/azd/cmd/actions"
"github.com/azure/azure-dev/cli/azd/internal"
Expand Down Expand Up @@ -158,6 +159,14 @@ func newEnvSelectAction(azdCtx *azdcontext.AzdContext, args []string) actions.Ac
}

func (e *envSelectAction) Run(ctx context.Context) (*actions.ActionResult, error) {
_, err := environment.GetEnvironment(e.azdCtx, e.args[0])
if errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf(`environment '%s' does not exist. You can create it with "azd env new %s"`,
e.args[0], e.args[0])
} else if err != nil {
return nil, fmt.Errorf("ensuring environment exists: %w", err)
}

if err := e.azdCtx.SetDefaultEnvironmentName(e.args[0]); err != nil {
return nil, fmt.Errorf("setting default environment: %w", err)
}
Expand Down Expand Up @@ -247,7 +256,7 @@ func newEnvNewFlags(cmd *cobra.Command, global *internal.GlobalCommandOptions) *
func newEnvNewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "new <environment>",
Short: "Create a new environment.",
Short: "Create a new environment and set it as the default.",
}
cmd.Args = cobra.MaximumNArgs(1)

Expand Down
2 changes: 1 addition & 1 deletion cli/azd/cmd/testdata/TestUsage-azd-env-new.snap
@@ -1,5 +1,5 @@

Create a new environment.
Create a new environment and set it as the default.

Usage
azd env new <environment> [flags]
Expand Down
2 changes: 1 addition & 1 deletion cli/azd/cmd/testdata/TestUsage-azd-env.snap
Expand Up @@ -12,7 +12,7 @@ Usage
Available Commands
get-values : Get all environment values.
list : List environments.
new : Create a new environment.
new : Create a new environment and set it as the default.
refresh : Refresh environment settings by using information from a previous infrastructure provision.
select : Set the default environment.
set : Manage your environment settings.
Expand Down
5 changes: 5 additions & 0 deletions cli/azd/test/functional/env_test.go
Expand Up @@ -100,6 +100,11 @@ func Test_CLI_Env_Management(t *testing.T) {
require.Len(t, environmentList, 2)
requireIsDefault(t, environmentList, envName)

// Verify that trying to select an environment which does not exist fails.
res, err := cli.RunCommand(ctx, "env", "select", "does-not-exist")
require.Error(t, err)
require.Contains(t, res.Stdout, "environment 'does-not-exist' does not exist")

// Verify that running refresh with an explicit env name from an argument and from a flag leads to an error.
_, err = cli.RunCommand(context.Background(), "env", "refresh", "-e", "from-flag", "from-arg")
require.Error(t, err)
Expand Down

0 comments on commit 8647e5c

Please sign in to comment.