Skip to content

Commit

Permalink
Merge pull request #466 from mlycore/refactor-delete
Browse files Browse the repository at this point in the history
feat: add user approve prompt to delete
  • Loading branch information
tristaZero authored Nov 16, 2023
2 parents 3d87f11 + 8dcf177 commit c5da93a
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 20 deletions.
8 changes: 8 additions & 0 deletions pitr/cli/internal/cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ func backup() error {
return xerr.NewCliErr(fmt.Sprintf("check disk space failed. err: %s", err))
}

prompt := fmt.Sprintf(
"Please Check All Nodes Disk Space, Make Sure Have Enough Space To Backup Or Restore Data.\n" +
"Are you sure to continue? (Y/N)")
err = getUserApproveInTerminal(prompt)
if err != nil {
return xerr.NewCliErr(fmt.Sprintf("%s", err))
}

// Step5. send backup command to agent-server.
logging.Info("Starting backup ...")
err = execBackup(lsBackup)
Expand Down
8 changes: 8 additions & 0 deletions pitr/cli/internal/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ func deleteRecord() error {
return xerr.NewCliErr("one or more agent server are not available.")
}

prompt := fmt.Sprintf(
"The backup record(ID: %s, CSN: %s) will be deleted forever.\n"+
"Are you sure to continue? (Y/N)", bak.Info.ID, bak.Info.CSN)
err = getUserApproveInTerminal(prompt)
if err != nil {
return xerr.NewCliErr(fmt.Sprintf("%s", err))
}

// mark the target backup record to be deleted
// meanwhile this record cannot be restored
if err := ls.HideByName(bak.Info.FileName); err != nil {
Expand Down
4 changes: 3 additions & 1 deletion pitr/cli/internal/cmd/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ var _ = Describe("test delete", func() {
monkey.Patch(pkg.NewLocalStorage, func(rootDir string) (pkg.ILocalStorage, error) {
return ls, nil
})
monkey.Patch(getUserApproveInTerminal, func(_ string) error {
return nil
})
})

AfterEach(func() {
Expand All @@ -92,7 +95,6 @@ var _ = Describe("test delete", func() {
ls.EXPECT().HideByName(bak.Info.FileName).Return(nil)
as.EXPECT().DeleteBackup(gomock.Any()).Return(nil)
ls.EXPECT().DeleteByHidedName(bak.Info.FileName).Return(nil)

Expect(deleteRecord()).To(BeNil())
})

Expand Down
16 changes: 10 additions & 6 deletions pitr/cli/internal/cmd/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ func restore() error {
return xerr.NewCliErr(fmt.Sprintf("check database exist failed. err: %s", err))
}

prompt := fmt.Sprintf(
"Detected That The Database [%s] Already Exists In ShardingSphere-Proxy Metadata.\n"+
"The Logic Database Will Be DROPPED And Then Insert Backup's Metadata Into ShardingSphere-Proxy After Restoring The Backup Data.\n"+
"Are you sure to continue? (Y/N)", strings.Join(databaseNamesExist, ","))
err = getUserApproveInTerminal(prompt)
if err != nil {
return xerr.NewCliErr(fmt.Sprintf("%s", err))
}

// check agent server status
logging.Info("Checking agent server status...")
if available := checkAgentServerStatus(bak); !available {
Expand Down Expand Up @@ -150,12 +159,7 @@ func checkDatabaseExist(proxy pkg.IShardingSphereProxy, bak *model.LsBackup) err
return nil
}

// get user input to confirm
prompt := fmt.Sprintf(
"Detected That The Database [%s] Already Exists In ShardingSphere-Proxy Metadata.\n"+
"The Logic Database Will Be DROPPED And Then Insert Backup's Metadata Into ShardingSphere-Proxy After Restoring The Backup Data.\n"+
"Are you sure to continue? (Y|N)", strings.Join(databaseNamesExist, ","))
return getUserApproveInTerminal(prompt)
return nil
}

func restoreDataToSSProxy(proxy pkg.IShardingSphereProxy, lsBackup *model.LsBackup) error {
Expand Down
12 changes: 3 additions & 9 deletions pitr/cli/internal/cmd/restore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ var _ = Describe("test restore", func() {
monkey.Patch(pkg.NewLocalStorage, func(rootDir string) (pkg.ILocalStorage, error) {
return ls, nil
})
monkey.Patch(getUserApproveInTerminal, func(_ string) error {
return nil
})
})

AfterEach(func() {
Expand Down Expand Up @@ -131,15 +134,6 @@ var _ = Describe("test restore", func() {
Expect(restore()).To(BeNil())
})

// test getUserApproveInTerminal
Context("test userApproveInTerminal", func() {
// test user abort
It("user abort", func() {
// exec getUserApproveInTerminal
Expect(getUserApproveInTerminal("")).To(Equal(xerr.NewCliErr("User abort")))
})
})

Context("restore data to ss proxy", func() {

It("no need to drop database", func() {
Expand Down
5 changes: 1 addition & 4 deletions pitr/cli/internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,5 @@ func checkDiskSpace(lsBackup *model.LsBackup) error {
t.AppendSeparator()
}
t.Render()
prompt := fmt.Sprintf(
"Please Check All Nodes Disk Space, Make Sure Have Enough Space To Backup Or Restore Data.\n" +
"Are you sure to continue? (Y/N)")
return getUserApproveInTerminal(prompt)
return nil
}
3 changes: 3 additions & 0 deletions pitr/cli/internal/pkg/local-storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ func (ls *localStorage) ReadAll() ([]*model.LsBackup, error) {
if entry.IsDir() {
continue
}
if strings.HasPrefix(entry.Name(), ".") {
continue
}

info, err := entry.Info()
if errors.Is(err, os.ErrNotExist) {
Expand Down

0 comments on commit c5da93a

Please sign in to comment.