pass revision as common opts instead of package global

This commit is contained in:
Umputun
2018-09-10 10:44:22 -05:00
parent 75a1edcff3
commit a856fd9c7d
5 changed files with 19 additions and 15 deletions
+1 -1
View File
@@ -8,7 +8,7 @@ import (
"os"
"testing"
flags "github.com/jessevdk/go-flags"
"github.com/jessevdk/go-flags"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
+3 -3
View File
@@ -16,9 +16,6 @@ import (
"github.com/pkg/errors"
)
// Revision sets from main
var Revision = "unknown"
// CommonOptionsCommander extends flags.Commander with SetCommon
// All commands should implement this interfaces
type CommonOptionsCommander interface {
@@ -30,12 +27,15 @@ type CommonOptionsCommander interface {
type CommonOpts struct {
RemarkURL string
SharedSecret string
Revision string
}
// SetCommon satisfies CommonOptionsCommander interface and sets common option fields
// The method called by main for each command
func (c *CommonOpts) SetCommon(commonOpts CommonOpts) {
c.RemarkURL = commonOpts.RemarkURL
c.SharedSecret = commonOpts.SharedSecret
c.Revision = commonOpts.Revision
}
// fileParser used to convert template strings like blah-{{.SITE}}-{{.YYYYMMDD}} the final format
+7 -6
View File
@@ -149,7 +149,7 @@ func (s *ServerCommand) Execute(args []string) error {
log.Fatalf("[ERROR] failed to setup application, %+v", err)
}
if err = app.run(ctx); err != nil {
log.Printf("[INFO] remark terminated with error %+v", err)
log.Printf("[WARN] remark terminated with error %+v", err)
return err
}
log.Printf("[INFO] remark terminated")
@@ -170,17 +170,18 @@ func (s *ServerCommand) newServerApp() (*serverApp, error) {
storeEngine, err := s.makeDataStore()
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to make data store engine")
}
keyStore, err := s.makeKeyStore()
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to make key store")
}
adminStore, err := s.makeAdminStore()
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to make admin store")
}
dataService := &service.DataStore{
@@ -193,7 +194,7 @@ func (s *ServerCommand) newServerApp() (*serverApp, error) {
loadingCache, err := s.makeCache()
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to make cache")
}
// token TTL is 5 minutes, inactivity interval 7+ days by default
@@ -225,7 +226,7 @@ func (s *ServerCommand) newServerApp() (*serverApp, error) {
commentFormatter := store.NewCommentFormatter(imgProxy)
srv := &api.Rest{
Version: Revision,
Version: s.Revision,
DataService: dataService,
WebRoot: s.WebRoot,
RemarkURL: s.RemarkURL,
+2 -2
View File
@@ -142,7 +142,7 @@ func TestServerApp_Failed(t *testing.T) {
_, err := p.ParseArgs([]string{"--backup=/tmp", "--store.bolt.path=/dev/null"})
assert.Nil(t, err)
_, err = opts.newServerApp()
assert.EqualError(t, err, "can't initialize data store: failed to make boltdb for /dev/null/remark.db: "+
assert.EqualError(t, err, "failed to make data store engine: can't initialize data store: failed to make boltdb for /dev/null/remark.db: "+
"open /dev/null/remark.db: not a directory")
t.Log(err)
@@ -174,7 +174,7 @@ func TestServerApp_Failed(t *testing.T) {
opts.Store.Type = "blah"
_, err = opts.newServerApp()
assert.EqualError(t, err, "unsupported store type blah")
assert.EqualError(t, err, "failed to make data store engine: unsupported store type blah")
t.Log(err)
}
+6 -3
View File
@@ -28,15 +28,18 @@ var revision = "unknown"
func main() {
fmt.Printf("remark42 %s\n", revision)
cmd.Revision = revision
var opts Opts
p := flags.NewParser(&opts, flags.Default)
p.CommandHandler = func(command flags.Commander, args []string) error {
setupLog(opts.Dbg)
commonOpts := cmd.CommonOpts{RemarkURL: opts.RemarkURL, SharedSecret: opts.SharedSecret}
// commands implements CommonOptionsCommander to allow passing set of extra options defined for all commands
c := command.(cmd.CommonOptionsCommander)
c.SetCommon(commonOpts)
c.SetCommon(cmd.CommonOpts{
RemarkURL: opts.RemarkURL,
SharedSecret: opts.SharedSecret,
Revision: revision,
})
err := c.Execute(args)
if err != nil {
log.Printf("[ERROR] failed with %+v", err)