2021-10-04 21:56:44 +02:00
|
|
|
package config
|
|
|
|
|
2021-10-12 23:00:24 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
2021-10-18 21:02:10 +02:00
|
|
|
|
2021-10-12 23:00:24 +02:00
|
|
|
"github.com/qwc/backive/core"
|
2021-10-04 21:56:44 +02:00
|
|
|
|
2021-10-12 23:00:24 +02:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
2021-10-04 21:56:44 +02:00
|
|
|
|
2021-10-12 23:00:24 +02:00
|
|
|
type Configuration struct {
|
|
|
|
Settings Settings `mapstructure:"settings"`
|
|
|
|
Devices Devices `mapstructure:"devices"`
|
|
|
|
Backups Backups `mapstructure:"backups"`
|
2021-10-04 21:56:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Settings struct {
|
2021-10-12 23:00:24 +02:00
|
|
|
SystemMountPoint string `mapstructure:"systemMountPoint"`
|
|
|
|
UserMountPoint string `mapstructure:"userMountPoint"`
|
2021-10-04 21:56:44 +02:00
|
|
|
}
|
|
|
|
|
2021-10-12 23:00:24 +02:00
|
|
|
type Devices map[string]core.Device
|
2021-10-04 22:14:16 +02:00
|
|
|
|
2021-10-12 23:00:24 +02:00
|
|
|
type Backups map[string]core.Backup
|
2021-10-04 22:14:16 +02:00
|
|
|
|
2021-10-12 23:00:24 +02:00
|
|
|
func CreateViper() *viper.Viper {
|
|
|
|
vconfig := viper.New()
|
|
|
|
vconfig.SetConfigName("backive")
|
|
|
|
vconfig.SetConfigType("yaml")
|
|
|
|
vconfig.AddConfigPath("/etc/backive/") // system config
|
|
|
|
vconfig.AddConfigPath("$HOME/.backive/")
|
|
|
|
vconfig.AddConfigPath(".")
|
|
|
|
return vconfig
|
|
|
|
}
|
2021-10-04 21:56:44 +02:00
|
|
|
|
2021-10-12 23:00:24 +02:00
|
|
|
func Load() *Configuration {
|
|
|
|
vconfig := CreateViper()
|
|
|
|
if err := vconfig.ReadInConfig(); err != nil {
|
|
|
|
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
|
|
|
panic(fmt.Errorf("Fatal: No config file could be found!"))
|
|
|
|
}
|
|
|
|
panic(fmt.Errorf("Fatal error config file: %w \n", err))
|
|
|
|
}
|
|
|
|
|
2021-10-18 21:02:10 +02:00
|
|
|
var cfg *Configuration
|
|
|
|
|
|
|
|
err := vconfig.Unmarshal(cfg)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error occured when loading config: %v\n", err)
|
|
|
|
panic("No configuration available!")
|
|
|
|
}
|
2021-10-12 23:00:24 +02:00
|
|
|
//Unmarshal all into Configuration type
|
2021-10-18 21:02:10 +02:00
|
|
|
return cfg
|
2021-10-04 21:56:44 +02:00
|
|
|
}
|