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