move common options to shared cmd

This commit is contained in:
Umputun
2018-09-05 16:42:46 -05:00
parent ba4cc38e40
commit add715a111
6 changed files with 14 additions and 28 deletions
-5
View File
@@ -22,11 +22,6 @@ type BackupCommand struct {
CommonOpts
}
// SetCommon satisfies main.CommonOptionsCommander interface and sets common options
func (ec *BackupCommand) SetCommon(commonOpts CommonOpts) {
ec.CommonOpts = commonOpts
}
// Execute runs export with ExportCommand parameters, entry point for "export" command
func (ec *BackupCommand) Execute(args []string) error {
log.Printf("[INFO] export to %s, site %s", ec.ExportPath, ec.Site)
+13
View File
@@ -19,12 +19,25 @@ import (
// Revision sets from main
var Revision = "unknown"
// CommonOptionsCommander extends flags.Commander with SetCommon
// All commands should implement this interfaces
type CommonOptionsCommander interface {
SetCommon(commonOpts CommonOpts)
Execute(args []string) error
}
// CommonOpts sets externally from main, shared across all commands
type CommonOpts struct {
RemarkURL string
SharedSecret string
}
// SetCommon satisfies CommonOptionsCommander interface and sets common option fields
func (c *CommonOpts) SetCommon(commonOpts CommonOpts) {
c.RemarkURL = commonOpts.RemarkURL
c.SharedSecret = commonOpts.SharedSecret
}
// fileParser used to convert template strings like blah-{{.SITE}}-{{.YYYYMMDD}} the final format
type fileParser struct {
site string
-5
View File
@@ -24,11 +24,6 @@ type ImportCommand struct {
CommonOpts
}
// SetCommon satisfies main.CommonOptionsCommander interface and sets common options
func (ic *ImportCommand) SetCommon(commonOpts CommonOpts) {
ic.CommonOpts = commonOpts
}
// Execute runs import with ImportCommand parameters, entry point for "import" command
func (ic *ImportCommand) Execute(args []string) error {
log.Printf("[INFO] import %s (%s), site %s", ic.InputFile, ic.Provider, ic.Site)
-5
View File
@@ -15,11 +15,6 @@ type RestoreCommand struct {
CommonOpts
}
// SetCommon satisfies main.CommonOptionsCommander interface and sets common options
func (rc *RestoreCommand) SetCommon(commonOpts CommonOpts) {
rc.CommonOpts = commonOpts
}
// Execute runs import with RestoreCommand parameters, entry point for "restore" command
// uses ImportCommand with constructed full file name
func (rc *RestoreCommand) Execute(args []string) error {
-5
View File
@@ -130,11 +130,6 @@ type serverApp struct {
terminated chan struct{}
}
// SetCommon satisfies CommonOptionsCommander interface and sets common options
func (s *ServerCommand) SetCommon(commonOpts CommonOpts) {
s.CommonOpts = commonOpts
}
// Execute is the entry point for "server" command, called by flag parser
func (s *ServerCommand) Execute(args []string) error {
log.Printf("[INFO] start server on port %d", s.Port)
+1 -8
View File
@@ -26,13 +26,6 @@ type Opts struct {
var revision = "unknown"
// CommonOptionsCommander extends flags.Commander with SetCommon
// All commands should implement this interfaces
type CommonOptionsCommander interface {
SetCommon(commonOpts cmd.CommonOpts)
Execute(args []string) error
}
func main() {
fmt.Printf("remark42 %s\n", revision)
cmd.Revision = revision
@@ -42,7 +35,7 @@ func main() {
p.CommandHandler = func(command flags.Commander, args []string) error {
setupLog(opts.Dbg)
commonOpts := cmd.CommonOpts{RemarkURL: opts.RemarkURL, SharedSecret: opts.SharedSecret}
c := command.(CommonOptionsCommander)
c := command.(cmd.CommonOptionsCommander)
c.SetCommon(commonOpts)
err := c.Execute(args)
if err != nil {