restic/cmd/restic/cmd_unlock.go

53 lines
1.0 KiB
Go
Raw Normal View History

package main
2016-09-17 12:36:05 +02:00
import (
2017-06-04 11:16:55 +02:00
"context"
2016-09-17 12:36:05 +02:00
"restic"
"github.com/spf13/cobra"
)
var unlockCmd = &cobra.Command{
Use: "unlock",
Short: "remove locks other processes created",
Long: `
The "unlock" command removes stale locks that have been created by other restic processes.
`,
RunE: func(cmd *cobra.Command, args []string) error {
return runUnlock(unlockOptions, globalOptions)
},
}
2016-09-17 12:36:05 +02:00
// UnlockOptions collects all options for the unlock command.
type UnlockOptions struct {
RemoveAll bool
}
2016-09-17 12:36:05 +02:00
var unlockOptions UnlockOptions
func init() {
2016-09-17 12:36:05 +02:00
cmdRoot.AddCommand(unlockCmd)
unlockCmd.Flags().BoolVar(&unlockOptions.RemoveAll, "remove-all", false, "remove all locks, even non-stale ones")
}
2016-09-17 12:36:05 +02:00
func runUnlock(opts UnlockOptions, gopts GlobalOptions) error {
repo, err := OpenRepository(gopts)
if err != nil {
return err
}
fn := restic.RemoveStaleLocks
2016-09-17 12:36:05 +02:00
if opts.RemoveAll {
fn = restic.RemoveAllLocks
}
2017-06-04 11:16:55 +02:00
err = fn(context.TODO(), repo)
if err != nil {
return err
}
2016-09-17 12:36:05 +02:00
Verbosef("successfully removed locks\n")
return nil
}