Skip to content

Commit

Permalink
move common options to cmd.go
Browse files Browse the repository at this point in the history
Timeout, admin password and site id are set in many commands,
and we need to take care of synchronising the descriptions
and flags between them.

This change moves these standard options to cmd.go importing them
in the same manner CommonOpts imported by all commands already.
  • Loading branch information
paskal committed Jul 24, 2022
1 parent 5db6e43 commit 1b7c982
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 34 deletions.
9 changes: 4 additions & 5 deletions backend/app/cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ import (
// BackupCommand set of flags and command for export
// ExportPath used as a separate element to leverage BACKUP_PATH. If ExportFile has a path (i.e. with /) BACKUP_PATH ignored.
type BackupCommand struct {
ExportPath string `short:"p" long:"path" env:"BACKUP_PATH" default:"./var/backup" description:"export path"`
ExportFile string `short:"f" long:"file" default:"userbackup-{{.SITE}}-{{.TS}}.gz" description:"file name"`
Site string `short:"s" long:"site" env:"SITE" default:"remark" description:"site name"`
Timeout time.Duration `long:"timeout" default:"60m" description:"export (backup) timeout"`
AdminPasswd string `long:"admin-passwd" env:"ADMIN_PASSWD" required:"true" description:"admin basic auth password"`
ExportPath string `short:"p" long:"path" env:"BACKUP_PATH" default:"./var/backup" description:"export path"`
ExportFile string `short:"f" long:"file" default:"userbackup-{{.SITE}}-{{.TS}}.gz" description:"file name"`

SupportCmdOpts
CommonOpts
}

Expand Down
16 changes: 8 additions & 8 deletions backend/app/cmd/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import (

// CleanupCommand set of flags and command for cleanup
type CleanupCommand struct {
Site string `short:"s" long:"site" env:"SITE" default:"remark" description:"site name"`
Dry bool `long:"dry" description:"dry mode, will not remove comments"`
From string `long:"from" description:"from yyyymmdd"`
To string `long:"to" description:"from yyyymmdd"`
BadWords []string `short:"w" long:"bword" description:"bad word(s)"`
BadUsers []string `short:"u" long:"buser" description:"bad user(s)"`
AdminPasswd string `long:"admin-passwd" env:"ADMIN_PASSWD" required:"true" description:"admin basic auth password"`
SetTitle bool `long:"title" description:"title mode, will not remove comments, but reset titles to page's title'"`
Dry bool `long:"dry" description:"dry mode, will not remove comments"`
From string `long:"from" description:"from yyyymmdd"`
To string `long:"to" description:"from yyyymmdd"`
BadWords []string `short:"w" long:"bword" description:"bad word(s)"`
BadUsers []string `short:"u" long:"buser" description:"bad user(s)"`
SetTitle bool `long:"title" description:"title mode, will not remove comments, but reset titles to page's title'"`

SupportCmdOpts
CommonOpts
}

Expand Down
14 changes: 14 additions & 0 deletions backend/app/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ type CommonOpts struct {
Revision string
}

// AdminPasswordOpt is shared among many commands for setting the server password
// and then using it for backup/restore and such
type AdminPasswordOpt struct {
AdminPasswd string `long:"admin-passwd" env:"ADMIN_PASSWD" default:"" description:"admin basic auth password"`
}

// SupportCmdOpts is set of commands shared among similar commands like backup/restore and such.
// Order of fields defines the help command output order.
type SupportCmdOpts struct {
Site string `short:"s" long:"site" env:"SITE" default:"remark" description:"site name"`
AdminPasswordOpt
Timeout time.Duration `long:"timeout" default:"60m" description:"timeout for the command run"`
}

// DeprecatedFlag contains information about deprecated option
type DeprecatedFlag struct {
Old string
Expand Down
10 changes: 4 additions & 6 deletions backend/app/cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@ import (
"net/http"
"os"
"strings"
"time"

log "github.com/go-pkgz/lgr"
)

// ImportCommand set of flags and command for import
type ImportCommand struct {
InputFile string `short:"f" long:"file" description:"input file name" required:"true"`
Provider string `short:"p" long:"provider" default:"disqus" choice:"disqus" choice:"wordpress" choice:"commento" description:"import format"` //nolint
Site string `short:"s" long:"site" env:"SITE" default:"remark" description:"site name"`
Timeout time.Duration `long:"timeout" default:"60m" description:"import timeout"`
AdminPasswd string `long:"admin-passwd" env:"ADMIN_PASSWD" required:"true" description:"admin basic auth password"`
InputFile string `short:"f" long:"file" description:"input file name" required:"true"`
Provider string `short:"p" long:"provider" default:"disqus" choice:"disqus" choice:"wordpress" choice:"commento" description:"import format"` //nolint

SupportCmdOpts
CommonOpts
}

Expand Down
8 changes: 3 additions & 5 deletions backend/app/cmd/remap.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@ import (
"io"
"net/http"
"os"
"time"

log "github.com/go-pkgz/lgr"
)

// RemapCommand set of flags and command for change linkage between comments to
// different urls based on given rules (input file)
type RemapCommand struct {
Site string `short:"s" long:"site" env:"SITE" default:"remark" description:"site name"`
InputFile string `short:"f" long:"file" description:"input file name" required:"true"`
AdminPasswd string `long:"admin-passwd" env:"ADMIN_PASSWD" required:"true" description:"admin basic auth password"`
Timeout time.Duration `long:"timeout" default:"60m" description:"remap timeout"`
InputFile string `short:"f" long:"file" description:"input file name" required:"true"`

SupportCmdOpts
CommonOpts
}

Expand Down
14 changes: 5 additions & 9 deletions backend/app/cmd/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ type RestoreCommand struct {
ImportPath string `short:"p" long:"path" env:"BACKUP_PATH" default:"./var/backup" description:"export path"`
ImportFile string `short:"f" long:"file" default:"userbackup-{{.SITE}}-{{.YYYYMMDD}}.gz" description:"file name" required:"true"`

Site string `short:"s" long:"site" env:"SITE" default:"remark" description:"site name"`
Timeout time.Duration `long:"timeout" default:"60m" description:"import timeout"`
AdminPasswd string `long:"admin-passwd" env:"ADMIN_PASSWD" required:"true" description:"admin basic auth password"`
SupportCmdOpts
CommonOpts
}

Expand All @@ -29,12 +27,10 @@ func (rc *RestoreCommand) Execute(args []string) error {
return err
}
importer := ImportCommand{
InputFile: fname,
Site: rc.Site,
Provider: "native",
Timeout: rc.Timeout,
AdminPasswd: rc.AdminPasswd,
CommonOpts: rc.CommonOpts,
InputFile: fname,
Provider: "native",
SupportCmdOpts: rc.SupportCmdOpts,
CommonOpts: rc.CommonOpts,
}
return importer.Execute(args)
}
2 changes: 1 addition & 1 deletion backend/app/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ type ServerCommand struct {

Sites []string `long:"site" env:"SITE" default:"remark" description:"site names" env-delim:","`
AnonymousVote bool `long:"anon-vote" env:"ANON_VOTE" description:"enable anonymous votes (works only with VOTES_IP enabled)"`
AdminPasswd string `long:"admin-passwd" env:"ADMIN_PASSWD" default:"" description:"admin basic auth password"`
BackupLocation string `long:"backup" env:"BACKUP_PATH" default:"./var/backup" description:"backups location"`
MaxBackupFiles int `long:"max-back" env:"MAX_BACKUP_FILES" default:"10" description:"max backups to keep"`
LegacyImageProxy bool `long:"img-proxy" env:"IMG_PROXY" description:"[deprecated, use image-proxy.http2https] enable image proxy"`
Expand Down Expand Up @@ -117,6 +116,7 @@ type ServerCommand struct {
} `group:"email" namespace:"email" env-namespace:"EMAIL"`
} `group:"auth" namespace:"auth" env-namespace:"AUTH"`

AdminPasswordOpt
CommonOpts

emailMsgTemplatePath string // used only in tests
Expand Down

0 comments on commit 1b7c982

Please sign in to comment.