Skip to content

Commit

Permalink
Add overwrite flag and abort if target file exists
Browse files Browse the repository at this point in the history
  • Loading branch information
ccremer committed Jan 29, 2023
1 parent 3fb6e99 commit 846e50f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
44 changes: 32 additions & 12 deletions bulk_download_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ type BulkDownloadCommand struct {
PaperlessToken string
PaperlessUser string

TargetPath string
Content string
UnzipEnabled bool
TargetPath string
Content string
UnzipEnabled bool
OverwriteExistingTarget bool
}

func newBulkDownloadCommand() *BulkDownloadCommand {
Expand All @@ -35,6 +36,7 @@ func newBulkDownloadCommand() *BulkDownloadCommand {
newTargetPathFlag(&c.TargetPath),
newDownloadContentFlag(&c.Content),
newUnzipFlag(&c.UnzipEnabled),
newOverwriteFlag(&c.OverwriteExistingTarget),
},
}
return c
Expand All @@ -43,7 +45,9 @@ func newBulkDownloadCommand() *BulkDownloadCommand {
func (c *BulkDownloadCommand) Action(ctx *cli.Context) error {
log := logr.FromContextOrDiscard(ctx.Context)

log.V(1)
if prepareErr := c.prepareTarget(); prepareErr != nil {
return prepareErr
}
clt := paperless.NewClient(c.PaperlessURL, c.PaperlessUser, c.PaperlessToken)

log.Info("Getting list of documents")
Expand Down Expand Up @@ -79,10 +83,7 @@ func (c *BulkDownloadCommand) Action(ctx *cli.Context) error {

func (c *BulkDownloadCommand) unzip(ctx *cli.Context, tmpFile *os.File) error {
log := logr.FromContextOrDiscard(ctx.Context)
downloadFilePath := c.TargetPath
if c.TargetPath == "" {
downloadFilePath = "documents"
}
downloadFilePath := c.getTargetPath()
if unzipErr := archive.Unzip(ctx.Context, tmpFile.Name(), downloadFilePath); unzipErr != nil {
return fmt.Errorf("cannot unzip file %q to %q: %w", tmpFile.Name(), downloadFilePath, unzipErr)
}
Expand All @@ -92,13 +93,32 @@ func (c *BulkDownloadCommand) unzip(ctx *cli.Context, tmpFile *os.File) error {

func (c *BulkDownloadCommand) move(ctx *cli.Context, tmpFile *os.File) error {
log := logr.FromContextOrDiscard(ctx.Context)
downloadFilePath := c.TargetPath
if c.TargetPath == "" {
downloadFilePath = "documents.zip"
}
downloadFilePath := c.getTargetPath()
if renameErr := os.Rename(tmpFile.Name(), downloadFilePath); renameErr != nil {
return fmt.Errorf("cannot move temp file: %w", renameErr)
}
log.Info("Downloaded zip archive", "file", downloadFilePath)
return nil
}

func (c *BulkDownloadCommand) getTargetPath() string {
if c.TargetPath != "" {
return c.TargetPath
}
if c.UnzipEnabled {
return "documents"
}
return "documents.zip"
}

func (c *BulkDownloadCommand) prepareTarget() error {
target := c.getTargetPath()
if c.OverwriteExistingTarget {
return os.RemoveAll(target)
}
_, err := os.Stat(target)
if err != nil && os.IsNotExist(err) {
return nil
}
return fmt.Errorf("target %q exists, abort", target)
}
8 changes: 8 additions & 0 deletions flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ func newUnzipFlag(dest *bool) *cli.BoolFlag {
}
}

func newOverwriteFlag(dest *bool) *cli.BoolFlag {
return &cli.BoolFlag{
Name: "overwrite", EnvVars: []string{"DOWNLOAD_OVERWRITE"},
Usage: "deletes existing file(s) before downloading.",
Destination: dest,
}
}

func checkEmptyString(flagName string) func(*cli.Context, string) error {
return func(ctx *cli.Context, s string) error {
if s == "" {
Expand Down

0 comments on commit 846e50f

Please sign in to comment.