2021-08-15 19:37:11 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-08-25 19:17:19 +00:00
|
|
|
"log"
|
2021-08-15 19:37:11 +00:00
|
|
|
"os"
|
2021-08-25 19:17:19 +00:00
|
|
|
"path/filepath"
|
2021-08-15 19:37:11 +00:00
|
|
|
|
2021-08-25 19:17:19 +00:00
|
|
|
"gitea.nehmer.net/torben/dyndns/service"
|
|
|
|
"gitea.nehmer.net/torben/dyndns/webapi"
|
2021-08-15 19:37:11 +00:00
|
|
|
"github.com/spf13/cobra"
|
2021-08-25 19:17:19 +00:00
|
|
|
"github.com/spf13/viper"
|
2021-08-15 19:37:11 +00:00
|
|
|
)
|
|
|
|
|
2021-08-25 19:17:19 +00:00
|
|
|
var ConfigDir string
|
|
|
|
|
2021-08-15 19:37:11 +00:00
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "dyndns",
|
|
|
|
Short: "Go DynDNS Server",
|
|
|
|
Long: `A DynDNS Server implementation in Go, requires both an
|
|
|
|
NFTable Firewall and a properly configured Bind DNS Server.`,
|
|
|
|
}
|
|
|
|
|
2021-08-25 19:17:19 +00:00
|
|
|
func init() {
|
|
|
|
cobra.OnInitialize(initConfig)
|
|
|
|
|
|
|
|
rootCmd.PersistentFlags().StringVarP(&ConfigDir, "config-dir", "c", ".", "base configuration directory, defaults to .")
|
|
|
|
}
|
|
|
|
|
2021-08-15 19:37:11 +00:00
|
|
|
func Exec() {
|
2021-08-25 19:17:19 +00:00
|
|
|
if err := os.Chdir(ConfigDir); err != nil {
|
|
|
|
log.Fatalf("Could not change to directory %s: %v", ConfigDir, err)
|
|
|
|
}
|
|
|
|
if err := rootCmd.Execute(); err != nil {
|
|
|
|
log.Fatalf("Failed to execute command: %v", err)
|
2021-08-15 19:37:11 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-25 19:17:19 +00:00
|
|
|
|
|
|
|
func sanitizeConfigDir() {
|
|
|
|
var err error
|
|
|
|
if ConfigDir, err = filepath.Abs(ConfigDir); err != nil {
|
|
|
|
cobra.CheckErr(fmt.Sprintf("path is invalid: %v", err))
|
|
|
|
}
|
|
|
|
log.Printf("Using configuration directory %s", ConfigDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
func initConfig() {
|
|
|
|
sanitizeConfigDir()
|
|
|
|
|
|
|
|
viper.SetConfigName("conf.yml")
|
|
|
|
viper.SetConfigType("yaml")
|
|
|
|
viper.AddConfigPath(ConfigDir)
|
|
|
|
|
|
|
|
if err := viper.ReadInConfig(); err != nil {
|
|
|
|
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
|
|
|
log.Println("Could not find configfile, using defaults")
|
|
|
|
} else {
|
|
|
|
cobra.CheckErr(fmt.Sprintf("configuration is invalid: %s", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
viper.Set("ConfigDir", ConfigDir)
|
|
|
|
|
|
|
|
log.Printf("Configuration in use: %v", viper.AllSettings())
|
|
|
|
}
|
|
|
|
|
|
|
|
func setAllConfigDefaults() {
|
|
|
|
service.SetConfigDefaults()
|
|
|
|
webapi.SetConfigDefaults()
|
|
|
|
}
|