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

Validate environment exists when running azd env select #2471

Merged
merged 2 commits into from
Jul 5, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion cli/azd/cmd/env.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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