restic/cmd/restic/cmd_unlock.go

61 lines
1.3 KiB
Go
Raw Normal View History

package main
2016-09-17 10:36:05 +00:00
import (
"context"
2017-07-24 15:42:25 +00:00
"github.com/restic/restic/internal/restic"
2016-09-17 10:36:05 +00:00
"github.com/spf13/cobra"
)
var unlockCmd = &cobra.Command{
Use: "unlock",
Short: "Remove locks other processes created",
2016-09-17 10:36:05 +00:00
Long: `
The "unlock" command removes stale locks that have been created by other restic processes.
EXIT STATUS
===========
Exit status is 0 if the command was successful, and non-zero if there was any error.
2016-09-17 10:36:05 +00:00
`,
DisableAutoGenTag: true,
2016-09-17 10:36:05 +00:00
RunE: func(cmd *cobra.Command, args []string) error {
2022-10-02 21:24:37 +00:00
return runUnlock(cmd.Context(), unlockOptions, globalOptions)
2016-09-17 10:36:05 +00:00
},
}
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")
}
func runUnlock(ctx context.Context, opts UnlockOptions, gopts GlobalOptions) error {
repo, err := OpenRepository(ctx, gopts)
if err != nil {
return err
}
fn := restic.RemoveStaleLocks
2016-09-17 10:36:05 +00:00
if opts.RemoveAll {
fn = restic.RemoveAllLocks
}
processed, err := fn(ctx, repo)
if err != nil {
return err
}
if processed > 0 {
Verbosef("successfully removed %d locks\n", processed)
}
return nil
}