Skip to content

Commit

Permalink
Add dry-run mode
Browse files Browse the repository at this point in the history
  • Loading branch information
cedws committed May 25, 2020
1 parent a24cb79 commit 7b6c3ac
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
19 changes: 15 additions & 4 deletions client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var endpoints = map[string]string{
type Client struct {
deletedCount int
requestCount int
dryRun bool
token string
httpClient http.Client
}
Expand All @@ -45,6 +46,10 @@ func New(token string) (c Client) {
}
}

func (c *Client) SetDryRun(dryRun bool) {
c.dryRun = dryRun
}

func (c *Client) PartialDelete() error {
me, err := c.Me()
if err != nil {
Expand Down Expand Up @@ -164,12 +169,18 @@ func (c *Client) DeleteMessages(messages *Messages, seek *int) error {
// An example of an action is a call request.
if msg.Type == UserMessage {
log.Infof("Deleting message %v from channel %v", msg.ID, msg.ChannelID)
err := c.DeleteMessage(&msg)
if err != nil {
return errors.Wrap(err, "Error deleting message")
if c.dryRun {
// Move seek index forward to simulate message deletion on server's side
(*seek)++
} else {
err := c.DeleteMessage(&msg)
if err != nil {
return errors.Wrap(err, "Error deleting message")
}
time.Sleep(minSleep * time.Millisecond)
}
// Increment regardless of whether it's a dry run
c.deletedCount++
time.Sleep(minSleep * time.Millisecond)
} else {
log.Debugf("Found message of non-zero type, incrementing seek index")
(*seek)++
Expand Down
5 changes: 5 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

var verbose bool
var dryrun bool

var rootCmd = &cobra.Command{
Use: "discord-delete",
Expand Down Expand Up @@ -37,6 +38,8 @@ var partialCmd = &cobra.Command{
}

client := client.New(tok)
client.SetDryRun(dryrun)

err = client.PartialDelete()
if err != nil {
log.Fatal(err)
Expand All @@ -45,6 +48,8 @@ var partialCmd = &cobra.Command{
}

func init() {
partialCmd.Flags().BoolVarP(&dryrun, "dry-run", "d", false, "perform dry run without deleting anything")

rootCmd.AddCommand(partialCmd)
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "enable verbose logging")
}
Expand Down

0 comments on commit 7b6c3ac

Please sign in to comment.