/
config.go
86 lines (75 loc) · 2.43 KB
/
config.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
//
// Copyright © 2016-2017 Ikey Doherty <ikey@solus-project.com>
//
// 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 builder
import (
"fmt"
"github.com/BurntSushi/toml"
"io/ioutil"
"os"
"path/filepath"
)
// Config defines the global defaults for solbuild
type Config struct {
DefaultProfile string `toml:"default_profile"` // Name of the default profile to use
EnableTmpfs bool `toml:"enable_tmpfs"` // Whether to enable tmpfs builds or
TmpfsSize string `toml:"tmpfs_size"` // Bounding size on the tmpfs
}
var (
// ConfigPaths is a set of locations for valid solbuild configuration files
ConfigPaths = []string{
"/etc/solbuild",
"/usr/share/solbuild",
}
// ConfigSuffix is the suffix a file must have to be glob loaded by solbuild
ConfigSuffix = ".conf"
)
// NewConfig will read all the system config files and then the vendor config files
// until it gets somewhere.
func NewConfig() (*Config, error) {
// Set up some sane defaults just in case someone mangles the configs
config := &Config{
DefaultProfile: "main-x86_64",
EnableTmpfs: false,
TmpfsSize: "",
}
// Reverse because /etc takes precedence in stateless
for i := len(ConfigPaths) - 1; i >= 0; i-- {
globPat := filepath.Join(ConfigPaths[i], fmt.Sprintf("*%s", ConfigSuffix))
configs, _ := filepath.Glob(globPat)
// Load all globbed configs, using the same Config instance, to keep
// setting the new flags/etc/
for _, p := range configs {
// Read the config file
fi, err := os.Open(p)
if err != nil {
return nil, err
}
var b []byte
// We don't defer the close because of the amount of files we could
// potentially glob & open, we don't want to take the piss with open
// file descriptors.
if b, err = ioutil.ReadAll(fi); err != nil {
fi.Close()
return nil, err
}
fi.Close()
if _, err = toml.Decode(string(b), config); err != nil {
return nil, err
}
}
}
return config, nil
}