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

azd env set add warning #4547

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
45 changes: 44 additions & 1 deletion cli/azd/cmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"errors"
"fmt"
"io"
"slices"
"strings"

"github.com/azure/azure-dev/cli/azd/cmd/actions"
"github.com/azure/azure-dev/cli/azd/internal"
Expand Down Expand Up @@ -141,7 +143,13 @@ func newEnvSetAction(
}

func (e *envSetAction) Run(ctx context.Context) (*actions.ActionResult, error) {
e.env.DotenvSet(e.args[0], e.args[1])
key := e.args[0]
value := e.args[1]

dotEnv := e.env.Dotenv()
warnKeyCaseConflicts(ctx, e.console, dotEnv, key)

e.env.DotenvSet(key, value)

if err := e.envManager.Save(ctx, e.env); err != nil {
return nil, fmt.Errorf("saving environment: %w", err)
Expand All @@ -150,6 +158,41 @@ func (e *envSetAction) Run(ctx context.Context) (*actions.ActionResult, error) {
return nil, nil
}

// Prints a warning message if there are any case-insensitive conflicts with the provided key
func warnKeyCaseConflicts(
ctx context.Context,
console input.Console,
dotEnv map[string]string,
key string) {
var conflicts []string
for k := range dotEnv {
if strings.EqualFold(k, key) && k != key {
conflicts = append(conflicts, "'"+k+"'")
}
}

if len(conflicts) == 1 {
console.MessageUxItem(ctx,
&ux.WarningMessage{
Description: fmt.Sprintf(
"'%s' already exists as %s. Did you mean to set %s instead?",
key,
conflicts[0],
conflicts[0]),
})
} else if len(conflicts) > 1 {
slices.Sort(conflicts)

console.MessageUxItem(ctx,
&ux.WarningMessage{
Description: fmt.Sprintf(
"'%s' already exists as %s",
key,
ux.ListAsText(conflicts)),
})
}
}

func newEnvSelectCmd() *cobra.Command {
return &cobra.Command{
Use: "select <environment>",
Expand Down
Loading