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
|
/*
* Copyright (c) 2016-2017 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
// Package config finds and parses snapsync config and blacklist files.
package config
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"net/url"
"os"
"path"
"regexp"
"gopkg.in/ini.v1"
)
// General represents a General section in config file.
// It contains all keys that don't belong to other sections.
type General struct {
Workers int
Timeout string
BlacklistFile string `ini:"Blacklist"`
Editor string
}
// User represents User section in config file.
// It contains all information required to push via ssh.
type User struct {
Username string
}
// Repos represents Repos section in config file.
// It contains all information required to get metadata and sources.
type Repos struct {
LocalMirror string `ini:"Local"`
SourceURL string
TargetURL string
Source string
Target string
SSHURL string
}
// Settings contains all config sections.
type Settings struct {
G General
U User
R Repos
}
var (
systemConfigDir string
userConfigDir string
)
func init() {
systemConfigDir = "/etc/snapsync/"
userConfigDir = os.Getenv("HOME") + "/.snapsync/"
}
func checkTimeout(timeout string) error {
re := regexp.MustCompile("^[0-9]+(s|ms){1}$")
if timeout == "" || re.MatchString(timeout) {
return nil
}
return fmt.Errorf("timeout must be positive number with 's' or 'ms' suffix (got \"%s\")", timeout)
}
func checkSSHUrl(addrStr string) error {
addr, err := url.Parse(addrStr)
if err != nil {
return fmt.Errorf("couldn't parse SSH URL: %s", err)
}
if addr.Scheme != "ssh" {
return fmt.Errorf("SSH URL doesn't begin with ssh://")
}
return nil
}
// LoadSettings reads configuration file and returns Settings
func LoadSettings(fpath string) (*Settings, error) {
cfg, err := ini.InsensitiveLoad(fpath)
if err != nil {
return nil, err
}
s := new(Settings)
err = cfg.Section("General").MapTo(&s.G)
if err != nil {
return nil, err
}
err = cfg.Section("User").MapTo(&s.U)
if err != nil {
return nil, err
}
err = cfg.Section("Repos").MapTo(&s.R)
if err != nil {
return nil, err
}
// check if read settings are correct
err = checkTimeout(s.G.Timeout)
if err != nil {
return nil, err
}
if s.R.LocalMirror != "" && !path.IsAbs(s.R.LocalMirror) {
return nil, fmt.Errorf("local mirror path (%s) is not absolute", s.R.LocalMirror)
}
err = checkSSHUrl(s.R.SSHURL)
if err != nil {
return nil, err
}
return s, nil
}
// FindFPath returns full path of config file.
// Function looks in standard places for configuration file if path wasn't provided.
func FindFPath(file, fallbackName string) (string, error) {
var finfo os.FileInfo
var err error
errfmt := "%s file not found in standard locations"
if file == "" {
// 1st user config directory
file = userConfigDir + fallbackName
finfo, err = os.Stat(file)
if err != nil {
if os.IsNotExist(err) {
// there was no such file in user config
// directory, check in system config directory
file = systemConfigDir + fallbackName
finfo, err = os.Stat(file)
if err != nil {
// file not found
if os.IsNotExist(err) {
err = fmt.Errorf(errfmt, fallbackName)
}
return "", err
}
} else {
// other error
return "", err
}
}
} else {
finfo, err = os.Stat(file)
if err != nil {
return "", err
}
}
if !finfo.Mode().IsRegular() {
return "", fmt.Errorf("file is not regular file")
}
return file, nil
}
// GetBlacklist returns slice with regular expressions
// that should match to repositories paths.
func GetBlacklist(path string) ([]*regexp.Regexp, error) {
if path == "" {
return nil, nil
}
blacklist, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
var ret []*regexp.Regexp
scanner := bufio.NewScanner(bytes.NewReader(blacklist))
for scanner.Scan() {
line, err := regexp.Compile(scanner.Text())
if err != nil {
return nil, err
}
ret = append(ret, line)
}
return ret, nil
}
|