pkg/config/config.go

152 lines
2.2 KiB
Go
Raw Normal View History

2018-06-12 09:52:20 +00:00
package config
import (
"fmt"
2018-07-06 23:08:42 +00:00
"io"
2018-06-12 09:52:20 +00:00
"os"
yaml "gopkg.in/yaml.v2"
)
2018-07-06 23:08:42 +00:00
// Load a config from a file.
func Load(file string) (config *Config, err error) {
f, err := os.Open(file)
if err != nil {
return
}
defer f.Close()
config, err = Read(f)
if err != nil {
return nil, fmt.Errorf("failed to parse %s: %v", file, err)
}
return
}
// Read a config from a reader.
func Read(reader io.Reader) (config *Config, err error) {
config = &Config{}
err = yaml.NewDecoder(reader).Decode(config)
if err != nil {
return nil, err
}
return
}
// Parse the config in data.
func Parse(data []byte) (config *Config, err error) {
config = &Config{}
err = yaml.Unmarshal(data, config)
if err != nil {
return nil, err
}
return
}
2018-06-12 09:52:20 +00:00
// Config represent this system's configuration
type Config struct {
Vars []VarDefault
Layers []string
Modules []string
RootUser struct {
PasswordHash string `yaml:"password_hash"`
AuthorizedKeys []string `yaml:"authorized_keys"`
} `yaml:"root_user"`
Storage StorageConfig
2022-02-21 17:13:54 +00:00
Mounts []MountDef
2022-03-31 12:49:29 +00:00
VPNs []VPNDef
2022-02-21 17:13:54 +00:00
2018-06-12 09:52:20 +00:00
Groups []GroupDef
Users []UserDef
Files []FileDef
Networks []NetworkDef
}
type VarDefault struct {
Name string
Default string
}
type StorageConfig struct {
UdevMatch string `yaml:"udev_match"`
RemoveVolumes []string `yaml:"remove_volumes"`
Volumes []VolumeDef
}
type VolumeDef struct {
Name string
Size string
Extents string
FS string
Mount struct {
Path string
Options string
}
}
2022-02-21 17:13:54 +00:00
type MountDef struct {
2022-02-21 17:13:54 +00:00
Dev string
Path string
Options string
}
2022-03-31 12:49:29 +00:00
type VPNDef struct {
Name string `yaml:"name"`
IPs []string
Config string
}
2018-06-12 09:52:20 +00:00
type GroupDef struct {
Name string
Gid int
}
type UserDef struct {
Name string
Gid int
Uid int
}
type FileDef struct {
Path string
Mode os.FileMode
Content string
}
type NetworkDef struct {
Match struct {
All bool
Name string
Ping *struct {
Source string
Target string
Count int
Timeout int
}
}
Optional bool
Script string
}
2022-03-30 10:13:41 +00:00
func (c *Config) FileContent(filePath string) []byte {
for _, f := range c.Files {
if f.Path == filePath {
return []byte(f.Content)
}
}
return nil
}