Skip to content

Commit

Permalink
azd env set add warning (#4547)
Browse files Browse the repository at this point in the history
* add warn

* update

* update format

* update warning style

* update

* fix

* update message; use WarningMessage

* handle n=1

* fix ci error

---------

Co-authored-by: Wei Lim <[email protected]>
Co-authored-by: Menghua Chen (MSFT) <[email protected]>
Co-authored-by: Menghua Chen (WICRESOFT NORTH AMERICA LTD) <[email protected]>
  • Loading branch information
4 people authored Feb 20, 2025
1 parent 0d89779 commit d10ad41
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 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"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -159,7 +160,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 @@ -168,6 +175,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 newEnvSetSecretFlags(cmd *cobra.Command, global *internal.GlobalCommandOptions) *envSetSecretFlags {
flags := &envSetSecretFlags{}
flags.Bind(cmd.Flags(), global)
Expand Down

0 comments on commit d10ad41

Please sign in to comment.