Compare commits

...

4 Commits

Author SHA1 Message Date
Boris Bera
6dfff7ed95
Merge 51a8298f2a into 8a773856de 2024-10-21 16:07:41 +02:00
Wez Furlong
8a773856de
Improve error handling in install.sh (#404)
Prior to this change, running the example from the docs without root privs produces this misleading/confusing output that claims that the software was installed when it wasn't:

```console
$ wget -qO - https://raw.githubusercontent.com/cupcakearmy/autorestic/master/install.sh | bash
linux
amd64
/usr/local/bin/autorestic.bz2: Permission denied
bzip2: Can't open input file /usr/local/bin/autorestic.bz2: No such file or directory.
chmod: cannot access '/usr/local/bin/autorestic': No such file or directory
bash: line 49: autorestic: command not found
Successfully installed autorestic
```

With this change, the errors stop the script much earlier and produce this output instead:

```
linux
amd64
/usr/local/bin/autorestic.bz2: Permission denied
```
2024-10-21 16:06:49 +02:00
Boris Bera
51a8298f2a
doc(lockfile): Document --lockfile-path flag 2024-07-02 21:15:00 -04:00
Boris Bera
f6aa45fd6c
feat(lockfile): allow changing the lockfile path 2024-07-02 21:12:26 -04:00
6 changed files with 65 additions and 13 deletions

View File

@ -42,6 +42,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_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

@ -34,3 +34,11 @@ With `--restic-bin` you can specify to run a specific restic binary. This can be
```bash ```bash
autorestic --restic-bin /some/path/to/my/custom/restic/binary autorestic --restic-bin /some/path/to/my/custom/restic/binary
``` ```
## `--lockfile-path`
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
autorestic --lockfile-path /path/to/my/.autorestic.lock.yml
```

View File

@ -1,5 +1,5 @@
#!/bin/bash #!/bin/bash
set -e -o pipefail
shopt -s nocaseglob shopt -s nocaseglob
OUT_FILE=/usr/local/bin/autorestic OUT_FILE=/usr/local/bin/autorestic

View File

@ -6,4 +6,5 @@ var (
CRON_LEAN bool = false CRON_LEAN bool = false
RESTIC_BIN string RESTIC_BIN string
DOCKER_IMAGE string DOCKER_IMAGE string
LOCKFILE_PATH string
) )

View File

@ -18,18 +18,28 @@ const (
RUNNING = "running" RUNNING = "running"
) )
func getLock() *viper.Viper { // getLockfilePath returns the path to the lockfile. If flags.LOCKFILE_PATH is
if lock == nil { // set, its value is used, otherwise the path is generated relative to the
// config file.
once.Do(func() { func getLockfilePath() string {
lock = viper.New() if flags.LOCKFILE_PATH != "" {
lock.SetDefault("running", false) return flags.LOCKFILE_PATH
} else {
p := viper.ConfigFileUsed() p := viper.ConfigFileUsed()
if p == "" { if p == "" {
colors.Error.Println("cannot lock before reading config location") colors.Error.Println("cannot lock before reading config location")
os.Exit(1) os.Exit(1)
} }
file = path.Join(path.Dir(p), ".autorestic.lock.yml") return path.Join(path.Dir(p), ".autorestic.lock.yml")
}
}
func getLock() *viper.Viper {
if lock == nil {
once.Do(func() {
lock = viper.New()
lock.SetDefault("running", false)
file = getLockfilePath()
if !flags.CRON_LEAN { if !flags.CRON_LEAN {
colors.Faint.Println("Using lock:\t", file) colors.Faint.Println("Using lock:\t", file)
} }

View File

@ -7,6 +7,7 @@ import (
"strconv" "strconv"
"testing" "testing"
"github.com/cupcakearmy/autorestic/internal/flags"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -28,6 +29,37 @@ func setup(t *testing.T) {
}) })
} }
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 = "" }()
p := getLockfilePath()
if p != "/path/to/my/autorestic.lock.yml" {
t.Errorf("got %v, want %v", p, "/path/to/my/autorestic.lock.yml")
}
})
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()
flags.LOCKFILE_PATH = ""
p := getLockfilePath()
if p != d+"/.autorestic.lock.yml" {
t.Errorf("got %v, want %v", p, d+"/.autorestic.lock.yml")
}
})
}
func TestLock(t *testing.T) { func TestLock(t *testing.T) {
setup(t) setup(t)