forked from github/depstubber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
146 lines (126 loc) · 3.1 KB
/
util.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"runtime/debug"
"strings"
"golang.org/x/tools/go/packages"
)
// removeDot removes a dot from the end of `s`, if it ends with a dot.
func removeDot(s string) string {
if len(s) > 0 && s[len(s)-1] == '.' {
return s[0 : len(s)-1]
}
return s
}
// packageNameOfDir get package import path via dir
func packageNameOfDir(srcDir string) (string, error) {
files, err := ioutil.ReadDir(srcDir)
if err != nil {
log.Fatal(err)
}
var goFilePath string
for _, file := range files {
if !file.IsDir() && strings.HasSuffix(file.Name(), ".go") {
goFilePath = file.Name()
break
}
}
if goFilePath == "" {
return "", fmt.Errorf("go source file not found %s", srcDir)
}
packageImport, err := parsePackageImport(goFilePath, srcDir)
if err != nil {
return "", err
}
return packageImport, nil
}
func printModuleVersion() {
if bi, exists := debug.ReadBuildInfo(); exists {
fmt.Println(bi.Main.Version)
} else {
log.Printf("No version information found. Make sure to use " +
"GO111MODULE=on when running 'go get' in order to use specific " +
"version of the binary.")
}
}
// parseImportPackage get package import path via source file
func parsePackageImport(source, srcDir string) (string, error) {
cfg := &packages.Config{
Mode: packages.NeedName,
Tests: true,
Dir: srcDir,
}
pkgs, err := packages.Load(cfg, "file="+source)
if err != nil {
return "", err
}
if packages.PrintErrors(pkgs) > 0 || len(pkgs) == 0 {
return "", errors.New("loading package failed")
}
packageImport := pkgs[0].PkgPath
// It is illegal to import a _test package.
packageImport = strings.TrimSuffix(packageImport, "_test")
return packageImport, nil
}
func split(s string) []string {
return strings.FieldsFunc(s, func(c rune) bool { return c == ',' })
}
func DirExists(path string) (bool, error) {
return FileExists(path)
}
func FileExists(filepath string) (bool, error) {
_, err := os.Stat(filepath)
if os.IsNotExist(err) {
return false, nil
}
if err == nil {
return true, nil
}
return false, err
}
// CreateFolderIfNotExists creates a folder if it does not exists.
func CreateFolderIfNotExists(name string, perm os.FileMode) error {
_, err := os.Stat(name)
if os.IsNotExist(err) {
return os.MkdirAll(name, perm)
}
return err
}
func MustCreateFolderIfNotExists(path string, perm os.FileMode) {
err := CreateFolderIfNotExists(path, perm)
if err != nil {
panic(fmt.Sprintf("error creating dir %q: %s", path, err))
}
}
func MustCopyFile(src, dst string) {
_, err := copyFile(src, dst)
if err != nil {
log.Fatalf("error copying %q to %q: %s", src, dst, err)
}
}
func copyFile(src, dst string) (int64, error) {
sourceFileStat, err := os.Stat(src)
if err != nil {
return 0, err
}
if !sourceFileStat.Mode().IsRegular() {
return 0, fmt.Errorf("%s is not a regular file", src)
}
source, err := os.Open(src)
if err != nil {
return 0, err
}
defer source.Close()
destination, err := os.Create(dst)
if err != nil {
return 0, err
}
defer destination.Close()
nBytes, err := io.Copy(destination, source)
return nBytes, err
}