forked from aquasecurity/go-dep-parser
/
settings.go
44 lines (36 loc) · 977 Bytes
/
settings.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
package pom
import (
"encoding/xml"
"os"
"path/filepath"
"golang.org/x/net/html/charset"
)
type settings struct {
LocalRepository string `xml:"localRepository"`
}
func readSettings() settings {
userSettingsPath := filepath.Join(os.Getenv("HOME"), ".m2", "settings.xml")
userSettings, err := openSettings(userSettingsPath)
if err == nil && userSettings.LocalRepository != "" {
return userSettings
}
globalSettingsPath := filepath.Join(os.Getenv("MAVEN_HOME"), "conf", "settings.xml")
globalSettings, err := openSettings(globalSettingsPath)
if err == nil && globalSettings.LocalRepository != "" {
return globalSettings
}
return settings{}
}
func openSettings(filePath string) (settings, error) {
f, err := os.Open(filePath)
if err != nil {
return settings{}, err
}
s := settings{}
decoder := xml.NewDecoder(f)
decoder.CharsetReader = charset.NewReaderLabel
if err = decoder.Decode(&s); err != nil {
return settings{}, err
}
return s, nil
}