-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathconfig.go
More file actions
128 lines (112 loc) · 4.39 KB
/
config.go
File metadata and controls
128 lines (112 loc) · 4.39 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
package main
import (
"fmt"
"os"
"sync"
"time"
"github.com/gorhill/cronexpr"
"golang.org/x/sys/unix"
"gopkg.in/yaml.v3"
)
const (
DefaultPort = 9129
DefaultCacheDir = "/var/cache/pacoloco"
DefaultTTLUnaccessed = 30
DefaultTTLUnupdated = 200
DefaultDBName = "sqlite-pkg-cache.db"
)
type Repo struct {
URL string `yaml:"url"`
URLs []string `yaml:"urls"`
Mirrorlist string `yaml:"mirrorlist"`
HttpProxy string `yaml:"http_proxy"`
LastMirrorlistCheck time.Time `yaml:"-"`
MirrorlistMutex sync.Mutex `yaml:"-"`
LastModificationTime time.Time `yaml:"-"`
}
type RefreshPeriod struct {
Cron string `yaml:"cron"`
TTLUnaccessed int `yaml:"ttl_unaccessed_in_days"`
TTLUnupdated int `yaml:"ttl_unupdated_in_days"`
}
type Tls struct {
Certificate string `yaml:"cert"`
Key string `yaml:"key"`
}
type Config struct {
CacheDir string `yaml:"cache_dir"`
Address string `yaml:"address"`
Port int `yaml:"port"`
Repos map[string]*Repo `yaml:"repos,omitempty"`
PurgeFilesAfter int `yaml:"purge_files_after"`
DownloadTimeout int `yaml:"download_timeout"`
Prefetch *RefreshPeriod `yaml:"prefetch"`
HttpProxy string `yaml:"http_proxy"`
UserAgent string `yaml:"user_agent"`
LogTimestamp bool `yaml:"set_timestamp_to_logs"`
Tls *Tls `yaml:"tls"`
}
var config *Config
func parseConfig(raw []byte) (*Config, error) {
result := Config{
CacheDir: DefaultCacheDir,
Port: DefaultPort,
}
if err := yaml.Unmarshal(raw, &result); err != nil {
return nil, err
}
// validate config
for name, repo := range result.Repos {
if repo.URL != "" && len(repo.URLs) > 0 {
return nil, fmt.Errorf("repo '%v' specifies both url and urls parameters, please use only one of them", name)
}
if repo.URL != "" && repo.Mirrorlist != "" {
return nil, fmt.Errorf("repo '%v' specifies both url and mirrorlist parameter, please use only one of them", name)
}
if len(repo.URLs) > 0 && repo.Mirrorlist != "" {
return nil, fmt.Errorf("repo '%v' specifies both urls and mirrorlist parameter, please use only one of them", name)
}
if repo.URL == "" && len(repo.URLs) == 0 && repo.Mirrorlist == "" {
return nil, fmt.Errorf("please specify url(s) or mirrorlist for repo '%v'", name)
}
// validate Mirrorlist config
if repo.Mirrorlist != "" && unix.Access(repo.Mirrorlist, unix.R_OK) != nil {
return nil, fmt.Errorf("mirrorlist file %v for repo %v does not exist or isn't readable for userid %v", repo.Mirrorlist, name, os.Getuid())
}
}
if result.PurgeFilesAfter < 10*60 && result.PurgeFilesAfter != 0 {
return nil, fmt.Errorf("'purge_files_after' period is too low (%v) please specify at least 10 minutes", result.PurgeFilesAfter)
}
if unix.Access(result.CacheDir, unix.R_OK|unix.W_OK) != nil {
return nil, fmt.Errorf("directory %v does not exist or isn't writable for userid %v", result.CacheDir, os.Getuid())
}
// validate Prefetch config
if result.Prefetch != nil {
// set default values
if result.Prefetch.TTLUnaccessed == 0 {
result.Prefetch.TTLUnaccessed = DefaultTTLUnaccessed
}
if result.Prefetch.TTLUnupdated == 0 {
result.Prefetch.TTLUnupdated = DefaultTTLUnupdated
}
// check Prefetch config
if result.Prefetch.TTLUnaccessed < 0 {
return nil, fmt.Errorf("'ttl_unaccessed_in_days' value is too low. Please set it to a value greater than 0")
}
if result.Prefetch.TTLUnupdated < 0 {
return nil, fmt.Errorf("'ttl_unupdated_in_days' value is too low. Please set it to a value greater than 0")
}
if _, err := cronexpr.Parse(result.Prefetch.Cron); err != nil {
return nil, fmt.Errorf("invalid cron string (if you don't know how to compose them, there are many online utilities for doing so). Please check https://github.com/gorhill/cronexpr#implementation for documentation")
}
}
if result.Tls != nil {
if unix.Access(result.Tls.Certificate, unix.R_OK) != nil {
return nil, fmt.Errorf("tls cert file %v does not exist or isn't readable for userid %v", result.Tls.Certificate, os.Getuid())
}
if unix.Access(result.Tls.Key, unix.R_OK) != nil {
return nil, fmt.Errorf("tls key file %v does not exist or isn't readable for userid %v", result.Tls.Key, os.Getuid())
}
}
return &result, nil
}