restic/src/cmds/restic/cmd_init.go

56 lines
1.3 KiB
Go
Raw Normal View History

package main
import (
2016-09-01 20:17:37 +00:00
"restic/errors"
"restic/repository"
)
type CmdInit struct {
global *GlobalOptions
}
func (cmd CmdInit) Execute(args []string) error {
if cmd.global.Repo == "" {
2016-09-01 20:17:37 +00:00
return errors.Fatal("Please specify repository location (-r)")
}
be, err := create(cmd.global.Repo)
if err != nil {
cmd.global.Exitf(1, "creating backend at %s failed: %v\n", cmd.global.Repo, err)
}
if cmd.global.password == "" {
cmd.global.password, err = cmd.global.ReadPasswordTwice(
"enter password for new backend: ",
"enter password again: ")
if err != nil {
return err
}
}
2016-03-06 12:14:06 +00:00
s := repository.New(be)
err = s.Init(cmd.global.password)
if err != nil {
cmd.global.Exitf(1, "creating key in backend at %s failed: %v\n", cmd.global.Repo, err)
}
2016-09-01 14:04:29 +00:00
cmd.global.Verbosef("created restic backend %v at %s\n", s.Config().ID[:10], cmd.global.Repo)
2015-06-21 11:25:26 +00:00
cmd.global.Verbosef("\n")
cmd.global.Verbosef("Please note that knowledge of your password is required to access\n")
cmd.global.Verbosef("the repository. Losing your password means that your data is\n")
cmd.global.Verbosef("irrecoverably lost.\n")
return nil
}
func init() {
_, err := parser.AddCommand("init",
"create repository",
"The init command creates a new repository",
&CmdInit{global: &globalOpts})
if err != nil {
panic(err)
}
}