]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: clean up old readEnvFile code
authorRuss Cox <rsc@golang.org>
Tue, 23 May 2023 17:42:22 +0000 (13:42 -0400)
committerRuss Cox <rsc@golang.org>
Tue, 23 May 2023 18:36:08 +0000 (18:36 +0000)
Now that cfg.CanGetenv does the right thing, we don't need a separate
readEnvFile in 'go env'.

Change-Id: I187c8615ad074ba132516bcf499f82877a32d5e3
Reviewed-on: https://go-review.googlesource.com/c/go/+/497457
Reviewed-by: Bryan Mills <bcmills@google.com>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/cmd/go/internal/envcmd/env.go

index 5241c4ac6b32886d042821cb684f35171e8c541a..74c0b69629baf5d608ed595715cabcdf9bb9270d 100644 (file)
@@ -325,13 +325,12 @@ func runEnvW(args []string) {
                }
        }
        add := make(map[string]string)
-       envFile := readEnvFile()
        for _, arg := range args {
                key, val, found := strings.Cut(arg, "=")
                if !found {
                        base.Fatalf("go: arguments must be KEY=VALUE: invalid argument: %s", arg)
                }
-               if err := checkEnvWrite(key, val, envFile, 'w'); err != nil {
+               if err := checkEnvWrite(key, val); err != nil {
                        base.Fatalf("go: %v", err)
                }
                if _, ok := add[key]; ok {
@@ -362,10 +361,9 @@ func runEnvU(args []string) {
        if len(args) == 0 {
                base.Fatalf("go: 'go env -u' requires an argument")
        }
-       envFile := readEnvFile()
        del := make(map[string]bool)
        for _, arg := range args {
-               if err := checkEnvWrite(arg, "", envFile, 'u'); err != nil {
+               if err := checkEnvWrite(arg, ""); err != nil {
                        base.Fatalf("go: %v", err)
                }
                del[arg] = true
@@ -523,14 +521,7 @@ func getOrigEnv(key string) string {
        return ""
 }
 
-func checkEnvWrite(key, val string, envFile map[string]string, op rune) error {
-       _, inEnvFile := envFile[key]
-
-       // Always OK to delete something in the env file; maybe a different toolchain put it there.
-       if op == 'u' && inEnvFile {
-               return nil
-       }
-
+func checkEnvWrite(key, val string) error {
        switch key {
        case "GOEXE", "GOGCCFLAGS", "GOHOSTARCH", "GOHOSTOS", "GOMOD", "GOWORK", "GOTOOLDIR", "GOVERSION":
                return fmt.Errorf("%s cannot be modified", key)
@@ -540,10 +531,8 @@ func checkEnvWrite(key, val string, envFile map[string]string, op rune) error {
 
        // To catch typos and the like, check that we know the variable.
        // If it's already in the env file, we assume it's known.
-       if !inEnvFile && !cfg.CanGetenv(key) {
-               if _, ok := envFile[key]; !ok {
-                       return fmt.Errorf("unknown go command variable %s", key)
-               }
+       if !cfg.CanGetenv(key) {
+               return fmt.Errorf("unknown go command variable %s", key)
        }
 
        // Some variables can only have one of a few valid values. If set to an
@@ -595,20 +584,6 @@ func checkEnvWrite(key, val string, envFile map[string]string, op rune) error {
        return nil
 }
 
-func readEnvFile() map[string]string {
-       lines := readEnvFileLines(false)
-       m := make(map[string]string)
-       for _, line := range lines {
-               line = strings.TrimRight(line, "\r\n")
-               key := lineToKey(line)
-               if key == "" {
-                       continue
-               }
-               m[key] = string(line[len(key)+len("="):])
-       }
-       return m
-}
-
 func readEnvFileLines(mustExist bool) []string {
        file, err := cfg.EnvFile()
        if file == "" {