mirror of
https://github.com/restic/restic.git
synced 2025-12-13 16:33:22 +00:00
init: convert to termstatus
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
|||||||
"github.com/restic/restic/internal/errors"
|
"github.com/restic/restic/internal/errors"
|
||||||
"github.com/restic/restic/internal/repository"
|
"github.com/restic/restic/internal/repository"
|
||||||
"github.com/restic/restic/internal/restic"
|
"github.com/restic/restic/internal/restic"
|
||||||
|
"github.com/restic/restic/internal/ui/termstatus"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
@@ -33,7 +34,9 @@ Exit status is 1 if there was any error.
|
|||||||
GroupID: cmdGroupDefault,
|
GroupID: cmdGroupDefault,
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return runInit(cmd.Context(), opts, globalOptions, args)
|
term, cancel := setupTermstatus()
|
||||||
|
defer cancel()
|
||||||
|
return runInit(cmd.Context(), opts, globalOptions, args, term)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
opts.AddFlags(cmd.Flags())
|
opts.AddFlags(cmd.Flags())
|
||||||
@@ -53,11 +56,13 @@ func (opts *InitOptions) AddFlags(f *pflag.FlagSet) {
|
|||||||
f.StringVar(&opts.RepositoryVersion, "repository-version", "stable", "repository format version to use, allowed values are a format version, 'latest' and 'stable'")
|
f.StringVar(&opts.RepositoryVersion, "repository-version", "stable", "repository format version to use, allowed values are a format version, 'latest' and 'stable'")
|
||||||
}
|
}
|
||||||
|
|
||||||
func runInit(ctx context.Context, opts InitOptions, gopts GlobalOptions, args []string) error {
|
func runInit(ctx context.Context, opts InitOptions, gopts GlobalOptions, args []string, term *termstatus.Terminal) error {
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
return errors.Fatal("the init command expects no arguments, only options - please see `restic help init` for usage and flags")
|
return errors.Fatal("the init command expects no arguments, only options - please see `restic help init` for usage and flags")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printer := newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, term)
|
||||||
|
|
||||||
var version uint
|
var version uint
|
||||||
if opts.RepositoryVersion == "latest" || opts.RepositoryVersion == "" {
|
if opts.RepositoryVersion == "latest" || opts.RepositoryVersion == "" {
|
||||||
version = restic.MaxRepoVersion
|
version = restic.MaxRepoVersion
|
||||||
@@ -110,16 +115,14 @@ func runInit(ctx context.Context, opts InitOptions, gopts GlobalOptions, args []
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !gopts.JSON {
|
if !gopts.JSON {
|
||||||
Verbosef("created restic repository %v at %s", s.Config().ID[:10], location.StripPassword(gopts.backends, gopts.Repo))
|
printer.P("created restic repository %v at %s", s.Config().ID[:10], location.StripPassword(gopts.backends, gopts.Repo))
|
||||||
if opts.CopyChunkerParameters && chunkerPolynomial != nil {
|
if opts.CopyChunkerParameters && chunkerPolynomial != nil {
|
||||||
Verbosef(" with chunker parameters copied from secondary repository\n")
|
printer.P(" with chunker parameters copied from secondary repository")
|
||||||
} else {
|
|
||||||
Verbosef("\n")
|
|
||||||
}
|
}
|
||||||
Verbosef("\n")
|
printer.P("")
|
||||||
Verbosef("Please note that knowledge of your password is required to access\n")
|
printer.P("Please note that knowledge of your password is required to access")
|
||||||
Verbosef("the repository. Losing your password means that your data is\n")
|
printer.P("the repository. Losing your password means that your data is")
|
||||||
Verbosef("irrecoverably lost.\n")
|
printer.P("irrecoverably lost.")
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
status := initSuccess{
|
status := initSuccess{
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/restic/restic/internal/repository"
|
"github.com/restic/restic/internal/repository"
|
||||||
"github.com/restic/restic/internal/restic"
|
"github.com/restic/restic/internal/restic"
|
||||||
rtest "github.com/restic/restic/internal/test"
|
rtest "github.com/restic/restic/internal/test"
|
||||||
|
"github.com/restic/restic/internal/ui/termstatus"
|
||||||
)
|
)
|
||||||
|
|
||||||
func testRunInit(t testing.TB, opts GlobalOptions) {
|
func testRunInit(t testing.TB, opts GlobalOptions) {
|
||||||
@@ -16,7 +17,10 @@ func testRunInit(t testing.TB, opts GlobalOptions) {
|
|||||||
restic.TestDisableCheckPolynomial(t)
|
restic.TestDisableCheckPolynomial(t)
|
||||||
restic.TestSetLockTimeout(t, 0)
|
restic.TestSetLockTimeout(t, 0)
|
||||||
|
|
||||||
rtest.OK(t, runInit(context.TODO(), InitOptions{}, opts, nil))
|
err := withTermStatus(opts, func(ctx context.Context, term *termstatus.Terminal) error {
|
||||||
|
return runInit(ctx, InitOptions{}, opts, nil, term)
|
||||||
|
})
|
||||||
|
rtest.OK(t, err)
|
||||||
t.Logf("repository initialized at %v", opts.Repo)
|
t.Logf("repository initialized at %v", opts.Repo)
|
||||||
|
|
||||||
// create temporary junk files to verify that restic does not trip over them
|
// create temporary junk files to verify that restic does not trip over them
|
||||||
@@ -39,10 +43,16 @@ func TestInitCopyChunkerParams(t *testing.T) {
|
|||||||
password: env2.gopts.password,
|
password: env2.gopts.password,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
rtest.Assert(t, runInit(context.TODO(), initOpts, env.gopts, nil) != nil, "expected invalid init options to fail")
|
err := withTermStatus(env.gopts, func(ctx context.Context, term *termstatus.Terminal) error {
|
||||||
|
return runInit(ctx, initOpts, env.gopts, nil, term)
|
||||||
|
})
|
||||||
|
rtest.Assert(t, err != nil, "expected invalid init options to fail")
|
||||||
|
|
||||||
initOpts.CopyChunkerParameters = true
|
initOpts.CopyChunkerParameters = true
|
||||||
rtest.OK(t, runInit(context.TODO(), initOpts, env.gopts, nil))
|
err = withTermStatus(env.gopts, func(ctx context.Context, term *termstatus.Terminal) error {
|
||||||
|
return runInit(ctx, initOpts, env.gopts, nil, term)
|
||||||
|
})
|
||||||
|
rtest.OK(t, err)
|
||||||
|
|
||||||
repo, err := OpenRepository(context.TODO(), env.gopts)
|
repo, err := OpenRepository(context.TODO(), env.gopts)
|
||||||
rtest.OK(t, err)
|
rtest.OK(t, err)
|
||||||
|
|||||||
Reference in New Issue
Block a user