github.com/mckael/restic@v0.8.3/cmd/restic/cmd_init.go (about)

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