mirror of
https://github.com/cupcakearmy/autorestic.git
synced 2024-12-22 08:16:25 +00:00
Compare commits
3 Commits
0fdf9c77ae
...
59b2393127
Author | SHA1 | Date | |
---|---|---|---|
|
59b2393127 | ||
|
de0862a3dd | ||
|
c9a1474297 |
@ -41,7 +41,7 @@ func init() {
|
||||
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.DOCKER_IMAGE, "docker-image", "cupcakearmy/autorestic:"+internal.VERSION, "specify a custom docker image")
|
||||
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)")
|
||||
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)")
|
||||
cobra.OnInitialize(initConfig)
|
||||
}
|
||||
|
||||
|
@ -3,6 +3,7 @@
|
||||
"quick": "Quick Start",
|
||||
"installation": "Installation",
|
||||
"config": "Configuration",
|
||||
"lockfile": "Lockfile",
|
||||
"location": "Locations",
|
||||
"backend": "Backend",
|
||||
"cli": "CLI",
|
||||
|
@ -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
|
||||
```
|
||||
|
||||
## `--lockfile-path`
|
||||
## `--lockfile`
|
||||
|
||||
Specify the path for the lockfile used by `autorestic`. If omitted, this will default to `.autorestic.lock.yml` next to the loaded config file.
|
||||
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.
|
||||
|
||||
```bash
|
||||
autorestic --lockfile-path /path/to/my/.autorestic.lock.yml
|
||||
autorestic --lockfile /path/to/my/.autorestic.lock.yml
|
||||
```
|
||||
|
14
docs/pages/lockfile.md
Normal file
14
docs/pages/lockfile.md
Normal file
@ -0,0 +1,14 @@
|
||||
# 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.
|
@ -1,10 +1,10 @@
|
||||
package flags
|
||||
|
||||
var (
|
||||
CI bool = false
|
||||
VERBOSE bool = false
|
||||
CRON_LEAN bool = false
|
||||
RESTIC_BIN string
|
||||
DOCKER_IMAGE string
|
||||
LOCKFILE_PATH string
|
||||
CI bool = false
|
||||
VERBOSE bool = false
|
||||
CRON_LEAN bool = false
|
||||
RESTIC_BIN string
|
||||
DOCKER_IMAGE string
|
||||
LOCKFILE string
|
||||
)
|
||||
|
@ -20,14 +20,14 @@ const (
|
||||
)
|
||||
|
||||
// getLockfilePath returns the path to the lockfile. The path for the lockfile
|
||||
// can be sources from multiple places If flags.LOCKFILE_PATH is set, its value
|
||||
// is used; if the config has the `lockfile` option set, its value is used;
|
||||
// can be sources from multiple places If flags.LOCKFILE is set, its value is
|
||||
// used; if the config has the `lockfile` option set, its value is used;
|
||||
// otherwise the path is generated relative to the config file.
|
||||
func getLockfilePath() string {
|
||||
if flags.LOCKFILE_PATH != "" {
|
||||
abs, err := filepath.Abs(flags.LOCKFILE_PATH)
|
||||
if flags.LOCKFILE != "" {
|
||||
abs, err := filepath.Abs(flags.LOCKFILE)
|
||||
if err != nil {
|
||||
return flags.LOCKFILE_PATH
|
||||
return flags.LOCKFILE
|
||||
}
|
||||
return abs
|
||||
}
|
||||
|
@ -1,62 +1,88 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"strconv"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/cupcakearmy/autorestic/internal/flags"
|
||||
"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
|
||||
func setup(t *testing.T) {
|
||||
d, err := os.MkdirTemp("", testDirectory)
|
||||
if err != nil {
|
||||
log.Fatalf("error creating temp dir: %v", err)
|
||||
return
|
||||
}
|
||||
// set config file location
|
||||
viper.SetConfigFile(d + "/.autorestic.yml")
|
||||
|
||||
t.Cleanup(func() {
|
||||
os.RemoveAll(d)
|
||||
t.Helper()
|
||||
cleanup := func() {
|
||||
flags.LOCKFILE = ""
|
||||
config = nil
|
||||
once = sync.Once{}
|
||||
viper.Reset()
|
||||
})
|
||||
}
|
||||
|
||||
cleanup()
|
||||
d := t.TempDir()
|
||||
viper.SetConfigFile(d + "/.autorestic.yml")
|
||||
viper.Set("version", 2)
|
||||
viper.WriteConfig()
|
||||
|
||||
t.Cleanup(cleanup)
|
||||
}
|
||||
|
||||
func TestGetLockfilePath(t *testing.T) {
|
||||
t.Run("when flags.LOCKFILE_PATH is set", func(t *testing.T) {
|
||||
flags.LOCKFILE_PATH = "/path/to/my/autorestic.lock.yml"
|
||||
defer func() { flags.LOCKFILE_PATH = "" }()
|
||||
t.Run("user specified", func(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
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()
|
||||
|
||||
if p != "/path/to/my/autorestic.lock.yml" {
|
||||
t.Errorf("got %v, want %v", p, "/path/to/my/autorestic.lock.yml")
|
||||
p := getLockfilePath()
|
||||
assert.Equal(t, testCase.expected, p)
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("when flags.LOCKFILE_PATH is set", func(t *testing.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()
|
||||
t.Run("default", func(t *testing.T) {
|
||||
setup(t)
|
||||
|
||||
flags.LOCKFILE_PATH = ""
|
||||
configPath := viper.ConfigFileUsed()
|
||||
expectedLockfile := path.Join(path.Dir(configPath), ".autorestic.lock.yml")
|
||||
|
||||
p := getLockfilePath()
|
||||
|
||||
if p != d+"/.autorestic.lock.yml" {
|
||||
t.Errorf("got %v, want %v", p, d+"/.autorestic.lock.yml")
|
||||
}
|
||||
assert.Equal(t, expectedLockfile, p)
|
||||
})
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user