Skip to content

Commit

Permalink
Reduce duplication between flag name and env var
Browse files Browse the repository at this point in the history
  • Loading branch information
hansmi committed Jan 19, 2024
1 parent d2b0e33 commit 40806ba
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 54 deletions.
15 changes: 15 additions & 0 deletions pkg/kpflag/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package kpflag

import (
"strings"

"github.com/alecthomas/kingpin/v2"
)

type builder struct {
g FlagGroup
}

func (b *builder) flag(name, help string) *kingpin.FlagClause {
return b.g.Flag(name, help).Envar(strings.ToUpper(name))
}
47 changes: 24 additions & 23 deletions pkg/kpflag/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,48 +10,49 @@ import (

// RegisterClient adds flags for creating a Paperless-ngx API client.
func RegisterClient(g FlagGroup, f *client.Flags) {
g.Flag("paperless_url", "Base URL for accessing Paperless.").
b := builder{g}

b.flag("paperless_url", "Base URL for accessing Paperless.").
PlaceHolder("URL").
Envar("PAPERLESS_URL").StringVar(&f.BaseURL)
StringVar(&f.BaseURL)

g.Flag("paperless_auth_token", "Authentication token for Paperless. Reading the token from a file is preferrable.").
b.flag("paperless_auth_token", "Authentication token for Paperless. Reading the token from a file is preferrable.").
PlaceHolder("TOKEN").
Envar("PAPERLESS_AUTH_TOKEN").StringVar(&f.AuthToken)
StringVar(&f.AuthToken)

g.Flag("paperless_auth_token_file", "File containing authentication token for Paperless.").
b.flag("paperless_auth_token_file", "File containing authentication token for Paperless.").
PlaceHolder("PATH").
Envar("PAPERLESS_AUTH_TOKEN_FILE").StringVar(&f.AuthTokenFile)
StringVar(&f.AuthTokenFile)

g.Flag("paperless_auth_username", "Username for HTTP basic authentication.").
b.flag("paperless_auth_username", "Username for HTTP basic authentication.").
PlaceHolder("NAME").
Envar("PAPERLESS_AUTH_USERNAME").StringVar(&f.AuthUsername)
StringVar(&f.AuthUsername)

g.Flag("paperless_auth_password", "Password for HTTP basic authentication. Reading the password from a file is preferrable.").
b.flag("paperless_auth_password", "Password for HTTP basic authentication. Reading the password from a file is preferrable.").
PlaceHolder("PASSWORD").
Envar("PAPERLESS_AUTH_PASSWORD").StringVar(&f.AuthPassword)
StringVar(&f.AuthPassword)

g.Flag("paperless_auth_password_file", "Username for HTTP basic authentication.").
b.flag("paperless_auth_password_file", "Username for HTTP basic authentication.").
PlaceHolder("PATH").
Envar("PAPERLESS_AUTH_PASSWORD_FILE").StringVar(&f.AuthPasswordFile)
StringVar(&f.AuthPasswordFile)

g.Flag("paperless_auth_gcp_service_account_key_file", "Authenticate using OpenID Connect (OIDC) ID tokens derived from a Google Cloud Platform service account key file.").
b.flag("paperless_auth_gcp_service_account_key_file", "Authenticate using OpenID Connect (OIDC) ID tokens derived from a Google Cloud Platform service account key file.").
PlaceHolder("PATH").
Envar("PAPERLESS_AUTH_GCP_SERVICE_ACCOUNT_KEY_FILE").StringVar(&f.AuthGCPServiceAccountKeyFile)
StringVar(&f.AuthGCPServiceAccountKeyFile)

g.Flag("paperless_auth_oidc_id_token_audience", "Target audience for OpenID Connect (OIDC) ID tokens. Defaults to the base URL.").
b.flag("paperless_auth_oidc_id_token_audience", "Target audience for OpenID Connect (OIDC) ID tokens. Defaults to the base URL.").
PlaceHolder("STRING").
Envar("PAPERLESS_AUTH_OIDC_ID_TOKEN_AUDIENCE").StringVar(&f.AuthOIDCIDTokenAudience)
StringVar(&f.AuthOIDCIDTokenAudience)

kpflagvalue.HTTPHeaderVar(
g.Flag("paperless_header", "HTTP headers to set on all requests to Paperless.").
PlaceHolder("KEY:VALUE").
Envar("PAPERLESS_HEADER"), &f.Header)
b.flag("paperless_header", "HTTP headers to set on all requests to Paperless.").
PlaceHolder("KEY:VALUE"),
&f.Header)

g.Flag("paperless_server_timezone", fmt.Sprintf("Timezone for parsing timestamps. Defaults to %q.", time.Local.String())).
b.flag("paperless_server_timezone", fmt.Sprintf("Timezone for parsing timestamps. Defaults to %q.", time.Local.String())).
PlaceHolder("AREA/LOCATION").
Envar("PAPERLESS_SERVER_TIMEZONE").StringVar(&f.ServerTimezone)
StringVar(&f.ServerTimezone)

g.Flag("paperless_client_debug", "Enable verbose logging messages.").
Envar("PAPERLESS_CLIENT_DEBUG").
b.flag("paperless_client_debug", "Enable verbose logging messages.").
BoolVar(&f.DebugMode)
}
56 changes: 29 additions & 27 deletions pkg/kpflag/postconsume.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,57 @@ import (
// RegisterPostConsume adds flags capturing Paperless-ngx post-consumption
// command information.
func RegisterPostConsume(g FlagGroup, f *postconsume.Flags) {
g.Flag("document_id", "Primary database key of the document.").
b := builder{g}

b.flag("document_id", "Primary database key of the document.").
PlaceHolder("INT").
Envar("DOCUMENT_ID").Int64Var(&f.DocumentID)
Int64Var(&f.DocumentID)

g.Flag("document_file_name", "Formatted filename, not including paths.").
b.flag("document_file_name", "Formatted filename, not including paths.").
PlaceHolder("NAME").
Envar("DOCUMENT_FILE_NAME").StringVar(&f.DocumentFilename)
StringVar(&f.DocumentFilename)

kpflagvalue.TimeVar(
g.Flag("document_created", "Date and time when the document was created.").
Envar("DOCUMENT_CREATED"), &f.DocumentCreated)
b.flag("document_created", "Date and time when the document was created."),
&f.DocumentCreated)

kpflagvalue.TimeVar(
g.Flag("document_modified", "Date and time when the document was last modified.").
Envar("DOCUMENT_MODIFIED"), &f.DocumentModified)
b.flag("document_modified", "Date and time when the document was last modified."),
&f.DocumentModified)

