From bf50ed01928221cbeeab95f0507fc0493cef389e Mon Sep 17 00:00:00 2001 From: neha-Gupta1 Date: Wed, 4 Oct 2023 15:10:28 +0530 Subject: [PATCH] filesystem update command --- src/cli/repo/filesystem.go | 67 +++++++++++++++++++++++++ src/cli/repo/filesystem_e2e_test.go | 78 +++++++++++++++++++++++++++++ src/cli/repo/filesystem_test.go | 1 + src/cli/repo/repo.go | 3 +- src/cli/repo/s3.go | 2 + 5 files changed, 149 insertions(+), 2 deletions(-) diff --git a/src/cli/repo/filesystem.go b/src/cli/repo/filesystem.go index f6a495f21..2f690a803 100644 --- a/src/cli/repo/filesystem.go +++ b/src/cli/repo/filesystem.go @@ -38,6 +38,11 @@ func addFilesystemCommands(cmd *cobra.Command) *cobra.Command { case connectCommand: c, _ = utils.AddCommand(cmd, filesystemConnectCmd()) + + case updateCommand: + update := filesystemUpdateCmd() + flags.AddCorsoUpdatePassphraseFlags(update) + c, _ = utils.AddCommand(cmd, update) } c.Use = c.Use + " " + fsProviderCmdUseSuffix @@ -229,3 +234,65 @@ func connectFilesystemCmd(cmd *cobra.Command, args []string) error { return nil } + +// --------------------------------------------------------------------------------------------------------- +// Update +// --------------------------------------------------------------------------------------------------------- + +// `corso repo update filesystem [...]` +func filesystemUpdateCmd() *cobra.Command { + return &cobra.Command{ + Use: fsProviderCommand, + Short: "Update to a filesystem repository", + Long: `Update to an existing repository on local or network storage.`, + RunE: updateFilesystemCmd, + Args: cobra.NoArgs, + Example: fsProviderCmdConnectExamples, + } +} + +// updates to an existing filesystem repo. +func updateFilesystemCmd(cmd *cobra.Command, args []string) error { + ctx := cmd.Context() + overrides := flags.FilesystemFlagOverrides(cmd) + + // TODO(pandeyabs): Move filepath conversion to FilesystemConfig scope. + abs, err := utils.MakeAbsoluteFilePath(overrides[flags.FilesystemPathFN]) + if err != nil { + return Only(ctx, clues.Wrap(err, "getting absolute repo path")) + } + + overrides[flags.FilesystemPathFN] = abs + + cfg, err := config.GetConfigRepoDetails( + ctx, + storage.ProviderFilesystem, + true, + true, + overrides) + if err != nil { + return Only(ctx, err) + } + + opts := utils.ControlWithConfig(cfg) + + r, err := repository.New( + ctx, + cfg.Account, + cfg.Storage, + opts, + cfg.RepoID) + if err != nil { + return Only(ctx, clues.Wrap(err, "Failed to create a repository controller")) + } + + if err := r.UpdatePassword(ctx, flags.UpdateCorsoPhasephraseFV); err != nil { + return Only(ctx, clues.Wrap(err, "Failed to update s3")) + } + + defer utils.CloseRepo(ctx, r) + + Infof(ctx, "Updated repo password.") + + return nil +} diff --git a/src/cli/repo/filesystem_e2e_test.go b/src/cli/repo/filesystem_e2e_test.go index faeb5a5b1..6341d9128 100644 --- a/src/cli/repo/filesystem_e2e_test.go +++ b/src/cli/repo/filesystem_e2e_test.go @@ -153,3 +153,81 @@ func (suite *FilesystemE2ESuite) TestConnectFilesystemCmd() { }) } } + +func (suite *FilesystemE2ESuite) TestUpdateFilesystemCmd() { + t := suite.T() + + ctx, flush := tester.NewContext(t) + defer flush() + + st := storeTD.NewFilesystemStorage(t) + cfg, err := st.ToFilesystemConfig() + require.NoError(t, err, clues.ToCore(err)) + + force := map[string]string{ + tconfig.TestCfgAccountProvider: account.ProviderM365.String(), + tconfig.TestCfgStorageProvider: storage.ProviderFilesystem.String(), + tconfig.TestCfgFilesystemPath: cfg.Path, + } + vpr, configFP := tconfig.MakeTempTestConfigClone(t, force) + + ctx = config.SetViper(ctx, vpr) + + // init the repo first + r, err := repository.New( + ctx, + tconfig.NewM365Account(t), + st, + control.DefaultOptions(), + repository.NewRepoID) + require.NoError(t, err, clues.ToCore(err)) + + err = r.Initialize(ctx, repository.InitConfig{}) + require.NoError(t, err, clues.ToCore(err)) + + // then connect it + cmd := cliTD.StubRootCmd( + "repo", "connect", "filesystem", + "--config-file", configFP, + "--path", cfg.Path) + cli.BuildCommandTree(cmd) + + // run the command + err = cmd.ExecuteContext(ctx) + require.NoError(t, err, clues.ToCore(err)) + + // test update + cmd = cliTD.StubRootCmd( + "repo", "update", "filesystem", + "--config-file", configFP, + "--path", cfg.Path, + "--update-passphrase", "newpass") + cli.BuildCommandTree(cmd) + + // run the command + err = cmd.ExecuteContext(ctx) + require.NoError(t, err, clues.ToCore(err)) + + // try connecting with new password + cmd = cliTD.StubRootCmd( + "repo", "connect", "filesystem", + "--config-file", configFP, + "--path", cfg.Path, + "--passphrase", "newpass") + cli.BuildCommandTree(cmd) + + // run the command + err = cmd.ExecuteContext(ctx) + require.NoError(t, err, clues.ToCore(err)) + + // try connecting with old password + cmd = cliTD.StubRootCmd( + "repo", "connect", "filesystem", + "--config-file", configFP, + "--path", cfg.Path) + cli.BuildCommandTree(cmd) + + // run the command + err = cmd.ExecuteContext(ctx) + require.Error(t, err, clues.ToCore(err)) +} diff --git a/src/cli/repo/filesystem_test.go b/src/cli/repo/filesystem_test.go index b7fdf0cb8..5428f8d2a 100644 --- a/src/cli/repo/filesystem_test.go +++ b/src/cli/repo/filesystem_test.go @@ -31,6 +31,7 @@ func (suite *FilesystemSuite) TestAddFilesystemCommands() { }{ {"init filesystem", initCommand, expectUse, filesystemInitCmd().Short, initFilesystemCmd}, {"connect filesystem", connectCommand, expectUse, filesystemConnectCmd().Short, connectFilesystemCmd}, + {"update filesystem", updateCommand, expectUse, filesystemUpdateCmd().Short, updateFilesystemCmd}, } for _, test := range table { suite.Run(test.name, func() { diff --git a/src/cli/repo/repo.go b/src/cli/repo/repo.go index c43fef744..ae59d44e2 100644 --- a/src/cli/repo/repo.go +++ b/src/cli/repo/repo.go @@ -57,9 +57,8 @@ func AddCommands(cmd *cobra.Command) { for _, addRepoTo := range repoCommands { addRepoTo(initCmd) addRepoTo(connectCmd) + addRepoTo(updateCmd) } - - addS3Commands(updateCmd) } // The repo category of commands. diff --git a/src/cli/repo/s3.go b/src/cli/repo/s3.go index 1181209c9..fb248d99f 100644 --- a/src/cli/repo/s3.go +++ b/src/cli/repo/s3.go @@ -295,6 +295,8 @@ func updateS3Cmd(cmd *cobra.Command, args []string) error { return Only(ctx, clues.Wrap(err, "Failed to update s3")) } + defer utils.CloseRepo(ctx, r) + Infof(ctx, "Updated repo password.") return nil