first with full handle over rpm
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
7948368573
commit
274ea454dd
50 changed files with 4309 additions and 0 deletions
31
config/config.go
Normal file
31
config/config.go
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
package config
|
||||
|
||||
import "time"
|
||||
|
||||
type Config struct {
|
||||
Server ServerConfig `mapstructure:"server"`
|
||||
DB DBConfig `mapstructure:"db"`
|
||||
DataDir string `mapstructure:"data_dir"`
|
||||
Sync SyncConfig `mapstructure:"sync"`
|
||||
}
|
||||
|
||||
type ServerConfig struct {
|
||||
Host string `mapstructure:"host"`
|
||||
Port int `mapstructure:"port"`
|
||||
}
|
||||
|
||||
type DBConfig struct {
|
||||
Path string `mapstructure:"path"`
|
||||
}
|
||||
|
||||
type SyncConfig struct {
|
||||
Interval string `mapstructure:"interval"`
|
||||
}
|
||||
|
||||
func (s SyncConfig) IntervalDuration() time.Duration {
|
||||
d, err := time.ParseDuration(s.Interval)
|
||||
if err != nil || d <= 0 {
|
||||
return time.Hour
|
||||
}
|
||||
return d
|
||||
}
|
||||
41
config/loader.go
Normal file
41
config/loader.go
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func Load(cfgFile string) (*Config, error) {
|
||||
if cfgFile != "" {
|
||||
viper.SetConfigFile(cfgFile)
|
||||
} else {
|
||||
viper.SetConfigName("clonepack")
|
||||
viper.SetConfigType("yaml")
|
||||
viper.AddConfigPath(".")
|
||||
viper.AddConfigPath("$HOME/.clonepack")
|
||||
viper.AddConfigPath("/etc/clonepack")
|
||||
}
|
||||
|
||||
viper.SetEnvPrefix("CLONEPACK")
|
||||
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||
viper.AutomaticEnv()
|
||||
|
||||
viper.SetDefault("server.host", "0.0.0.0")
|
||||
viper.SetDefault("server.port", 8080)
|
||||
viper.SetDefault("db.path", "./clonepack.db")
|
||||
viper.SetDefault("data_dir", "./data")
|
||||
viper.SetDefault("sync.interval", "1h")
|
||||
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
var cfg Config
|
||||
if err := viper.Unmarshal(&cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &cfg, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue