Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add user approve prompt to delete #466

Merged
merged 8 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading