Skip to content

Commit bf15437

Browse files
author
Bryan C. Mills
committed
all: add go.mod
Copy golang.org/x/build/envutil.Dedup locally to avoid pulling in all of x/build. Commands run: go mod init go mod edit -go=1.11 go mod tidy go list -m all go test ./... Updates golang/go#30228 Change-Id: I0fc24565c08a73db067daf080b46f4aa82a63c41 Reviewed-on: https://go-review.googlesource.com/c/dl/+/167703 Reviewed-by: Dmitri Shuralyov <[email protected]>
1 parent 0c1e558 commit bf15437

File tree

3 files changed

+73
-6
lines changed

3 files changed

+73
-6
lines changed

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module golang.org/dl
2+
3+
go 1.11

internal/version/version.go

+35-6
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ import (
2424
"runtime"
2525
"strings"
2626
"time"
27-
28-
"golang.org/x/build/envutil"
2927
)
3028

3129
func init() {
@@ -62,10 +60,7 @@ func Run(version string) {
6260
if p := os.Getenv("PATH"); p != "" {
6361
newPath += string(filepath.ListSeparator) + p
6462
}
65-
// This envutil.Dedup call is unnecessary when the binary is
66-
// built with Go 1.9+, but keep it around for now until Go 1.8
67-
// is no longer seen in the wild in common distros.
68-
cmd.Env = envutil.Dedup(caseInsensitiveEnv, append(os.Environ(), "GOROOT="+root, "PATH="+newPath))
63+
cmd.Env = dedupEnv(caseInsensitiveEnv, append(os.Environ(), "GOROOT="+root, "PATH="+newPath))
6964
if err := cmd.Run(); err != nil {
7065
// TODO: return the same exit status maybe.
7166
os.Exit(1)
@@ -456,3 +451,37 @@ func (uat userAgentTransport) RoundTrip(r *http.Request) (*http.Response, error)
456451
r.Header.Set("User-Agent", "golang-x-build-version/"+version)
457452
return uat.rt.RoundTrip(r)
458453
}
454+
455+
// dedupEnv returns a copy of env with any duplicates removed, in favor of
456+
// later values.
457+
// Items are expected to be on the normal environment "key=value" form.
458+
// If caseInsensitive is true, the case of keys is ignored.
459+
//
460+
// This function is unnecessary when the binary is
461+
// built with Go 1.9+, but keep it around for now until Go 1.8
462+
// is no longer seen in the wild in common distros.
463+
//
464+
// This is copied verbatim from golang.org/x/build/envutil.Dedup at CL 10301
465+
// (commit a91ae26).
466+
func dedupEnv(caseInsensitive bool, env []string) []string {
467+
out := make([]string, 0, len(env))
468+
saw := map[string]int{} // to index in the array
469+
for _, kv := range env {
470+
eq := strings.Index(kv, "=")
471+
if eq < 1 {
472+
out = append(out, kv)
473+
continue
474+
}
475+
k := kv[:eq]
476+
if caseInsensitive {
477+
k = strings.ToLower(k)
478+
}
479+
if dupIdx, isDup := saw[k]; isDup {
480+
out[dupIdx] = kv
481+
} else {
482+
saw[k] = len(out)
483+
out = append(out, kv)
484+
}
485+
}
486+
return out
487+
}

internal/version/version_test.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2019 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package version
6+
7+
import (
8+
"reflect"
9+
"testing"
10+
)
11+
12+
func TestDedupEnv(t *testing.T) {
13+
tests := []struct {
14+
noCase bool
15+
in []string
16+
want []string
17+
}{
18+
{
19+
noCase: true,
20+
in: []string{"k1=v1", "k2=v2", "K1=v3"},
21+
want: []string{"K1=v3", "k2=v2"},
22+
},
23+
{
24+
noCase: false,
25+
in: []string{"k1=v1", "K1=V2", "k1=v3"},
26+
want: []string{"k1=v3", "K1=V2"},
27+
},
28+
}
29+
for _, tt := range tests {
30+
got := dedupEnv(tt.noCase, tt.in)
31+
if !reflect.DeepEqual(got, tt.want) {
32+
t.Errorf("Dedup(%v, %q) = %q; want %q", tt.noCase, tt.in, got, tt.want)
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)