Added project files

This commit is contained in:
David J. Allen 2024-03-07 17:00:39 -07:00
parent 40615a97f8
commit be40387f4a
No known key found for this signature in database
GPG key ID: 717C593FF60A2ACC
10 changed files with 486 additions and 0 deletions

82
internal/config.go Normal file
View 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
}
}