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 6 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
27 changes: 27 additions & 0 deletions 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"
"strings"

"github.com/azure/azure-dev/cli/azd/cmd/actions"
"github.com/azure/azure-dev/cli/azd/internal"
Expand Down Expand Up @@ -142,6 +143,8 @@ func newEnvSetAction(

func (e *envSetAction) Run(ctx context.Context) (*actions.ActionResult, error) {
e.env.DotenvSet(e.args[0], e.args[1])
// Check for case-insensitive key conflicts
checkKeyCaseConflict(e.env, e.args[0])

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

// Check if there are any case-insensitive match conflicts in the environment name
func checkKeyCaseConflict(env *environment.Environment, key string) {
lowerKey := strings.ToLower(key)
var conflictKeys []string

for existingKey := range env.Dotenv() {
if existingKey == key {
continue
}

if strings.ToLower(existingKey) == lowerKey {
conflictKeys = append(conflictKeys, fmt.Sprintf(`"%s"`, existingKey))
}
}

if len(conflictKeys) > 0 {
conflictKeysStr := strings.Join(conflictKeys, " and ")
fmt.Print(
output.WithWarningFormat(
"WARNING: The environment variable %s already exists in the .env file with a different case.\n",
Copy link
Contributor

@weikanglim weikanglim Nov 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's hold off on merging, I'd like to continue with the requirements discussion here before we finalize the changes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@weikanglim ping.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vhvb1989 pushed a change, does this look good to you?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merge merge merge

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just me but the word "instead" is throwing me off a bit since it's printing the same name used in the original command. I think it makes more sense to either omit "instead" or change the phrasing to "Did you mean to set 'foo' instead":

image

Any thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It definitely should be reworded as did you mean to set 'foo' instead?

@vhvb1989 The multiple insensitive cases is still a weird situation :D it almost clearly signals user intention of wanting to set multiple different casings. Still feels as-if the confirmation would be a better approach

or we can just stop printing warning messages for n>1

conflictKeysStr))
}
}

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