Skip to content

Commit

Permalink
Add flag to delete file after upload
Browse files Browse the repository at this point in the history
  • Loading branch information
ccremer committed Dec 27, 2022
1 parent dcb74c8 commit 9425797
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
8 changes: 8 additions & 0 deletions flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ func newTagFlag(dest *cli.StringSlice) *cli.StringSliceFlag {
}
}

func newDeleteAfterUploadFlag(dest *bool) *cli.BoolFlag {
return &cli.BoolFlag{
Name: "delete-after-upload", EnvVars: envVars("DELETE_AFTER_UPLOAD"),
Usage: "deletes the file(s) after upload",
Destination: dest,
}
}

func newConsumeDirFlag(dest *string) *cli.StringFlag {
return &cli.StringFlag{
Name: "consume-dir", EnvVars: []string{"CONSUME_DIR"},
Expand Down
26 changes: 21 additions & 5 deletions upload_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"os"

"github.com/ccremer/clustercode/pkg/paperless"
"github.com/ccremer/plogr"
Expand All @@ -17,11 +18,12 @@ type UploadCommand struct {
PaperlessToken string
PaperlessUser string

CreatedAt cli.Timestamp
DocumentTitle string
DocumentType string
Correspondent string
DocumentTags cli.StringSlice
CreatedAt cli.Timestamp
DocumentTitle string
DocumentType string
Correspondent string
DocumentTags cli.StringSlice
DeleteAfterUpload bool
}

func newUploadCommand() *UploadCommand {
Expand All @@ -48,6 +50,7 @@ func newUploadCommand() *UploadCommand {
newDocumentTypeFlag(&c.DocumentType),
newCorrespondentFlag(&c.Correspondent),
newTagFlag(&c.DocumentTags),
newDeleteAfterUploadFlag(&c.DeleteAfterUpload),
},
ArgsUsage: "[FILES...]",
}
Expand Down Expand Up @@ -78,6 +81,19 @@ func (c *UploadCommand) Action(ctx *cli.Context) error {
pterm.Success.Println(plogr.DefaultFormatter("File uploaded", map[string]interface{}{
"file": arg,
}))
if c.DeleteAfterUpload {
c.deleteAfterUpload(arg)
}
}
return nil
}

func (c *UploadCommand) deleteAfterUpload(arg string) {
err := os.Remove(arg)
if err != nil {
pterm.Warning.Println(plogr.DefaultFormatter("File could not be deleted", map[string]interface{}{
"file": arg,
"error": err,
}))
}
}

0 comments on commit 9425797

Please sign in to comment.