restic/cmd/restic/cmd_unlock.go

54 lines
1 KiB
Go
Raw Normal View History

package main
2016-09-17 10:36:05 +00:00
import (
2017-06-04 09:16:55 +00:00
"context"
2017-07-23 12:21:03 +00:00
"github.com/restic/restic/internal"
2016-09-17 10:36:05 +00:00
"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 10:36:05 +00:00
// UnlockOptions collects all options for the unlock command.
type UnlockOptions struct {
RemoveAll bool
}
2016-09-17 10:36:05 +00:00
var unlockOptions UnlockOptions
func init() {
2016-09-17 10:36:05 +00:00
cmdRoot.AddCommand(unlockCmd)
unlockCmd.Flags().BoolVar(&unlockOptions.RemoveAll, "remove-all", false, "remove all locks, even non-stale ones")
}
2016-09-17 10:36:05 +00:00
func runUnlock(opts UnlockOptions, gopts GlobalOptions) error {
repo, err := OpenRepository(gopts)
if err != nil {
return err
}
fn := restic.RemoveStaleLocks
2016-09-17 10:36:05 +00:00
if opts.RemoveAll {
fn = restic.RemoveAllLocks
}
2017-06-04 09:16:55 +00:00
err = fn(context.TODO(), repo)
if err != nil {
return err
}
2016-09-17 10:36:05 +00:00
Verbosef("successfully removed locks\n")
return nil
}