intialize ddns-client in CLI endpoint
This commit is contained in:
parent
75afeb3712
commit
74dad011c0
48
cmd/main.go
48
cmd/main.go
|
@ -1 +1,47 @@
|
|||
package cmd
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
ddnsclient "github.com/datahearth/ddns-client"
|
||||
"github.com/datahearth/ddns-client/internal/utils"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var (
|
||||
rootCmd = cobra.Command{
|
||||
Use: "ddns-client",
|
||||
Short: "ddns-client is a dynamic DNS updater with built-in providers",
|
||||
Long: `ddns-client will use a config file to update your A DNS settings periodicly.
|
||||
Checkout the documentation for parameters in the yaml config file.
|
||||
`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
ddnsclient.Start(logger)
|
||||
},
|
||||
}
|
||||
logger = logrus.StandardLogger()
|
||||
)
|
||||
|
||||
func init() {
|
||||
viper.BindEnv("CONFIG_PATH")
|
||||
viper.SetConfigType("yaml")
|
||||
if conf := viper.GetString("CONFIG_PATH"); conf == "" {
|
||||
viper.SetConfigFile("ddns-client.yaml")
|
||||
} else {
|
||||
viper.SetConfigFile(conf)
|
||||
}
|
||||
|
||||
if err := utils.LoadConfig(logger); err != nil {
|
||||
log.Fatalf("failed to load config file: %v\n", err.Error())
|
||||
}
|
||||
|
||||
utils.SetupLogger(logger)
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
logger.WithError(err).Fatalln("failed to execute command")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// LoadConfig will read the yaml config from the viper config path
|
||||
func LoadConfig(log logrus.FieldLogger) error {
|
||||
logger := log.WithFields(logrus.Fields{
|
||||
"pkg": "utils",
|
||||
"component": "config",
|
||||
})
|
||||
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
logger.WithError(err).Errorln(ErrReadConfigFile.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetupLogger setup the root logger
|
||||
func SetupLogger(logger *logrus.Logger) {
|
||||
var (
|
||||
level = logrus.InfoLevel
|
||||
timestamp = true
|
||||
color = true
|
||||
loggerConfig = viper.GetStringMap("logger")
|
||||
)
|
||||
|
||||
if l, ok := loggerConfig["level"]; ok {
|
||||
parsedLevel, err := logrus.ParseLevel(l.(string))
|
||||
if err != nil {
|
||||
level = logrus.InfoLevel
|
||||
}
|
||||
level = parsedLevel
|
||||
}
|
||||
|
||||
if t, ok := loggerConfig["disable-timestamp"]; ok {
|
||||
timestamp = t.(bool)
|
||||
}
|
||||
if c, ok := loggerConfig["disable-color"]; ok {
|
||||
color = c.(bool)
|
||||
}
|
||||
_ = timestamp
|
||||
|
||||
logger.SetLevel(level)
|
||||
logger.SetFormatter(&logrus.TextFormatter{
|
||||
DisableColors: color,
|
||||
ForceColors: true,
|
||||
FullTimestamp: true,
|
||||
DisableTimestamp: timestamp,
|
||||
})
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package utils
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
// ErrReadConfigFile is thrown when viper failed to read config file
|
||||
ErrReadConfigFile = errors.New("failed to read config file")
|
||||
)
|
Loading…
Reference in New Issue