Compare commits

...

6 Commits

Author SHA1 Message Date
Boris Bera
c5bb423b0b
Merge 51a8298f2a into d39dafaef1 2024-09-21 17:18:50 -04:00
dependabot[bot]
d39dafaef1
Bump golang from 1.22-alpine to 1.23-alpine (#391)
Bumps golang from 1.22-alpine to 1.23-alpine.

---
updated-dependencies:
- dependency-name: golang
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-09-21 22:56:08 +02:00
dependabot[bot]
5a0f7e94f4
Bump restic/restic from 0.17.0 to 0.17.1 (#393)
Bumps restic/restic from 0.17.0 to 0.17.1.

---
updated-dependencies:
- dependency-name: restic/restic
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-09-21 22:55:52 +02:00
Boris Bera
6a60d02759
chore(CI): add basic CI (build & test) for PRs and master pushes (#396) 2024-09-21 22:55:23 +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
7 changed files with 90 additions and 14 deletions

24
.github/workflows/ci.yml vendored Normal file
View File

@ -0,0 +1,24 @@
name: CI
on:
pull_request:
push:
branches: [master]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: '^1.21'
- run: go test -v ./...
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: '^1.21'
- run: go build -v .

View File

@ -1,4 +1,4 @@
FROM golang:1.22-alpine as builder
FROM golang:1.23-alpine as builder
WORKDIR /app
COPY go.* .
@ -6,7 +6,7 @@ RUN go mod download
COPY . .
RUN go build
FROM restic/restic:0.17.0
FROM restic/restic:0.17.1
RUN apk add --no-cache rclone bash curl docker-cli
COPY --from=builder /app/autorestic /usr/bin/autorestic
ENTRYPOINT []

View File

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

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

View File

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

View File

@ -7,6 +7,7 @@ import (
"strconv"
"testing"
"github.com/cupcakearmy/autorestic/internal/flags"
"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) {
setup(t)