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 6 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
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.

conflictKeysStr))
}
}

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