Added project files
This commit is contained in:
parent
40615a97f8
commit
be40387f4a
10 changed files with 486 additions and 0 deletions
82
internal/config.go
Normal file
82
internal/config.go
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
package configurator
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Version string `yaml:"version"`
|
||||
SmdHost string `yaml:"smd-host"`
|
||||
SmdPort int `yaml:"smd-port"`
|
||||
AccessToken string `yaml:"access-token"`
|
||||
TemplatePaths map[string]string `yaml:"templates"`
|
||||
}
|
||||
|
||||
func NewConfig() *Config {
|
||||
return &Config{
|
||||
Version: "",
|
||||
SmdHost: "http://127.0.0.1",
|
||||
SmdPort: 27779,
|
||||
TemplatePaths: map[string]string{
|
||||
"dnsmasq": "templates/dhcp/dnsmasq.conf",
|
||||
"syslog": "templates/syslog/",
|
||||
"ansible": "templates/ansible",
|
||||
"powerman": "templates/powerman",
|
||||
"conman": "templates/conman",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func LoadConfig(path string) *Config {
|
||||
var c *Config = NewConfig()
|
||||
file, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
log.Printf("failed to read config file: %v\n", err)
|
||||
return c
|
||||
}
|
||||
err = yaml.Unmarshal(file, &c)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to unmarshal config: %v\n", err)
|
||||
return c
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func (config *Config) SaveConfig(path string) {
|
||||
path = filepath.Clean(path)
|
||||
if path == "" || path == "." {
|
||||
path = "config.yaml"
|
||||
}
|
||||
data, err := yaml.Marshal(config)
|
||||
if err != nil {
|
||||
log.Printf("failed to marshal config: %v\n", err)
|
||||
return
|
||||
}
|
||||
err = os.WriteFile(path, data, os.ModePerm)
|
||||
if err != nil {
|
||||
log.Printf("failed to write default config file: %v\n", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func SaveDefaultConfig(path string) {
|
||||
path = filepath.Clean(path)
|
||||
if path == "" || path == "." {
|
||||
path = "config.yaml"
|
||||
}
|
||||
var c = NewConfig()
|
||||
data, err := yaml.Marshal(c)
|
||||
if err != nil {
|
||||
log.Printf("failed to marshal config: %v\n", err)
|
||||
return
|
||||
}
|
||||
err = os.WriteFile(path, data, os.ModePerm)
|
||||
if err != nil {
|
||||
log.Printf("failed to write default config file: %v\n", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue