Compare commits

..

No commits in common. "59b23931272ca6f112a83c1c983ddd324717c395" and "0fdf9c77aebfb9129e21d612fa5917edb0570ffa" have entirely different histories.

7 changed files with 47 additions and 88 deletions

View File

@ -41,7 +41,7 @@ func init() {
rootCmd.PersistentFlags().BoolVarP(&flags.VERBOSE, "verbose", "v", false, "verbose mode") rootCmd.PersistentFlags().BoolVarP(&flags.VERBOSE, "verbose", "v", false, "verbose mode")
rootCmd.PersistentFlags().StringVar(&flags.RESTIC_BIN, "restic-bin", "restic", "specify custom restic binary") rootCmd.PersistentFlags().StringVar(&flags.RESTIC_BIN, "restic-bin", "restic", "specify custom restic binary")
rootCmd.PersistentFlags().StringVar(&flags.DOCKER_IMAGE, "docker-image", "cupcakearmy/autorestic:"+internal.VERSION, "specify a custom docker image") rootCmd.PersistentFlags().StringVar(&flags.DOCKER_IMAGE, "docker-image", "cupcakearmy/autorestic:"+internal.VERSION, "specify a custom docker image")
rootCmd.PersistentFlags().StringVar(&flags.LOCKFILE, "lockfile", "", "specify a custom path for the lockfile (defaults to .autorestic.lock.yml next to the loaded autorestic config file)") rootCmd.PersistentFlags().StringVar(&flags.LOCKFILE_PATH, "lockfile-path", "", "specify a custom path for the lockfile (defaults to .autorestic.lock.yml next to the loaded autorestic config file)")
cobra.OnInitialize(initConfig) cobra.OnInitialize(initConfig)
} }

View File

@ -3,7 +3,6 @@
"quick": "Quick Start", "quick": "Quick Start",
"installation": "Installation", "installation": "Installation",
"config": "Configuration", "config": "Configuration",
"lockfile": "Lockfile",
"location": "Locations", "location": "Locations",
"backend": "Backend", "backend": "Backend",
"cli": "CLI", "cli": "CLI",

View File

@ -35,10 +35,10 @@ With `--restic-bin` you can specify to run a specific restic binary. This can be
autorestic --restic-bin /some/path/to/my/custom/restic/binary autorestic --restic-bin /some/path/to/my/custom/restic/binary
``` ```
## `--lockfile` ## `--lockfile-path`
Specify the path for the [lockfile](../lockfile.md) used by `autorestic`. If omitted, this will default to `.autorestic.lock.yml` next to the loaded config file. Specify the path for the lockfile used by `autorestic`. If omitted, this will default to `.autorestic.lock.yml` next to the loaded config file.
```bash ```bash
autorestic --lockfile /path/to/my/.autorestic.lock.yml autorestic --lockfile-path /path/to/my/.autorestic.lock.yml
``` ```

View File

@ -1,14 +0,0 @@
# Lockfile
Under the hood, `autorestic` uses a lockfile to ensure that only one instance is running and to keep track of when [cronjobs](./location/cron.md) were last run.
By default, the lockfile is stored next to your [configuration file](./config.md) as `.autorestic.lock.yml`. In other words, if your config file is located at `/some/path/.autorestic.yml`, then the lockfile will be located at `/some/path/.autorestic.lock.yml`.
## Customization
The path to the lockfile can be customized if need be. This can be done is a few ways:
1. Using the `--lockfile ...` command line flag
1. Setting `lockfile: ...` in the configuration file
Note that `autorestic` will check for a customized lockfile path in the order listed above. This means that if you specify a lockfile path in multiple places, the method that's higher in the list will win.

View File

@ -1,10 +1,10 @@
package flags package flags
var ( var (
CI bool = false CI bool = false
VERBOSE bool = false VERBOSE bool = false
CRON_LEAN bool = false CRON_LEAN bool = false
RESTIC_BIN string RESTIC_BIN string
DOCKER_IMAGE string DOCKER_IMAGE string
LOCKFILE string LOCKFILE_PATH string
) )

View File

@ -20,14 +20,14 @@ const (
) )
// getLockfilePath returns the path to the lockfile. The path for the lockfile // getLockfilePath returns the path to the lockfile. The path for the lockfile
// can be sources from multiple places If flags.LOCKFILE is set, its value is // can be sources from multiple places If flags.LOCKFILE_PATH is set, its value
// used; if the config has the `lockfile` option set, its value is used; // is used; if the config has the `lockfile` option set, its value is used;
// otherwise the path is generated relative to the config file. // otherwise the path is generated relative to the config file.
func getLockfilePath() string { func getLockfilePath() string {
if flags.LOCKFILE != "" { if flags.LOCKFILE_PATH != "" {
abs, err := filepath.Abs(flags.LOCKFILE) abs, err := filepath.Abs(flags.LOCKFILE_PATH)
if err != nil { if err != nil {
return flags.LOCKFILE return flags.LOCKFILE_PATH
} }
return abs return abs
} }

View File

@ -1,88 +1,62 @@
package internal package internal
import ( import (
"log"
"os" "os"
"os/exec" "os/exec"
"path"
"strconv" "strconv"
"sync"
"testing" "testing"
"github.com/cupcakearmy/autorestic/internal/flags" "github.com/cupcakearmy/autorestic/internal/flags"
"github.com/spf13/viper" "github.com/spf13/viper"
"github.com/stretchr/testify/assert"
) )
var testDirectory = "autorestic_test_tmp"
// All tests must share the same lock file as it is only initialized once // All tests must share the same lock file as it is only initialized once
func setup(t *testing.T) { func setup(t *testing.T) {
t.Helper() d, err := os.MkdirTemp("", testDirectory)
cleanup := func() { if err != nil {
flags.LOCKFILE = "" log.Fatalf("error creating temp dir: %v", err)
config = nil return
once = sync.Once{}
viper.Reset()
} }
// set config file location
cleanup()
d := t.TempDir()
viper.SetConfigFile(d + "/.autorestic.yml") viper.SetConfigFile(d + "/.autorestic.yml")
viper.Set("version", 2)
viper.WriteConfig()
t.Cleanup(cleanup) t.Cleanup(func() {
os.RemoveAll(d)
viper.Reset()
})
} }
func TestGetLockfilePath(t *testing.T) { func TestGetLockfilePath(t *testing.T) {
t.Run("user specified", func(t *testing.T) { t.Run("when flags.LOCKFILE_PATH is set", func(t *testing.T) {
testCases := []struct { flags.LOCKFILE_PATH = "/path/to/my/autorestic.lock.yml"
name string defer func() { flags.LOCKFILE_PATH = "" }()
flag string
config string
expected string
}{
{
name: "flag and config",
flag: "/flag.lock.yml",
config: "/config.lock.yml",
expected: "/flag.lock.yml",
},
{
name: "flag only",
flag: "/flag.lock.yml",
config: "",
expected: "/flag.lock.yml",
},
{
name: "config only",
flag: "",
config: "/config.lock.yml",
expected: "/config.lock.yml",
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
setup(t)
flags.LOCKFILE = testCase.flag
if testCase.config != "" {
viper.Set("lockfile", testCase.config)
err := viper.WriteConfig()
assert.NoError(t, err)
}
p := getLockfilePath() p := getLockfilePath()
assert.Equal(t, testCase.expected, p)
}) if p != "/path/to/my/autorestic.lock.yml" {
t.Errorf("got %v, want %v", p, "/path/to/my/autorestic.lock.yml")
} }
}) })
t.Run("default", func(t *testing.T) { t.Run("when flags.LOCKFILE_PATH is set", func(t *testing.T) {
setup(t) d, err := os.MkdirTemp("", testDirectory)
if err != nil {
log.Fatalf("error creating temp dir: %v", err)
return
}
viper.SetConfigFile(d + "/.autorestic.yml")
defer viper.Reset()
configPath := viper.ConfigFileUsed() flags.LOCKFILE_PATH = ""
expectedLockfile := path.Join(path.Dir(configPath), ".autorestic.lock.yml")
p := getLockfilePath() p := getLockfilePath()
assert.Equal(t, expectedLockfile, p)
if p != d+"/.autorestic.lock.yml" {
t.Errorf("got %v, want %v", p, d+"/.autorestic.lock.yml")
}
}) })
} }