Skip to content

Commit 648c8af

Browse files
committed
cmdget: accept package paths
Fixes #62
1 parent 0e0a004 commit 648c8af

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

cmdget.go

+34-6
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,8 +15,8 @@ import (
1415
)
1516

1617
var getCommand = &Command{
17-
UsageLine: "get [-vcs] [-u] [-f] [module...]",
18-
Short: "start hacking a module",
18+
UsageLine: "get [-vcs] [-u] [-f] [module/package...]",
19+
Short: "start hacking a module or package",
1920
Long: `
2021
The get command checks out Go module dependencies
2122
into a directory where they can be edited.
@@ -61,12 +62,39 @@ func runGet1(args []string) error {
6162
// Perhaps we should be more resilient in that case?
6263
return errors.Notef(err, nil, "cannot get module info")
6364
}
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)
65+
// Args could be package paths or modules.
66+
// Resolve them all to modules, deduplicate, and sort.
67+
resolved := make(map[string]bool)
68+
for _, arg := range args {
69+
if _, ok := mods[arg]; ok {
70+
// module in use
71+
resolved[arg] = true
6872
continue
6973
}
74+
// Either a package path, or an unused module.
75+
// Try to resolve as a package path.
76+
out, err := runCmd(cwd, "go", "list", "-f", "{{with .Module}}{{.Path}}{{end}}", arg)
77+
if err == nil {
78+
// Resolved to a module. Is it in use?
79+
out = strings.TrimSpace(out)
80+
if _, ok := mods[out]; ok {
81+
resolved[out] = true
82+
continue
83+
}
84+
}
85+
// Either an unused module, or an invalid package path,
86+
// or a valid package path that resolves to an unused module,
87+
// or something else.
88+
errorf("module/package %q does not appear to be in use", arg)
89+
}
90+
mpaths := make([]string, 0, len(resolved))
91+
for mpath := range resolved {
92+
mpaths = append(mpaths, mpath)
93+
}
94+
sort.Strings(mpaths)
95+
96+
for _, mpath := range mpaths {
97+
m := mods[mpath] // must be present, by construction (above)
7098
// Early check that we can replace the module, so we don't
7199
// do all the work to check it out only to find we can't
72100
// add the replace directive.

0 commit comments

Comments
 (0)