Use a separate lock file on Windows to protect configuration (#216)
This commit is contained in:
parent
e40ce6d999
commit
9da91fa21a
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/cirruslabs/orchard/internal/netconstants"
|
"github.com/cirruslabs/orchard/internal/netconstants"
|
||||||
"github.com/cirruslabs/orchard/internal/orchardhome"
|
"github.com/cirruslabs/orchard/internal/orchardhome"
|
||||||
"github.com/gofrs/flock"
|
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
@ -66,13 +65,11 @@ func (handle *Handle) SetConfig(config *Config) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handle *Handle) CreateContext(name string, context Context, force bool) error {
|
func (handle *Handle) CreateContext(name string, context Context, force bool) error {
|
||||||
lock := flock.New(handle.configPath)
|
unlock, err := handle.Lock()
|
||||||
if err := lock.Lock(); err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer func() {
|
defer unlock()
|
||||||
_ = lock.Unlock()
|
|
||||||
}()
|
|
||||||
|
|
||||||
config, err := handle.Config()
|
config, err := handle.Config()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -90,13 +87,11 @@ func (handle *Handle) CreateContext(name string, context Context, force bool) er
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handle *Handle) DefaultContext() (Context, error) {
|
func (handle *Handle) DefaultContext() (Context, error) {
|
||||||
lock := flock.New(handle.configPath)
|
unlock, err := handle.Lock()
|
||||||
if err := lock.Lock(); err != nil {
|
if err != nil {
|
||||||
return Context{}, err
|
return Context{}, err
|
||||||
}
|
}
|
||||||
defer func() {
|
defer unlock()
|
||||||
_ = lock.Unlock()
|
|
||||||
}()
|
|
||||||
|
|
||||||
config, err := handle.Config()
|
config, err := handle.Config()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -131,13 +126,11 @@ func (handle *Handle) DefaultContext() (Context, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handle *Handle) SetDefaultContext(name string) error {
|
func (handle *Handle) SetDefaultContext(name string) error {
|
||||||
lock := flock.New(handle.configPath)
|
unlock, err := handle.Lock()
|
||||||
if err := lock.Lock(); err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer func() {
|
defer unlock()
|
||||||
_ = lock.Unlock()
|
|
||||||
}()
|
|
||||||
|
|
||||||
config, err := handle.Config()
|
config, err := handle.Config()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -155,13 +148,11 @@ func (handle *Handle) SetDefaultContext(name string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handle *Handle) DeleteContext(name string) error {
|
func (handle *Handle) DeleteContext(name string) error {
|
||||||
lock := flock.New(handle.configPath)
|
unlock, err := handle.Lock()
|
||||||
if err := lock.Lock(); err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer func() {
|
defer unlock()
|
||||||
_ = lock.Unlock()
|
|
||||||
}()
|
|
||||||
|
|
||||||
config, err := handle.Config()
|
config, err := handle.Config()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
//go:build !windows
|
||||||
|
|
||||||
|
package config
|
||||||
|
|
||||||
|
import "github.com/gofrs/flock"
|
||||||
|
|
||||||
|
func (handle *Handle) Lock() (func(), error) {
|
||||||
|
lock := flock.New(handle.configPath)
|
||||||
|
|
||||||
|
if err := lock.Lock(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return func() {
|
||||||
|
_ = lock.Unlock()
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gofrs/flock"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (handle *Handle) Lock() (func(), error) {
|
||||||
|
lockPath := handle.configPath + ".lock"
|
||||||
|
|
||||||
|
lock := flock.New(lockPath)
|
||||||
|
|
||||||
|
if err := lock.Lock(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return func() {
|
||||||
|
_ = lock.Unlock()
|
||||||
|
_ = os.Remove(lockPath)
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue