diff --git a/cmd/restic/cmd_init.go b/cmd/restic/cmd_init.go index d66163af1..0ac65dbe4 100644 --- a/cmd/restic/cmd_init.go +++ b/cmd/restic/cmd_init.go @@ -10,6 +10,7 @@ import ( "github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/repository" "github.com/restic/restic/internal/restic" + "github.com/restic/restic/internal/ui/termstatus" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -33,7 +34,9 @@ Exit status is 1 if there was any error. GroupID: cmdGroupDefault, DisableAutoGenTag: true, 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()) @@ -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'") } -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 { 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 if opts.RepositoryVersion == "latest" || opts.RepositoryVersion == "" { version = restic.MaxRepoVersion @@ -110,16 +115,14 @@ func runInit(ctx context.Context, opts InitOptions, gopts GlobalOptions, args [] } 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 { - Verbosef(" with chunker parameters copied from secondary repository\n") - } else { - Verbosef("\n") + printer.P(" with chunker parameters copied from secondary repository") } - 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") + printer.P("") + printer.P("Please note that knowledge of your password is required to access") + printer.P("the repository. Losing your password means that your data is") + printer.P("irrecoverably lost.") } else { status := initSuccess{ diff --git a/cmd/restic/cmd_init_integration_test.go b/cmd/restic/cmd_init_integration_test.go index 4795d5510..3d4fddcf6 100644 --- a/cmd/restic/cmd_init_integration_test.go +++ b/cmd/restic/cmd_init_integration_test.go @@ -9,6 +9,7 @@ import ( "github.com/restic/restic/internal/repository" "github.com/restic/restic/internal/restic" rtest "github.com/restic/restic/internal/test" + "github.com/restic/restic/internal/ui/termstatus" ) func testRunInit(t testing.TB, opts GlobalOptions) { @@ -16,7 +17,10 @@ func testRunInit(t testing.TB, opts GlobalOptions) { restic.TestDisableCheckPolynomial(t) 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) // 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, }, } - 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 - 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) rtest.OK(t, err)