Skip to content

Commit 6b292f3

Browse files
committed
cmdget: accept package paths
DO NOT MERGE The test fails because go-internal's proxy doesn't support .../@v/list requests. Fixes #62
1 parent 0e0a004 commit 6b292f3

File tree

3 files changed

+71
-7
lines changed

3 files changed

+71
-7
lines changed

cmdget.go

+35-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io/ioutil"
66
"os"
77
"path/filepath"
8+
"sort"
89
"strings"
910

1011
"gopkg.in/errgo.v2/fmt/errors"
@@ -14,11 +15,13 @@ import (
1415
)
1516

1617
var getCommand = &Command{
17-
UsageLine: "get [-vcs] [-u] [-f] [module...]",
18+
UsageLine: "get [-vcs] [-u] [-f] [module/package...]",
1819
Short: "start hacking a module",
1920
Long: `
2021
The get command checks out Go module dependencies
2122
into a directory where they can be edited.
23+
If a package is named, its containing module
24+
is checked out.
2225
2326
It uses $GOHACK/<module> as the destination directory,
2427
or $HOME/gohack/<module> if $GOHACK is empty.
@@ -61,12 +64,39 @@ func runGet1(args []string) error {
6164
// Perhaps we should be more resilient in that case?
6265
return errors.Notef(err, nil, "cannot get module info")
6366
}
64-
for _, mpath := range args {
65-
m := mods[mpath]
66-
if m == nil {
67-
errorf("module %q does not appear to be in use", mpath)
67+
// Args could be package paths or modules.
68+
// Resolve them all to modules, deduplicate, and sort.
69+
resolved := make(map[string]bool)
70+
for _, arg := range args {
71+
if _, ok := mods[arg]; ok {
72+
// module in use
73+
resolved[arg] = true
6874
continue
6975
}
76+
// Either a package path, or an unused module.
77+
// Try to resolve as a package path.
78+
out, err := runCmd(cwd, "go", "list", "-f", "{{with .Module}}{{.Path}}{{end}}", arg)
79+
if err == nil {
80+
// Resolved to a module. Is it in use?
81+
out = strings.TrimSpace(out)
82+
if _, ok := mods[out]; ok {
83+
resolved[out] = true
84+
continue
85+
}
86+
}
87+
// Either an unused module, or an invalid package path,
88+
// or a valid package path that resolves to an unused module,
89+
// or something else.
90+
errorf("module/package %q does not appear to be in use", arg)
91+
}
92+
mpaths := make([]string, 0, len(resolved))
93+
for mpath := range resolved {
94+
mpaths = append(mpaths, mpath)
95+
}
96+
sort.Strings(mpaths)
97+
98+
for _, mpath := range mpaths {
99+
m := mods[mpath] // must be present, by construction (above)
70100
// Early check that we can replace the module, so we don't
71101
// do all the work to check it out only to find we can't
72102
// add the replace directive.

testdata/get-pkg.txt

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
cd repo
2+
go get golang.org/x/text
3+
env GOHACK=$WORK/gohack
4+
gohack get golang.org/x/text/language
5+
# Check that golang.org/x/text/language resolved
6+
# to golang.org/x/text by examining stdout.
7+
# Trust the other get tests to ensure that everything else went ok.
8+
stdout '^golang.org/x/text => .*/gohack/golang.org/x/text$'
9+
! stderr .+
10+
11+
gohack undo
12+
! stderr .+
13+
14+
# Check that deduplication works across modules and packages.
15+
gohack get golang.org/x/text golang.org/x/text/language golang.org/x/text/unused
16+
stdout '^golang.org/x/text => .*/gohack/golang.org/x/text$'
17+
! stdout language
18+
! stdout unused
19+
! stderr .+
20+
21+
-- repo/main.go --
22+
package main
23+
import (
24+
"golang.org/x/text/language"
25+
_ "golang.org/x/text/unused"
26+
)
27+
28+
var _ = language.Make
29+
30+
func main() {
31+
}
32+
33+
-- repo/go.mod --
34+
module example.com/repo

testdata/help.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# --help flag produces output to stderr and fails
22
! gohack get --help
3-
stderr '^usage: get \[-vcs] \[-u] \[-f] \[module...]\nRun ''gohack help get'' for details.\n'
3+
stderr '^usage: get \[-vcs] \[-u] \[-f] \[module/package...]\nRun ''gohack help get'' for details.\n'
44
! stdout .+
55

66
gohack help get
7-
stdout '^usage: get \[-vcs] \[-u] \[-f] \[module...]$'
7+
stdout '^usage: get \[-vcs] \[-u] \[-f] \[module/package...]$'
88
! stderr .+

0 commit comments

Comments
 (0)