kpflagvalue.TimeVar(
g.Flag("document_added", "Date and time when the document was added.").
Envar("DOCUMENT_ADDED"), &f.DocumentAdded)
b.flag("document_added", "Date and time when the document was added."),
&f.DocumentAdded)

g.Flag("document_source_path", "Path to the original document file.").
b.flag("document_source_path", "Path to the original document file.").
PlaceHolder("PATH").
Envar("DOCUMENT_SOURCE_PATH").StringVar(&f.DocumentSourcePath)
StringVar(&f.DocumentSourcePath)

g.Flag("document_archive_path", "Path to the generated archive file (if any).").
b.flag("document_archive_path", "Path to the generated archive file (if any).").
PlaceHolder("PATH").
Envar("DOCUMENT_ARCHIVE_PATH").StringVar(&f.DocumentArchivePath)
StringVar(&f.DocumentArchivePath)

g.Flag("document_thumbnail_path", "Path to the generated thumbnail image.").
b.flag("document_thumbnail_path", "Path to the generated thumbnail image.").
PlaceHolder("PATH").
Envar("DOCUMENT_THUMBNAIL_PATH").StringVar(&f.DocumentThumbnailPath)
StringVar(&f.DocumentThumbnailPath)

g.Flag("document_download_url", "URL for document download.").
b.flag("document_download_url", "URL for document download.").
PlaceHolder("URL").
Envar("DOCUMENT_DOWNLOAD_URL").URLVar(&f.DocumentDownloadURL)
URLVar(&f.DocumentDownloadURL)

g.Flag("document_thumbnail_url", "URL for the document thumbnail image.").
b.flag("document_thumbnail_url", "URL for the document thumbnail image.").
PlaceHolder("URL").
Envar("DOCUMENT_THUMBNAIL_URL").URLVar(&f.DocumentThumbnailURL)
URLVar(&f.DocumentThumbnailURL)

g.Flag("document_correspondent", "Assigned correspondent (if any).").
Envar("DOCUMENT_CORRESPONDENT").StringVar(&f.DocumentCorrespondent)
b.flag("document_correspondent", "Assigned correspondent (if any).").
StringVar(&f.DocumentCorrespondent)

kpflagvalue.CommaSeparatedStringsVar(
g.Flag("document_tags", "Comma separated list of tags applied (if any).").
PlaceHolder("TAGS").
Envar("DOCUMENT_TAGS"), &f.DocumentTags)
b.flag("document_tags", "Comma separated list of tags applied (if any).").
PlaceHolder("TAGS"),
&f.DocumentTags)

g.Flag("document_original_filename", "Filename of original document.").
b.flag("document_original_filename", "Filename of original document.").
PlaceHolder("NAME").
Envar("DOCUMENT_ORIGINAL_FILENAME").StringVar(&f.DocumentOriginalFilename)
StringVar(&f.DocumentOriginalFilename)
}
10 changes: 6 additions & 4 deletions pkg/kpflag/preconsume.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import "github.com/hansmi/paperhooks/pkg/preconsume"
// RegisterPreConsume adds flags capturing Paperless-ngx pre-consumption
// command information.
func RegisterPreConsume(g FlagGroup, f *preconsume.Flags) {
g.Flag("document_source_path", "Original path of the consumed document.").
b := builder{g}

b.flag("document_source_path", "Original path of the consumed document.").
PlaceHolder("PATH").
Envar("DOCUMENT_SOURCE_PATH").StringVar(&f.DocumentSourcePath)
StringVar(&f.DocumentSourcePath)

g.Flag("document_working_path", "Path to a copy of the original that consumption will work on.").
b.flag("document_working_path", "Path to a copy of the original that consumption will work on.").
PlaceHolder("PATH").
Envar("DOCUMENT_WORKING_PATH").StringVar(&f.DocumentWorkingPath)
StringVar(&f.DocumentWorkingPath)
}

0 comments on commit 40806ba

Please sign in to comment.