From f5adac51400c74cfa234f8964e09bfcf2c7f9266 Mon Sep 17 00:00:00 2001 From: David Dworken Date: Sat, 19 Nov 2022 17:14:27 -0800 Subject: [PATCH] Move code out of lib that is only referenced for one command --- client/cmd/redact.go | 75 +++++++++++++++++++++++++++++++++++++++++++- client/lib/lib.go | 66 -------------------------------------- 2 files changed, 74 insertions(+), 67 deletions(-) diff --git a/client/cmd/redact.go b/client/cmd/redact.go index 46f2b73..27a0205 100644 --- a/client/cmd/redact.go +++ b/client/cmd/redact.go @@ -1,11 +1,18 @@ package cmd import ( + "bufio" + "context" + "encoding/json" + "fmt" "os" "strings" + "time" + "github.com/ddworken/hishtory/client/data" "github.com/ddworken/hishtory/client/hctx" "github.com/ddworken/hishtory/client/lib" + "github.com/ddworken/hishtory/shared" "github.com/spf13/cobra" ) @@ -23,10 +30,76 @@ var redactCmd = &cobra.Command{ lib.CheckFatalError(lib.RetrieveAdditionalEntriesFromRemote(ctx)) lib.CheckFatalError(lib.ProcessDeletionRequests(ctx)) query := strings.Join(args, " ") - lib.CheckFatalError(lib.Redact(ctx, query, os.Getenv("HISHTORY_REDACT_FORCE") != "")) + lib.CheckFatalError(redact(ctx, query, os.Getenv("HISHTORY_REDACT_FORCE") != "")) }, } +func redact(ctx *context.Context, query string, force bool) error { + tx, err := lib.MakeWhereQueryFromSearch(ctx, hctx.GetDb(ctx), query) + if err != nil { + return err + } + var historyEntries []*data.HistoryEntry + res := tx.Find(&historyEntries) + if res.Error != nil { + return res.Error + } + if force { + fmt.Printf("Permanently deleting %d entries\n", len(historyEntries)) + } else { + fmt.Printf("This will permanently delete %d entries, are you sure? [y/N]", len(historyEntries)) + reader := bufio.NewReader(os.Stdin) + resp, err := reader.ReadString('\n') + if err != nil { + return fmt.Errorf("failed to read response: %v", err) + } + if strings.TrimSpace(resp) != "y" { + fmt.Printf("Aborting delete per user response of %#v\n", strings.TrimSpace(resp)) + return nil + } + } + tx, err = lib.MakeWhereQueryFromSearch(ctx, hctx.GetDb(ctx), query) + if err != nil { + return err + } + res = tx.Delete(&data.HistoryEntry{}) + if res.Error != nil { + return res.Error + } + if res.RowsAffected != int64(len(historyEntries)) { + return fmt.Errorf("DB deleted %d rows, when we only expected to delete %d rows, something may have gone wrong", res.RowsAffected, len(historyEntries)) + } + err = deleteOnRemoteInstances(ctx, historyEntries) + if err != nil { + return err + } + return nil +} + +func deleteOnRemoteInstances(ctx *context.Context, historyEntries []*data.HistoryEntry) error { + config := hctx.GetConf(ctx) + if config.IsOffline { + return nil + } + + var deletionRequest shared.DeletionRequest + deletionRequest.SendTime = time.Now() + deletionRequest.UserId = data.UserId(config.UserSecret) + + for _, entry := range historyEntries { + deletionRequest.Messages.Ids = append(deletionRequest.Messages.Ids, shared.MessageIdentifier{Date: entry.EndTime, DeviceId: entry.DeviceId}) + } + data, err := json.Marshal(deletionRequest) + if err != nil { + return err + } + _, err = lib.ApiPost("/api/v1/add-deletion-request", "application/json", data) + if err != nil { + return fmt.Errorf("failed to send deletion request to backend service, this may cause commands to not get deleted on other instances of hishtory: %v", err) + } + return nil +} + func init() { rootCmd.AddCommand(redactCmd) } diff --git a/client/lib/lib.go b/client/lib/lib.go index 7d4a3c1..93a3182 100644 --- a/client/lib/lib.go +++ b/client/lib/lib.go @@ -994,72 +994,6 @@ func EncryptAndMarshal(config hctx.ClientConfig, entries []*data.HistoryEntry) ( return jsonValue, nil } -func Redact(ctx *context.Context, query string, force bool) error { - tx, err := MakeWhereQueryFromSearch(ctx, hctx.GetDb(ctx), query) - if err != nil { - return err - } - var historyEntries []*data.HistoryEntry - res := tx.Find(&historyEntries) - if res.Error != nil { - return res.Error - } - if force { - fmt.Printf("Permanently deleting %d entries\n", len(historyEntries)) - } else { - fmt.Printf("This will permanently delete %d entries, are you sure? [y/N]", len(historyEntries)) - reader := bufio.NewReader(os.Stdin) - resp, err := reader.ReadString('\n') - if err != nil { - return fmt.Errorf("failed to read response: %v", err) - } - if strings.TrimSpace(resp) != "y" { - fmt.Printf("Aborting delete per user response of %#v\n", strings.TrimSpace(resp)) - return nil - } - } - tx, err = MakeWhereQueryFromSearch(ctx, hctx.GetDb(ctx), query) - if err != nil { - return err - } - res = tx.Delete(&data.HistoryEntry{}) - if res.Error != nil { - return res.Error - } - if res.RowsAffected != int64(len(historyEntries)) { - return fmt.Errorf("DB deleted %d rows, when we only expected to delete %d rows, something may have gone wrong", res.RowsAffected, len(historyEntries)) - } - err = deleteOnRemoteInstances(ctx, historyEntries) - if err != nil { - return err - } - return nil -} - -func deleteOnRemoteInstances(ctx *context.Context, historyEntries []*data.HistoryEntry) error { - config := hctx.GetConf(ctx) - if config.IsOffline { - return nil - } - - var deletionRequest shared.DeletionRequest - deletionRequest.SendTime = time.Now() - deletionRequest.UserId = data.UserId(config.UserSecret) - - for _, entry := range historyEntries { - deletionRequest.Messages.Ids = append(deletionRequest.Messages.Ids, shared.MessageIdentifier{Date: entry.EndTime, DeviceId: entry.DeviceId}) - } - data, err := json.Marshal(deletionRequest) - if err != nil { - return err - } - _, err = ApiPost("/api/v1/add-deletion-request", "application/json", data) - if err != nil { - return fmt.Errorf("failed to send deletion request to backend service, this may cause commands to not get deleted on other instances of hishtory: %v", err) - } - return nil -} - func Reupload(ctx *context.Context) error { config := hctx.GetConf(ctx) if config.IsOffline {