remove extra state.Locker assertions

All states are lockers, so get rid of extra asertions.
This commit is contained in:
James Bardin 2017-03-31 17:04:56 -04:00
parent bf6384a163
commit 75458a182d
4 changed files with 7 additions and 34 deletions

View File

@ -102,22 +102,19 @@ func (b *Backend) State(name string) (state.State, error) {
stateMgr = &state.LockDisabled{Inner: stateMgr} stateMgr = &state.LockDisabled{Inner: stateMgr}
} }
// Get the locker, which we know always exists
stateMgrLocker := stateMgr.(state.Locker)
// Grab a lock, we use this to write an empty state if one doesn't // Grab a lock, we use this to write an empty state if one doesn't
// exist already. We have to write an empty state as a sentinel value // exist already. We have to write an empty state as a sentinel value
// so States() knows it exists. // so States() knows it exists.
lockInfo := state.NewLockInfo() lockInfo := state.NewLockInfo()
lockInfo.Operation = "init" lockInfo.Operation = "init"
lockId, err := stateMgrLocker.Lock(lockInfo) lockId, err := stateMgr.Lock(lockInfo)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to lock state in Consul: %s", err) return nil, fmt.Errorf("failed to lock state in Consul: %s", err)
} }
// Local helper function so we can call it multiple places // Local helper function so we can call it multiple places
lockUnlock := func(parent error) error { lockUnlock := func(parent error) error {
if err := stateMgrLocker.Unlock(lockId); err != nil { if err := stateMgr.Unlock(lockId); err != nil {
return fmt.Errorf(strings.TrimSpace(errStateUnlock), lockId, err) return fmt.Errorf(strings.TrimSpace(errStateUnlock), lockId, err)
} }

View File

@ -50,15 +50,10 @@ that no one else is holding a lock.
// Lock locks the given state and outputs to the user if locking // Lock locks the given state and outputs to the user if locking
// is taking longer than the threshold. // is taking longer than the threshold.
func Lock(s state.State, info *state.LockInfo, ui cli.Ui, color *colorstring.Colorize) (string, error) { func Lock(s state.State, info *state.LockInfo, ui cli.Ui, color *colorstring.Colorize) (string, error) {
sl, ok := s.(state.Locker)
if !ok {
return "", nil
}
var lockID string var lockID string
err := slowmessage.Do(LockThreshold, func() error { err := slowmessage.Do(LockThreshold, func() error {
id, err := sl.Lock(info) id, err := s.Lock(info)
lockID = id lockID = id
return err return err
}, func() { }, func() {
@ -77,13 +72,8 @@ func Lock(s state.State, info *state.LockInfo, ui cli.Ui, color *colorstring.Col
// Unlock unlocks the given state and outputs to the user if the // Unlock unlocks the given state and outputs to the user if the
// unlock fails what can be done. // unlock fails what can be done.
func Unlock(s state.State, id string, ui cli.Ui, color *colorstring.Colorize) error { func Unlock(s state.State, id string, ui cli.Ui, color *colorstring.Colorize) error {
sl, ok := s.(state.Locker)
if !ok {
return nil
}
err := slowmessage.Do(LockThreshold, func() error { err := slowmessage.Do(LockThreshold, func() error {
return sl.Unlock(id) return s.Unlock(id)
}, func() { }, func() {
if ui != nil { if ui != nil {
ui.Output(color.Color(UnlockMessage)) ui.Output(color.Color(UnlockMessage))

View File

@ -59,13 +59,6 @@ func (c *UnlockCommand) Run(args []string) int {
return 1 return 1
} }
s, ok := st.(state.Locker)
if !ok {
c.Ui.Error("The remote state backend in use does not support locking, and therefor\n" +
"cannot be unlocked.")
return 1
}
isLocal := false isLocal := false
switch s := st.(type) { switch s := st.(type) {
case *state.BackupState: case *state.BackupState:
@ -103,7 +96,7 @@ func (c *UnlockCommand) Run(args []string) int {
} }
} }
if err := s.Unlock(lockID); err != nil { if err := st.Unlock(lockID); err != nil {
c.Ui.Error(fmt.Sprintf("Failed to unlock state: %s", err)) c.Ui.Error(fmt.Sprintf("Failed to unlock state: %s", err))
return 1 return 1
} }

View File

@ -41,19 +41,12 @@ func (s *BackupState) PersistState() error {
return s.Real.PersistState() return s.Real.PersistState()
} }
// all states get wrapped by BackupState, so it has to be a Locker
func (s *BackupState) Lock(info *LockInfo) (string, error) { func (s *BackupState) Lock(info *LockInfo) (string, error) {
if s, ok := s.Real.(Locker); ok { return s.Real.Lock(info)
return s.Lock(info)
}
return "", nil
} }
func (s *BackupState) Unlock(id string) error { func (s *BackupState) Unlock(id string) error {
if s, ok := s.Real.(Locker); ok { return s.Real.Unlock(id)
return s.Unlock(id)
}
return nil
} }
func (s *BackupState) backup() error { func (s *BackupState) backup() error {