2015-06-21 11:02:56 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
|
|
|
"github.com/restic/restic/internal/repository"
|
2016-09-17 10:36:05 +00:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2015-06-21 11:02:56 +00:00
|
|
|
)
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
var cmdInit = &cobra.Command{
|
|
|
|
Use: "init",
|
2017-09-11 16:32:44 +00:00
|
|
|
Short: "Initialize a new repository",
|
2016-09-17 10:36:05 +00:00
|
|
|
Long: `
|
|
|
|
The "init" command initializes a new repository.
|
2019-11-05 06:03:38 +00:00
|
|
|
|
|
|
|
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
|
|
|
`,
|
2017-08-06 19:02:16 +00:00
|
|
|
DisableAutoGenTag: true,
|
2016-09-17 10:36:05 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runInit(globalOptions, args)
|
|
|
|
},
|
2015-06-21 11:02:56 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
func init() {
|
|
|
|
cmdRoot.AddCommand(cmdInit)
|
|
|
|
}
|
|
|
|
|
|
|
|
func runInit(gopts GlobalOptions, args []string) error {
|
|
|
|
if gopts.Repo == "" {
|
2016-09-01 20:17:37 +00:00
|
|
|
return errors.Fatal("Please specify repository location (-r)")
|
2015-06-21 11:02:56 +00:00
|
|
|
}
|
|
|
|
|
2017-03-25 14:33:52 +00:00
|
|
|
be, err := create(gopts.Repo, gopts.extended)
|
2015-06-26 17:58:43 +00:00
|
|
|
if err != nil {
|
2018-01-20 08:51:49 +00:00
|
|
|
return errors.Fatalf("create repository at %s failed: %v\n", gopts.Repo, err)
|
2015-06-26 17:58:43 +00:00
|
|
|
}
|
|
|
|
|
2017-07-24 21:15:31 +00:00
|
|
|
gopts.password, err = ReadPasswordTwice(gopts,
|
2018-01-20 08:51:49 +00:00
|
|
|
"enter password for new repository: ",
|
2017-07-24 21:15:31 +00:00
|
|
|
"enter password again: ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-06-21 11:02:56 +00:00
|
|
|
}
|
|
|
|
|
2016-03-06 12:14:06 +00:00
|
|
|
s := repository.New(be)
|
2016-03-06 11:34:23 +00:00
|
|
|
|
use global context for check, debug, dump, find, forget, init, key,
list, mount, tag, unlock commands
gh-1434
2017-12-06 12:02:55 +00:00
|
|
|
err = s.Init(gopts.ctx, gopts.password)
|
2015-06-21 11:02:56 +00:00
|
|
|
if err != nil {
|
2018-01-20 08:51:49 +00:00
|
|
|
return errors.Fatalf("create key in repository at %s failed: %v\n", gopts.Repo, err)
|
2015-06-21 11:02:56 +00:00
|
|
|
}
|
|
|
|
|
2018-01-20 08:51:49 +00:00
|
|
|
Verbosef("created restic repository %v at %s\n", s.Config().ID[:10], gopts.Repo)
|
2016-09-17 10:36:05 +00:00
|
|
|
Verbosef("\n")
|
|
|
|
Verbosef("Please note that knowledge of your password is required to access\n")
|
|
|
|
Verbosef("the repository. Losing your password means that your data is\n")
|
|
|
|
Verbosef("irrecoverably lost.\n")
|
2015-06-21 11:02:56 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|