Skip to content

Commit

Permalink
Merge pull request #9 from ccremer/flags
Browse files Browse the repository at this point in the history
Fix required flags and usage printer
  • Loading branch information
ccremer authored Dec 27, 2022
2 parents 8eaf8b6 + 30de518 commit dcb74c8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
3 changes: 1 addition & 2 deletions consume_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ func newConsumeCommand() *ConsumeCommand {
c.Command = cli.Command{
Name: "consume",
Description: "Consumes a local directory and uploads each file to Paperless instance. The files will be deleted once uploaded.",
Before: LogMetadata,
Action: c.Action,
Action: actions(LogMetadata, c.Action),

Flags: []cli.Flag{
newURLFlag(&c.PaperlessURL),
Expand Down
19 changes: 18 additions & 1 deletion flags.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"fmt"

"github.com/urfave/cli/v2"
)

Expand All @@ -17,14 +19,17 @@ func newURLFlag(dest *string) *cli.StringFlag {
Name: "url", EnvVars: envVars("URL"),
Usage: "URL endpoint of the paperless instance.",
Required: true,
Action: checkEmptyString("url"),
Destination: dest,
}
}

func newTokenFlag(dest *string) *cli.StringFlag {
return &cli.StringFlag{
Name: "token", EnvVars: envVars("TOKEN"),
Usage: "Password or Token of the paperless instance.",
Usage: "password or token of the paperless instance.",
Required: true,
Action: checkEmptyString("token"),
Destination: dest,
}
}
Expand Down Expand Up @@ -81,5 +86,17 @@ func newConsumeDirFlag(dest *string) *cli.StringFlag {
Usage: "the directory name which to consume files.",
Required: true,
Destination: dest,
Action: checkEmptyString("consume-dir"),
}
}

func checkEmptyString(flagName string) func(*cli.Context, string) error {
return func(ctx *cli.Context, s string) error {
if s == "" {
ctx.Command.Subcommands = nil // required to print usage of subcommand
_ = cli.ShowCommandHelp(ctx, ctx.Command.Name)
return fmt.Errorf(`Required flag %q not set`, flagName)
}
return nil
}
}
11 changes: 11 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,14 @@ func envVars(suffixes ...string) []string {
}
return arr
}

func actions(actions ...cli.ActionFunc) cli.ActionFunc {
return func(ctx *cli.Context) error {
for _, action := range actions {
if err := action(ctx); err != nil {
return err
}
}
return nil
}
}
15 changes: 9 additions & 6 deletions upload_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,15 @@ func newUploadCommand() *UploadCommand {
c.Command = cli.Command{
Name: "upload",
Description: "Uploads local document(s) to Paperless instance",
Before: LogMetadata,
Action: c.Action,
Before: func(ctx *cli.Context) error {
if ctx.NArg() == 0 {
ctx.Command.Subcommands = nil // required to print usage of subcommand
_ = cli.ShowCommandHelp(ctx, ctx.Command.Name)
return fmt.Errorf("At least one file is required")
}
return nil
},
Action: actions(LogMetadata, c.Action),

Flags: []cli.Flag{
newURLFlag(&c.PaperlessURL),
Expand All @@ -50,10 +57,6 @@ func newUploadCommand() *UploadCommand {
func (c *UploadCommand) Action(ctx *cli.Context) error {
log := logr.FromContextOrDiscard(ctx.Context)

if ctx.NArg() == 0 {
return fmt.Errorf("at least one file is required")
}

params := paperless.UploadParams{}

if created := c.CreatedAt.Value(); created != nil {
Expand Down

0 comments on commit dcb74c8

Please sign in to comment.