-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.go
207 lines (182 loc) · 4.51 KB
/
file.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package mset
import (
"fmt"
"io"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"regexp"
"strings"
)
const (
catalogBaseDir = ".m2"
catalogPathEnvVar = "MSET_CATALOG_PATH" // replace default e.g ~/myDirectory
mavenPathEnvVar = "MSET_MAVEN_PATH"
mavenSettingsFileName = "settings.xml"
catalogDirectoryName = ".mset"
fileSuffix = "-settings.xml"
fileEntryRegexp = `^[a-zA-Z0-9-]*$`
configFileCurrent = ".mcurrent"
)
// check if the file exist
func fileExist(path string) error {
fi, err := pathExist(path)
if err != nil {
return err
}
if fi.IsDir() {
return fmt.Errorf("path '%v' is a directory", path)
}
return nil
}
func directoryExist(path string) error {
fi, err := pathExist(path)
if err != nil {
return err
}
if !fi.IsDir() {
return fmt.Errorf("path '%v' is a file", path)
}
return nil
}
func pathExist(path string) (os.FileInfo, error) {
fi, err := os.Stat(path)
if os.IsNotExist(err) {
return nil, err
}
return fi, nil
}
//
func isValidName(name string) bool {
match, _ := regexp.MatchString(fileEntryRegexp, name)
return match
}
// validName+ 'settings.xml' e.g. danta-settings.xml
func setEntryName(name string) string {
return name + fileSuffix
}
// check if all files in the catalog to check if the name exist
func isNameAlreadyTaken(name string) bool {
m, _ := filesInCatalog()
for k := range m {
if k == name {
return true
}
}
return false
}
// if validName then can be saved in the catalog, checks for the EnvVar
func saveFileToCatalog(name, path string) error {
destination := filepath.Join(getCatalogPath(), setEntryName(name))
return copy(path, destination)
}
// a list of the names in the catalog, files that follow xxxx-settings.xml
func filesInCatalog() (map[string]string, error) {
catalogPath := getCatalogPath()
allFiles, err := ioutil.ReadDir(catalogPath)
files := map[string]string{}
if err != nil {
return files, err
}
for _, f := range allFiles {
if !f.IsDir() {
if strings.HasSuffix(f.Name(), fileSuffix) {
files[strings.TrimSuffix(f.Name(), fileSuffix)] = filepath.Join(catalogPath, f.Name())
}
}
}
return files, nil
}
func findFilePathInCatalog(name string) (string, error) {
files, err := filesInCatalog()
if err != nil {
return "", err
}
if len(files) == 0 {
return "", fmt.Errorf("files not available in catalog")
}
for k, path := range files {
if k == name {
return path, nil
}
}
return "", fmt.Errorf("file not found in catalog")
}
// save current file selected in a .mcurrent, the file is created if not present
func saveCurrentFile(name string) error {
catalogPath := getCatalogPath()
err := os.MkdirAll(catalogPath, 0666)
if err != nil {
return err
}
f, err := os.OpenFile(filepath.Join(catalogPath, name), os.O_RDONLY|os.O_CREATE, 0666)
if err != nil {
return err
}
fmt.Println(f.Name())
return nil
}
// checks if the Envar is set otherwise returns default catalog path
func getCatalogPath() string {
if path := os.Getenv(catalogPathEnvVar); path != "" {
return filepath.Join(path, catalogDirectoryName)
}
return filepath.Join(getUserHomeDir(), catalogBaseDir, catalogDirectoryName)
}
func getMavenPath() string {
if path := os.Getenv(mavenPathEnvVar); path != "" {
return filepath.Join(path, catalogBaseDir)
}
return filepath.Join(getUserHomeDir(), catalogBaseDir)
}
func getUserHomeDir() string {
u, _ := user.Current()
return u.HomeDir
}
// creates the directory .mset in the path
func createCatalogDirectory() error {
return os.MkdirAll(getCatalogPath(), 0755)
}
// copy the src file to dst. Any existing file will be overwritten and will not
// copy file attributes.
func copy(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
if err != nil {
return err
}
return out.Close()
}
func setFileToMavenPath(path string) error {
mavenPath := getMavenPath()
return copy(path, filepath.Join(mavenPath, mavenSettingsFileName))
}
func setNameToCurrent(name string) error {
currentPath := filepath.Join(getCatalogPath(), configFileCurrent)
ioutil.WriteFile(currentPath, []byte(name), 0644)
return nil
}
func getCurrentName() (string, error) {
currentPath := filepath.Join(getCatalogPath(), configFileCurrent)
c, err := ioutil.ReadFile(currentPath)
if err != nil {
return "", err
}
return string(c), nil
}
func removeFile(path string) error {
if err := fileExist(path); err != nil {
return err
}
return os.Remove(path)
}