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,8 +15,8 @@ import (
14
15
)
15
16
16
17
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 " ,
19
20
Long : `
20
21
The get command checks out Go module dependencies
21
22
into a directory where they can be edited.
@@ -61,12 +62,39 @@ func runGet1(args []string) error {
61
62
// Perhaps we should be more resilient in that case?
62
63
return errors .Notef (err , nil , "cannot get module info" )
63
64
}
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
68
72
continue
69
73
}
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)
70
98
// Early check that we can replace the module, so we don't
71
99
// do all the work to check it out only to find we can't
72
100
// add the replace directive.
0 commit comments