-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
481 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/bakito/adguardhome-sync/pkg/log" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
homedir "github.com/mitchellh/go-homedir" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
const ( | ||
configOriginURL = "origin.url" | ||
configOriginAPIPath = "origin.apiPath" | ||
configOriginUsername = "origin.username" | ||
configOriginPassword = "origin.password" | ||
configOriginInsecureSkipVerify = "origin.insecureSkipVerify" | ||
|
||
configReplicaURL = "replica.url" | ||
configReplicaAPIPath = "replica.apiPath" | ||
configReplicaUsername = "replica.username" | ||
configReplicaPassword = "replica.password" | ||
configReplicaInsecureSkipVerify = "replica.insecureSkipVerify" | ||
) | ||
|
||
var ( | ||
cfgFile string | ||
logger = log.GetLogger("root") | ||
) | ||
|
||
// rootCmd represents the base command when called without any subcommands | ||
var rootCmd = &cobra.Command{ | ||
Use: "adguardhome-sync", | ||
Short: "Synchronize config from one AdGuardHome instance to another", | ||
} | ||
|
||
// Execute adds all child commands to the root command and sets flags appropriately. | ||
// This is called by main.main(). It only needs to happen once to the rootCmd. | ||
func Execute() { | ||
if err := rootCmd.Execute(); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func init() { | ||
cobra.OnInitialize(initConfig) | ||
|
||
// Here you will define your flags and configuration settings. | ||
// Cobra supports persistent flags, which, if defined here, | ||
// will be global for your application. | ||
|
||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.adguardhome-sync.yaml)") | ||
|
||
// Cobra also supports local flags, which will only run | ||
// when this action is called directly. | ||
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") | ||
} | ||
|
||
// initConfig reads in config file and ENV variables if set. | ||
func initConfig() { | ||
if cfgFile != "" { | ||
// Use config file from the flag. | ||
viper.SetConfigFile(cfgFile) | ||
} else { | ||
// Find home directory. | ||
home, err := homedir.Dir() | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
// Search config in home directory with name ".adguardhome-sync" (without extension). | ||
viper.AddConfigPath(home) | ||
viper.SetConfigName(".adguardhome-sync") | ||
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_", ".", "_")) | ||
} | ||
|
||
viper.AutomaticEnv() // read in environment variables that match | ||
|
||
// If a config file is found, read it in. | ||
if err := viper.ReadInConfig(); err == nil { | ||
logger.Info("Using config file:", viper.ConfigFileUsed()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/bakito/adguardhome-sync/pkg/client" | ||
"github.com/bakito/adguardhome-sync/pkg/log" | ||
"github.com/bakito/adguardhome-sync/pkg/sync" | ||
"github.com/bakito/adguardhome-sync/pkg/types" | ||
"github.com/spf13/viper" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// runCmd represents the run command | ||
var doCmd = &cobra.Command{ | ||
Use: "run", | ||
Short: "Start a synchronisation from origin to replica", | ||
Long: `Synchronizes the configuration form an origin instance to a replica`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
logger = log.GetLogger("root") | ||
cfg := &types.Config{} | ||
if err := viper.Unmarshal(cfg); err != nil { | ||
logger.Error(err) | ||
return | ||
} | ||
|
||
origin, err := client.New(cfg.Origin) | ||
if err != nil { | ||
logger.Error(err) | ||
return | ||
} | ||
replica, err := client.New(cfg.Replica) | ||
if err != nil { | ||
logger.Error(err) | ||
return | ||
} | ||
err = sync.Sync(origin, replica) | ||
if err != nil { | ||
logger.Error(err) | ||
return | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(doCmd) | ||
|
||
doCmd.PersistentFlags().String("origin-url", "", "Origin instance url") | ||
_ = viper.BindPFlag(configOriginURL, doCmd.PersistentFlags().Lookup("origin-url")) | ||
doCmd.PersistentFlags().String("origin-api-path", "/control", "Origin instance API path") | ||
_ = viper.BindPFlag(configOriginAPIPath, doCmd.PersistentFlags().Lookup("origin-api-path")) | ||
doCmd.PersistentFlags().String("origin-username", "", "Origin instance username") | ||
_ = viper.BindPFlag(configOriginUsername, doCmd.PersistentFlags().Lookup("origin-username")) | ||
doCmd.PersistentFlags().String("origin-password", "", "Origin instance password") | ||
_ = viper.BindPFlag(configOriginPassword, doCmd.PersistentFlags().Lookup("origin-password")) | ||
doCmd.PersistentFlags().String("origin-insecure-skip-verify", "", "Enable Origin instance InsecureSkipVerify") | ||
_ = viper.BindPFlag(configOriginInsecureSkipVerify, doCmd.PersistentFlags().Lookup("origin-insecure-skip-verify")) | ||
|
||
doCmd.PersistentFlags().String("replica-url", "", "Replica instance url") | ||
_ = viper.BindPFlag(configReplicaURL, doCmd.PersistentFlags().Lookup("replica-url")) | ||
doCmd.PersistentFlags().String("replica-api-path", "/control", "Replica instance API path") | ||
_ = viper.BindPFlag(configReplicaAPIPath, doCmd.PersistentFlags().Lookup("replica-api-path")) | ||
doCmd.PersistentFlags().String("replica-username", "", "Replica instance username") | ||
_ = viper.BindPFlag(configReplicaUsername, doCmd.PersistentFlags().Lookup("replica-username")) | ||
doCmd.PersistentFlags().String("replica-password", "", "Replica instance password") | ||
_ = viper.BindPFlag(configReplicaPassword, doCmd.PersistentFlags().Lookup("replica-password")) | ||
doCmd.PersistentFlags().String("replica-insecure-skip-verify", "", "Enable Replica instance InsecureSkipVerify") | ||
_ = viper.BindPFlag(configReplicaInsecureSkipVerify, doCmd.PersistentFlags().Lookup("replica-insecure-skip-verify")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.