From cbc7aae19e321f0fb5993a0e69988e3eecc00597 Mon Sep 17 00:00:00 2001 From: Day Date: Sun, 4 Dec 2022 07:28:23 +0800 Subject: [PATCH] refactor: os.LookupEnv (#2708) --- cmd/esbuild/main.go | 7 ++----- internal/logger/logger.go | 17 ++++++----------- pkg/api/api_impl.go | 4 ++-- pkg/cli/cli_impl.go | 24 ++++++++++-------------- 4 files changed, 20 insertions(+), 32 deletions(-) diff --git a/cmd/esbuild/main.go b/cmd/esbuild/main.go index 1208cb916d5..76712ccc8f6 100644 --- a/cmd/esbuild/main.go +++ b/cmd/esbuild/main.go @@ -16,11 +16,8 @@ import ( var helpText = func(colors logger.Colors) string { // Read "NO_COLOR" from the environment. This is a convention that some // software follows. See https://no-color.org/ for more information. - for _, key := range os.Environ() { - if strings.HasPrefix(key, "NO_COLOR=") { - colors = logger.Colors{} - break - } + if _, ok := os.LookupEnv("NO_COLOR"); ok { + colors = logger.Colors{} } return ` diff --git a/internal/logger/logger.go b/internal/logger/logger.go index 2121e8424f9..de3fb0b800b 100644 --- a/internal/logger/logger.go +++ b/internal/logger/logger.go @@ -133,11 +133,8 @@ func isProbablyWindowsCommandPrompt() bool { // because that means we're running in the new Windows Terminal instead. if runtime.GOOS == "windows" { windowsCommandPrompt.isProbablyCMD = true - for _, env := range os.Environ() { - if strings.HasPrefix(env, "WT_SESSION=") { - windowsCommandPrompt.isProbablyCMD = false - break - } + if _, ok := os.LookupEnv("WT_SESSION"); ok { + windowsCommandPrompt.isProbablyCMD = false } } } @@ -261,12 +258,10 @@ var noColorOnce sync.Once func hasNoColorEnvironmentVariable() bool { noColorOnce.Do(func() { - for _, key := range os.Environ() { - // Read "NO_COLOR" from the environment. This is a convention that some - // software follows. See https://no-color.org/ for more information. - if strings.HasPrefix(key, "NO_COLOR=") { - noColorResult = true - } + // Read "NO_COLOR" from the environment. This is a convention that some + // software follows. See https://no-color.org/ for more information. + if _, ok := os.LookupEnv("NO_COLOR"); ok { + noColorResult = true } }) return noColorResult diff --git a/pkg/api/api_impl.go b/pkg/api/api_impl.go index ddab0528c17..4f90e89e7c4 100644 --- a/pkg/api/api_impl.go +++ b/pkg/api/api_impl.go @@ -921,8 +921,8 @@ func printSummary(logOptions logger.OutputOptions, outputFiles []OutputFile, sta // Don't print the time taken by the build if we're running under Yarn 1 // since Yarn 1 always prints its own copy of the time taken by each command - for _, env := range os.Environ() { - if strings.HasPrefix(env, "npm_config_user_agent=") && strings.Contains(env, "yarn/1.") { + if userAgent, ok := os.LookupEnv("npm_config_user_agent"); ok { + if strings.Contains(userAgent, "yarn/1.") { logger.PrintSummary(logOptions.Color, table, nil) return } diff --git a/pkg/cli/cli_impl.go b/pkg/cli/cli_impl.go index add61da1271..75091f88c99 100644 --- a/pkg/cli/cli_impl.go +++ b/pkg/cli/cli_impl.go @@ -1064,20 +1064,16 @@ func runImpl(osArgs []string) int { switch { case buildOptions != nil: - for _, key := range os.Environ() { - // Read the "NODE_PATH" from the environment. This is part of node's - // module resolution algorithm. Documentation for this can be found here: - // https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders - if strings.HasPrefix(key, "NODE_PATH=") { - value := key[len("NODE_PATH="):] - separator := ":" - if fs.CheckIfWindows() { - // On Windows, NODE_PATH is delimited by semicolons instead of colons - separator = ";" - } - buildOptions.NodePaths = splitWithEmptyCheck(value, separator) - break - } + // Read the "NODE_PATH" from the environment. This is part of node's + // module resolution algorithm. Documentation for this can be found here: + // https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders + if value, ok := os.LookupEnv("NODE_PATH"); ok { + separator := ":" + if fs.CheckIfWindows() { + // On Windows, NODE_PATH is delimited by semicolons instead of colons + separator = ";" + } + buildOptions.NodePaths = splitWithEmptyCheck(value, separator) } // Read from stdin when there are no entry points