-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathimport.go
62 lines (53 loc) · 1.01 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
56
57
58
59
60
61
62
package main
import (
"fmt"
"go/ast"
"path"
// "path/filepath"
"strings"
)
type Import struct {
name string
path string
last string
isEmbeded bool
isParam bool
}
func createImportWithPath(p string) *Import {
last := path.Base(p)
name := last
if strings.Contains(last, "-") {
lastPieces := strings.Split(last, "-")
name = lastPieces[len(lastPieces)-1]
}
return &Import{
name: name,
path: p,
last: last,
}
}
func createImport(imp *ast.ImportSpec) *Import {
var name string
pth := strings.TrimPrefix(strings.TrimSuffix(imp.Path.Value, "\""), "\"")
last := path.Base(pth)
if n := imp.Name; n == nil {
name = last
} else {
name = n.String()
}
if strings.Contains(name, "-") {
namePieces := strings.Split(name, "-")
name = namePieces[len(namePieces)-1]
}
return &Import{
name: name,
path: pth,
last: last,
}
}
func (i Import) ImportSpec() string {
if i.name == i.last {
return fmt.Sprintf("\"%s\"", i.path)
}
return fmt.Sprintf("%s \"%s\"", i.name, i.path)
}