github.com/fawick/restic@v0.1.1-0.20171126184616-c02923fbfc79/cmd/restic/cmd_unlock.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/restic/restic/internal/restic"
     7  	"github.com/spf13/cobra"
     8  )
     9  
    10  var unlockCmd = &cobra.Command{
    11  	Use:   "unlock",
    12  	Short: "Remove locks other processes created",
    13  	Long: `
    14  The "unlock" command removes stale locks that have been created by other restic processes.
    15  `,
    16  	DisableAutoGenTag: true,
    17  	RunE: func(cmd *cobra.Command, args []string) error {
    18  		return runUnlock(unlockOptions, globalOptions)
    19  	},
    20  }
    21  
    22  // UnlockOptions collects all options for the unlock command.
    23  type UnlockOptions struct {
    24  	RemoveAll bool
    25  }
    26  
    27  var unlockOptions UnlockOptions
    28  
    29  func init() {
    30  	cmdRoot.AddCommand(unlockCmd)
    31  
    32  	unlockCmd.Flags().BoolVar(&unlockOptions.RemoveAll, "remove-all", false, "remove all locks, even non-stale ones")
    33  }
    34  
    35  func runUnlock(opts UnlockOptions, gopts GlobalOptions) error {
    36  	repo, err := OpenRepository(gopts)
    37  	if err != nil {
    38  		return err
    39  	}
    40  
    41  	fn := restic.RemoveStaleLocks
    42  	if opts.RemoveAll {
    43  		fn = restic.RemoveAllLocks
    44  	}
    45  
    46  	err = fn(context.TODO(), repo)
    47  	if err != nil {
    48  		return err
    49  	}
    50  
    51  	Verbosef("successfully removed locks\n")
    52  	return nil
    53  }