From 252968e15e0272b2b8c9d7f28501e3016dc9d131 Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Tue, 23 Nov 2021 12:32:35 +0100 Subject: [PATCH] ensure config is loaded before lock --- CHANGELOG.md | 60 +++++++++++++++++++++++-------------------- cmd/check.go | 1 + cmd/cron.go | 1 + cmd/restore.go | 1 + internal/lock/lock.go | 10 ++++++-- 5 files changed, 43 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1fac099..6563732 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,23 +11,27 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - use official docker image instead of installing rclone every time docker is used. +### Fixed + +- lock file not always next to the config file. + ## [1.5.0] - 2021-11-20 ### Added -- Support for multiple paths -- Improved error handling -- Allow for specific snapshot to be restored -- Docker image +- Support for multiple paths. +- Improved error handling. +- Allow for specific snapshot to be restored. +- Docker image. ### Fixed -- rclone in docker volumes +- rclone in docker volumes. ### Changed -- [Breaking Change] Declaration of docker volumes. See: https://autorestic.vercel.app/migration/1.4_1.5 -- [Breaking Change] Hooks default executing directory now defaults to the config file directory. See: https://autorestic.vercel.app/migration/1.4_1.5 +- [Breaking Change] Declaration of docker volumes. See: https://autorestic.vercel.app/migration/1.4_1.5. +- [Breaking Change] Hooks default executing directory now defaults to the config file directory. See: https://autorestic.vercel.app/migration/1.4_1.5. ## [1.4.1] - 2021-10-31 @@ -39,26 +43,26 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -- Allow specify to specify a backend for location backup -- Global restic flags -- Generic ENV support for backends +- Allow specify to specify a backend for location backup. +- Global restic flags. +- Generic ENV support for backends. ### Changed -- Install now only requires `wget` -- Env variable for the `KEY` has been renamed from `AUTORESTIC_[BACKEND NAME]_KEY` -> `AUTORESTIC_[BACKEND NAME]_RESTIC_PASSWORD` +- Install now only requires `wget`. +- Env variable for the `KEY` has been renamed from `AUTORESTIC_[BACKEND NAME]_KEY` -> `AUTORESTIC_[BACKEND NAME]_RESTIC_PASSWORD`. ### Fixed -- Error handling during upgrade & uninstall +- Error handling during upgrade & uninstall. ## [1.3.0] - 2021-10-26 ### Added -- Pass restic backup metadata as ENV to hooks +- Pass restic backup metadata as ENV to hooks. - Support for `XDG_CONFIG_HOME` and `${HOME}/.config` as default locations for `.autorestic.yaml` file. -- Binary restic flags are now supported +- Binary restic flags are now supported. - Pass encryption keys from env variables or files. ## [1.2.0] - 2021-08-05 @@ -66,12 +70,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Community page -- Support for yaml references and aliases +- Support for yaml references and aliases. ### Fixed -- Better verbose output for hooks -- Better error message for bad formatted configs +- Better verbose output for hooks. +- Better error message for bad formatted configs. ## [1.1.2] - 2021-07-11 @@ -83,24 +87,24 @@ Don't check all backend when running `forget` or `exec` commands. ### Added -- Options for backends +- Options for backends. ## [1.1.0] - 2021-05-06 ### Added -- use custom restic binary -- success & failure hooks +- use custom restic binary. +- success & failure hooks. ### Fixed -- don't skip other locations on failure +- don't skip other locations on failure. ## [1.0.9] - 2021-05-01 ### Fixed -- Validation for docker volumes +- Validation for docker volumes. ## [1.0.8] - 2021-04-28 @@ -123,7 +127,7 @@ Don't check all backend when running `forget` or `exec` commands. ### Added -- Support for rclone +- Support for rclone. ## [1.0.5] - 2021-04-24 @@ -136,17 +140,17 @@ Don't check all backend when running `forget` or `exec` commands. ### Added -- Options to add rest username and password in config +- Options to add rest username and password in config. ### Fixed -- Don't add empty strings when saving config +- Don't add empty strings when saving config. ## [1.0.3] - 2021-04-20 ### Fixed -- Auto upgrade script was not working on linux as linux does not support writing to the binary that is being executed +- Auto upgrade script was not working on linux as linux does not support writing to the binary that is being executed. ## [1.0.2] - 2021-04-20 @@ -162,7 +166,7 @@ Don't check all backend when running `forget` or `exec` commands. ### Added -- Completion command for various shells +- Completion command for various shells. ## [1.0.0] - 2021-04-17 diff --git a/cmd/check.go b/cmd/check.go index 2ff9485..159f341 100644 --- a/cmd/check.go +++ b/cmd/check.go @@ -11,6 +11,7 @@ var checkCmd = &cobra.Command{ Use: "check", Short: "Check if everything is setup", Run: func(cmd *cobra.Command, args []string) { + internal.GetConfig() err := lock.Lock() CheckErr(err) defer lock.Unlock() diff --git a/cmd/cron.go b/cmd/cron.go index 2f80bf7..5ea0c7f 100644 --- a/cmd/cron.go +++ b/cmd/cron.go @@ -11,6 +11,7 @@ var cronCmd = &cobra.Command{ Short: "Run cron job for automated backups", Long: `Intended to be mainly triggered by an automated system like systemd or crontab. For each location checks if a cron backup is due and runs it.`, Run: func(cmd *cobra.Command, args []string) { + internal.GetConfig() internal.CRON_LEAN, _ = cmd.Flags().GetBool("lean") err := lock.Lock() CheckErr(err) diff --git a/cmd/restore.go b/cmd/restore.go index f22a1dd..38df5e0 100644 --- a/cmd/restore.go +++ b/cmd/restore.go @@ -13,6 +13,7 @@ var restoreCmd = &cobra.Command{ Short: "Restore backup for location", Args: cobra.MaximumNArgs(1), Run: func(cmd *cobra.Command, args []string) { + internal.GetConfig() err := lock.Lock() CheckErr(err) defer lock.Unlock() diff --git a/internal/lock/lock.go b/internal/lock/lock.go index 0265142..4cae533 100644 --- a/internal/lock/lock.go +++ b/internal/lock/lock.go @@ -19,8 +19,14 @@ func getLock() *viper.Viper { once.Do(func() { lock = viper.New() lock.SetDefault("running", false) - p := path.Dir(viper.ConfigFileUsed()) - file = path.Join(p, ".autorestic.lock.yml") + p := viper.ConfigFileUsed() + colors.Primary.Println(p) + if p == "" { + colors.Error.Println("cannot lock before reading config location") + os.Exit(1) + } + file = path.Join(path.Dir(p), ".autorestic.lock.yml") + colors.Faint.Println("Using lock:\t", file) lock.SetConfigFile(file) lock.SetConfigType("yml") lock.ReadInConfig()