{
// user paths
homedir, _ := os.UserHomeDir()
viper.AddConfigPath(homedir + "/.config/abroot/")
// dev paths
viper.AddConfigPath("config/")
viper.AddConfigPath("../config/")
// prod paths
viper.AddConfigPath("/etc/abroot/")
viper.AddConfigPath("/usr/share/abroot/")
viper.SetConfigName("abroot")
viper.SetConfigType("json")
// VanillaOS specific defaults for backwards compatibility
viper.SetDefault("updateInitramfsCmd", "lpkg --unlock && /usr/sbin/update-initramfs -u && lpkg --lock")
viper.SetDefault("updateGrubCmd", "/usr/sbin/grub-mkconfig -o '%s'")
err := viper.ReadInConfig()
if err != nil {
return
}
CnfFileUsed = viper.ConfigFileUsed()
Cnf = &Config{
// Common
MaxParallelDownloads: viper.GetUint("maxParallelDownloads"),
// Registry
Registry: viper.GetString("registry"),
RegistryAPIVersion: viper.GetString("registryAPIVersion"),
RegistryService: viper.GetString("registryService"),
Name: viper.GetString("name"),
Tag: viper.GetString("tag"),
// Package manager
IPkgMngPre: viper.GetString("iPkgMngPre"),
IPkgMngPost: viper.GetString("iPkgMngPost"),
IPkgMngAdd: viper.GetString("iPkgMngAdd"),
IPkgMngRm: viper.GetString("iPkgMngRm"),
IPkgMngApi: viper.GetString("iPkgMngApi"),
IPkgMngStatus: viper.GetInt("iPkgMngStatus"),
// Boot configuration commands
UpdateInitramfsCmd: viper.GetString("updateInitramfsCmd"),
UpdateGrubCmd: viper.GetString("updateGrubCmd"),
// Package diff API (Differ)
DifferURL: viper.GetString("differURL"),
// Partitions
PartLabelVar: viper.GetString("partLabelVar"),
PartLabelA: viper.GetString("partLabelA"),
PartLabelB: viper.GetString("partLabelB"),
PartLabelBoot: viper.GetString("partLabelBoot"),
PartLabelEfi: viper.GetString("partLabelEfi"),
PartCryptVar: viper.GetString("PartCryptVar"),
// Structure
ThinProvisioning: viper.GetBool("thinProvisioning"),
ThinInitVolume: viper.GetString("thinInitVolume"),
// Virtual
FullImageName: "",
}
Cnf.FullImageName = fmt.Sprintf("%s/%s:%s", Cnf.Registry, Cnf.Name, Cnf.Tag)
}
WriteConfigToFile writes the current configuration to a file
{
jsonOutput, err := json.MarshalIndent(Cnf, "", " ")
if err != nil {
return err
}
outputFile, err := os.OpenFile(file, os.O_WRONLY|os.O_TRUNC, 0o644)
if err != nil {
return err
}
_, err = outputFile.Write(jsonOutput)
return err
}
import "encoding/json"
import "fmt"
import "os"
import "github.com/spf13/viper"