mirror of
https://github.com/restic/restic.git
synced 2026-01-20 05:43:39 -05:00
Previously the global context was either accessed via gopts.ctx, stored in a local variable and then used within that function or sometimes both. This makes it very hard to follow which ctx or a wrapped version of it reaches which method. Thus just drop the context from the globalOptions struct and pass it explicitly to every command line handler method.
60 lines
1.3 KiB
Go
60 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/restic/restic/internal/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.
|
|
|
|
EXIT STATUS
|
|
===========
|
|
|
|
Exit status is 0 if the command was successful, and non-zero if there was any error.
|
|
`,
|
|
DisableAutoGenTag: true,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return runUnlock(globalCtx(), unlockOptions, globalOptions)
|
|
},
|
|
}
|
|
|
|
// UnlockOptions collects all options for the unlock command.
|
|
type UnlockOptions struct {
|
|
RemoveAll bool
|
|
}
|
|
|
|
var unlockOptions UnlockOptions
|
|
|
|
func init() {
|
|
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
|
|
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
|
|
}
|