forked from constabulary/gb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.go
55 lines (50 loc) · 1.28 KB
/
import.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package cmd
import (
"path"
"path/filepath"
"strings"
)
type Context interface {
Srcdirs() []string
AllPackages(string) []string
}
// importPathsNoDotExpansion returns the import paths to use for the given
// command line, but it does no ... expansion.
func importPathsNoDotExpansion(ctx Context, cwd string, args []string) []string {
srcdir, _ := filepath.Rel(ctx.Srcdirs()[0], cwd)
if srcdir == ".." {
srcdir = "."
}
if len(args) == 0 {
args = []string{"..."}
}
var out []string
for _, a := range args {
// Arguments are supposed to be import paths, but
// as a courtesy to Windows developers, rewrite \ to /
// in command-line arguments. Handles .\... and so on.
if filepath.Separator == '\\' {
a = strings.Replace(a, `\`, `/`, -1)
}
if a == "all" || a == "std" {
out = append(out, ctx.AllPackages(a)...)
continue
}
a = path.Join(srcdir, path.Clean(a))
out = append(out, a)
}
return out
}
// importPaths returns the import paths to use for the given command line.
func ImportPaths(ctx Context, cwd string, args []string) []string {
args = importPathsNoDotExpansion(ctx, cwd, args)
var out []string
for _, a := range args {
if strings.Contains(a, "...") {
out = append(out, ctx.AllPackages(a)...)
continue
}
out = append(out, a)
}
return out
}