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

     1  package main
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/restic/restic/internal/errors"
     7  	"github.com/restic/restic/internal/repository"
     8  
     9  	"github.com/spf13/cobra"
    10  )
    11  
    12  var cmdInit = &cobra.Command{
    13  	Use:   "init",
    14  	Short: "Initialize a new repository",
    15  	Long: `
    16  The "init" command initializes a new repository.
    17  `,
    18  	DisableAutoGenTag: true,
    19  	RunE: func(cmd *cobra.Command, args []string) error {
    20  		return runInit(globalOptions, args)
    21  	},
    22  }
    23  
    24  func init() {
    25  	cmdRoot.AddCommand(cmdInit)
    26  }
    27  
    28  func runInit(gopts GlobalOptions, args []string) error {
    29  	if gopts.Repo == "" {
    30  		return errors.Fatal("Please specify repository location (-r)")
    31  	}
    32  
    33  	be, err := create(gopts.Repo, gopts.extended)
    34  	if err != nil {
    35  		return errors.Fatalf("create backend at %s failed: %v\n", gopts.Repo, err)
    36  	}
    37  
    38  	gopts.password, err = ReadPasswordTwice(gopts,
    39  		"enter password for new backend: ",
    40  		"enter password again: ")
    41  	if err != nil {
    42  		return err
    43  	}
    44  
    45  	s := repository.New(be)
    46  
    47  	err = s.Init(context.TODO(), gopts.password)
    48  	if err != nil {
    49  		return errors.Fatalf("create key in backend at %s failed: %v\n", gopts.Repo, err)
    50  	}
    51  
    52  	Verbosef("created restic backend %v at %s\n", s.Config().ID[:10], gopts.Repo)
    53  	Verbosef("\n")
    54  	Verbosef("Please note that knowledge of your password is required to access\n")
    55  	Verbosef("the repository. Losing your password means that your data is\n")
    56  	Verbosef("irrecoverably lost.\n")
    57  
    58  	return nil
    59  }