-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathsettings.go
More file actions
145 lines (126 loc) · 3.57 KB
/
Copy pathsettings.go
File metadata and controls
145 lines (126 loc) · 3.57 KB
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
// Package settings stores various googet settings.
package settings
import (
"io/ioutil"
"os"
"path/filepath"
"time"
"github.com/google/googet/v2/system"
"github.com/google/logger"
"gopkg.in/yaml.v3"
)
var (
// RootDir is the googet root directory; set by Initialize.
RootDir string
// Confirm tracks whether we should prompt user; set by Initialize.
Confirm bool
// CacheLife is how long cached indexes are considered valid.
// The default value can be overridden by googet.conf.
CacheLife = 3 * time.Minute
// LockFileMaxAge is the maximum age of a lock file before it's considered stale.
LockFileMaxAge = 24 * time.Hour
// Archs is the list of valid arches for the system.
// Taken from googet.conf, or else derived from runtime.GOARCH.
Archs []string
// ProxyServer is used by the HTTP client; set from googet.conf.
ProxyServer string
// AllowUnsafeURL allows HTTP repos; set from googet.conf.
AllowUnsafeURL bool
// StrictConflicts enables strict enforcement of file ownership conflicts.
StrictConflicts bool
)
// Initialize reads the initial settings.
func Initialize(rootDir string, confirm bool) {
RootDir = rootDir
Confirm = confirm
readConf(ConfFile())
}
// LockFile returns the path to the googet lock file.
func LockFile() string {
return filepath.Join(RootDir, "googet.lock")
}
// StateFile returns the path to the JSON package state.
// DEPRECATED: The state file was replaced by the googet database.
func StateFile() string {
return filepath.Join(RootDir, "googet.state")
}
// DBFile returns the path to the installed package state database.
func DBFile() string {
return filepath.Join(RootDir, "googet.db")
}
// ConfFile returns the path to the googet configuration file.
func ConfFile() string {
return filepath.Join(RootDir, "googet.conf")
}
// LogFile returns the path to the googet log.
func LogFile() string {
return filepath.Join(RootDir, "googet.log")
}
// CacheDir returns the path to the index / package cache.
func CacheDir() string {
return filepath.Join(RootDir, "cache")
}
// RepoDir returns the path to the repo config files.
func RepoDir() string {
return filepath.Join(RootDir, "repos")
}
// conf represents a googet configuration file.
type conf struct {
Archs []string
CacheLife string
LockFileMaxAge string
ProxyServer string
AllowUnsafeURL bool
StrictConflicts bool
}
// unmarshalConfFile returns a conf from a YAML configuration file.
func unmarshalConfFile(filename string) (*conf, error) {
b, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
var cf conf
return &cf, yaml.Unmarshal(b, &cf)
}
// readConf initializes settings based on the configuration file at cf.
func readConf(filename string) {
gc, err := unmarshalConfFile(filename)
if err != nil {
if os.IsNotExist(err) {
gc = &conf{}
} else {
// TODO: if ReadFile fails for some reason other than the file not existing,
// gc is nil and the code below will panic after falling through here.
logger.Errorf("Error unmarshalling conf file: %v", err)
}
}
if gc.Archs != nil {
Archs = gc.Archs
} else {
Archs, err = system.InstallableArchs()
if err != nil {
logger.Fatal(err)
}
}
if gc.CacheLife != "" {
cl, err := time.ParseDuration(gc.CacheLife)
if err != nil {
logger.Error(err)
} else {
CacheLife = cl
}
}
if gc.LockFileMaxAge != "" {
lfma, err := time.ParseDuration(gc.LockFileMaxAge)
if err != nil {
logger.Error(err)
} else {
LockFileMaxAge = lfma
}
}
if gc.ProxyServer != "" {
ProxyServer = gc.ProxyServer
}
AllowUnsafeURL = gc.AllowUnsafeURL
StrictConflicts = gc.StrictConflicts
}