diff --git a/README.md b/README.md index 4b864a5..1195207 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,7 @@ services: - REPLICA1_APIPATH=/some/path/control # - REPLICA1_AUTOSETUP=true # if true, AdGuardHome is automatically initialized. - CRON=*/10 * * * * # run every 10 minutes + - RUNONSTART=true ports: - 8080:8080 restart: unless-stopped @@ -114,6 +115,9 @@ location: $HOME/.adguardhome-sync.yaml # cron expression to run in daemon mode. (default; "" = runs only once) cron: "*/10 * * * *" +# runs the synchronisation on startup +runOnStart: true + origin: # url of the origin instance url: https://192.168.1.2:3000 diff --git a/cmd/root.go b/cmd/root.go index 4bc3230..77c6e6a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -14,7 +14,8 @@ import ( ) const ( - configCron = "cron" + configCron = "cron" + configRunOnStart = "runOnStart" configAPIPort = "api.port" configAPIUsername = "api.username" diff --git a/cmd/run.go b/cmd/run.go index 053e134..d50756b 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -28,6 +28,8 @@ func init() { rootCmd.AddCommand(doCmd) doCmd.PersistentFlags().String("cron", "", "The cron expression to run in daemon mode") _ = viper.BindPFlag(configCron, doCmd.PersistentFlags().Lookup("cron")) + doCmd.PersistentFlags().Bool("runOnStart", true, "Run the sync job on start.") + _ = viper.BindPFlag(configRunOnStart, doCmd.PersistentFlags().Lookup("runOnStart")) doCmd.PersistentFlags().Int("api-port", 8080, "Sync API Port, the API endpoint will be started to enable remote triggering; if 0 port API is disabled.") _ = viper.BindPFlag(configAPIPort, doCmd.PersistentFlags().Lookup("api-port")) doCmd.PersistentFlags().String("api-username", "", "Sync API username") diff --git a/pkg/sync/sync.go b/pkg/sync/sync.go index 635752b..7b08faa 100644 --- a/pkg/sync/sync.go +++ b/pkg/sync/sync.go @@ -51,7 +51,9 @@ func Sync(cfg *types.Config) error { } else { w.cron.Run() } - } else { + } + if cfg.RunOnStart { + l.With("version", version.Version).Info("Run on startup") w.sync() } if cfg.API.Port != 0 { diff --git a/pkg/types/types.go b/pkg/types/types.go index 9d75c13..fa3294c 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -13,11 +13,12 @@ const ( // Config application configuration struct type Config struct { - Origin AdGuardInstance `json:"origin" yaml:"origin"` - Replica AdGuardInstance `json:"replica,omitempty" yaml:"replica,omitempty"` - Replicas []AdGuardInstance `json:"replicas,omitempty" yaml:"replicas,omitempty"` - Cron string `json:"cron,omitempty" yaml:"cron,omitempty"` - API API `json:"api,omitempty" yaml:"api,omitempty"` + Origin AdGuardInstance `json:"origin" yaml:"origin"` + Replica AdGuardInstance `json:"replica,omitempty" yaml:"replica,omitempty"` + Replicas []AdGuardInstance `json:"replicas,omitempty" yaml:"replicas,omitempty"` + Cron string `json:"cron,omitempty" yaml:"cron,omitempty"` + RunOnStart bool `json:"runOnStart,omitempty" yaml:"runOnStart,omitempty"` + API API `json:"api,omitempty" yaml:"api,omitempty"` } // API configuration