From f855c12a0f42ed617799c776900f94829a7bca7a Mon Sep 17 00:00:00 2001 From: NanaXiong00 <138103031+NanaXiong00@users.noreply.github.com> Date: Wed, 13 Nov 2024 17:17:03 +0800 Subject: [PATCH 1/6] add warn --- cli/azd/cmd/env.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/cli/azd/cmd/env.go b/cli/azd/cmd/env.go index d9883e32d27..9cdfead7661 100644 --- a/cli/azd/cmd/env.go +++ b/cli/azd/cmd/env.go @@ -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" @@ -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) @@ -150,6 +153,26 @@ 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 duplicateKeys []string + + for existingKey := range env.Dotenv() { + if existingKey == key { + continue + } + if strings.ToLower(existingKey) == lowerKey { + duplicateKeys = append(duplicateKeys, existingKey) + } + } + + if len(duplicateKeys) > 0 { + fmt.Printf("WARN: Key '%v' already exists in the environment variables (case-insensitive match).\n", key) + } +} + func newEnvSelectCmd() *cobra.Command { return &cobra.Command{ Use: "select ", From 793ee5945f2c20de6778eb6b75ef4a4e56c33e3b Mon Sep 17 00:00:00 2001 From: NanaXiong00 <138103031+NanaXiong00@users.noreply.github.com> Date: Wed, 13 Nov 2024 18:17:18 +0800 Subject: [PATCH 2/6] update --- cli/azd/cmd/env.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/cli/azd/cmd/env.go b/cli/azd/cmd/env.go index 9cdfead7661..2ed83f1e243 100644 --- a/cli/azd/cmd/env.go +++ b/cli/azd/cmd/env.go @@ -157,20 +157,16 @@ func (e *envSetAction) Run(ctx context.Context) (*actions.ActionResult, error) { func checkKeyCaseConflict(env *environment.Environment, key string) { lowerKey := strings.ToLower(key) - var duplicateKeys []string - for existingKey := range env.Dotenv() { if existingKey == key { continue } + if strings.ToLower(existingKey) == lowerKey { - duplicateKeys = append(duplicateKeys, existingKey) + fmt.Printf("WARN: The environment variable '%v' already exists in the .env file with a different case.\n", existingKey) + return } } - - if len(duplicateKeys) > 0 { - fmt.Printf("WARN: Key '%v' already exists in the environment variables (case-insensitive match).\n", key) - } } func newEnvSelectCmd() *cobra.Command { From 5ac465dc60184cf6943240a94c88e085cc3a91dc Mon Sep 17 00:00:00 2001 From: NanaXiong00 <138103031+NanaXiong00@users.noreply.github.com> Date: Wed, 13 Nov 2024 18:33:27 +0800 Subject: [PATCH 3/6] update format --- cli/azd/cmd/env.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cli/azd/cmd/env.go b/cli/azd/cmd/env.go index 2ed83f1e243..e69236427bc 100644 --- a/cli/azd/cmd/env.go +++ b/cli/azd/cmd/env.go @@ -163,7 +163,9 @@ func checkKeyCaseConflict(env *environment.Environment, key string) { } if strings.ToLower(existingKey) == lowerKey { - fmt.Printf("WARN: The environment variable '%v' already exists in the .env file with a different case.\n", existingKey) + fmt.Printf( + "WARN: The environment variable '%v' already exists in the .env file with a different case.\n", + existingKey) return } } From 07b689c48c5471bf7d165305f46c9966df136447 Mon Sep 17 00:00:00 2001 From: NanaXiong00 <138103031+NanaXiong00@users.noreply.github.com> Date: Thu, 14 Nov 2024 10:09:43 +0800 Subject: [PATCH 4/6] update warning style --- cli/azd/cmd/env.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cli/azd/cmd/env.go b/cli/azd/cmd/env.go index e69236427bc..6ff0b71cdcf 100644 --- a/cli/azd/cmd/env.go +++ b/cli/azd/cmd/env.go @@ -164,8 +164,9 @@ func checkKeyCaseConflict(env *environment.Environment, key string) { if strings.ToLower(existingKey) == lowerKey { fmt.Printf( - "WARN: The environment variable '%v' already exists in the .env file with a different case.\n", - existingKey) + output.WithWarningFormat( + "WARNING: The environment variable '%v' already exists in the .env file with a different case.\n", + existingKey)) return } } From a22ec2f433c27c67f4d76d2d4724a2d93af5038f Mon Sep 17 00:00:00 2001 From: NanaXiong00 <138103031+NanaXiong00@users.noreply.github.com> Date: Thu, 14 Nov 2024 10:25:07 +0800 Subject: [PATCH 5/6] update --- cli/azd/cmd/env.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/azd/cmd/env.go b/cli/azd/cmd/env.go index 6ff0b71cdcf..e77e00bf860 100644 --- a/cli/azd/cmd/env.go +++ b/cli/azd/cmd/env.go @@ -163,7 +163,7 @@ func checkKeyCaseConflict(env *environment.Environment, key string) { } if strings.ToLower(existingKey) == lowerKey { - fmt.Printf( + fmt.Print( output.WithWarningFormat( "WARNING: The environment variable '%v' already exists in the .env file with a different case.\n", existingKey)) From 2d0e3a16297256368b606ee7e18334891f8175ce Mon Sep 17 00:00:00 2001 From: NanaXiong00 <138103031+NanaXiong00@users.noreply.github.com> Date: Thu, 14 Nov 2024 11:40:01 +0800 Subject: [PATCH 6/6] fix --- cli/azd/cmd/env.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/cli/azd/cmd/env.go b/cli/azd/cmd/env.go index e77e00bf860..f066ee0d07f 100644 --- a/cli/azd/cmd/env.go +++ b/cli/azd/cmd/env.go @@ -156,6 +156,7 @@ func (e *envSetAction) Run(ctx context.Context) (*actions.ActionResult, error) { // 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 { @@ -163,13 +164,17 @@ func checkKeyCaseConflict(env *environment.Environment, key string) { } if strings.ToLower(existingKey) == lowerKey { - fmt.Print( - output.WithWarningFormat( - "WARNING: The environment variable '%v' already exists in the .env file with a different case.\n", - existingKey)) - return + 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", + conflictKeysStr)) + } } func newEnvSelectCmd() *cobra.Command {