Skip to content

Commit

Permalink
feat(config): allow specifying lockfile
Browse files Browse the repository at this point in the history
  • Loading branch information
dotboris committed Nov 10, 2024
1 parent 8de8d00 commit 0fdf9c7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
1 change: 1 addition & 0 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Options map[string]OptionMap

type Config struct {
Version string `mapstructure:"version" yaml:"version"`
Lockfile string `mapstructure:"lockfile,omitempty" yaml:"lockfile,omitempty"`
Extras interface{} `mapstructure:"extras" yaml:"extras"`
Locations map[string]Location `mapstructure:"locations" yaml:"locations"`
Backends map[string]Backend `mapstructure:"backends" yaml:"backends"`
Expand Down
34 changes: 24 additions & 10 deletions internal/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package internal
import (
"os"
"path"
"path/filepath"
"sync"

"github.com/cupcakearmy/autorestic/internal/colors"
Expand All @@ -18,20 +19,33 @@ const (
RUNNING = "running"
)

// 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.
// 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;
// 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)
abs, err := filepath.Abs(flags.LOCKFILE_PATH)
if err != nil {
return flags.LOCKFILE_PATH
}
return abs
}

if lockfile := GetConfig().Lockfile; lockfile != "" {
abs, err := filepath.Abs(lockfile)
if err != nil {
return lockfile
}
return path.Join(path.Dir(p), ".autorestic.lock.yml")
return abs
}

p := viper.ConfigFileUsed()
if p == "" {
colors.Error.Println("cannot lock before reading config location")
os.Exit(1)
}
return path.Join(path.Dir(p), ".autorestic.lock.yml")
}

func getLock() *viper.Viper {
Expand Down

0 comments on commit 0fdf9c7

Please sign in to comment.