5
5
"io/ioutil"
6
6
"os"
7
7
"path/filepath"
8
+ "sort"
8
9
"strings"
9
10
10
11
"gopkg.in/errgo.v2/fmt/errors"
@@ -14,11 +15,13 @@ import (
14
15
)
15
16
16
17
var getCommand = & Command {
17
- UsageLine : "get [-vcs] [-u] [-f] [module...]" ,
18
+ UsageLine : "get [-vcs] [-u] [-f] [module/package ...]" ,
18
19
Short : "start hacking a module" ,
19
20
Long : `
20
21
The get command checks out Go module dependencies
21
22
into a directory where they can be edited.
23
+ If a package is named, its containing module
24
+ is checked out.
22
25
23
26
It uses $GOHACK/<module> as the destination directory,
24
27
or $HOME/gohack/<module> if $GOHACK is empty.
@@ -61,12 +64,39 @@ func runGet1(args []string) error {
61
64
// Perhaps we should be more resilient in that case?
62
65
return errors .Notef (err , nil , "cannot get module info" )
63
66
}
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
68
74
continue
69
75
}
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)
70
100
// Early check that we can replace the module, so we don't
71
101
// do all the work to check it out only to find we can't
72
102
// add the replace directive.
0 commit comments