Skip to content

Commit

Permalink
do WeakDecode to parse non-string types
Browse files Browse the repository at this point in the history
also set mapstructure tags for labels and ini file configuration options consistency
  • Loading branch information
taraspos committed Nov 6, 2019
1 parent 12adf11 commit 589398e
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 21 deletions.
8 changes: 4 additions & 4 deletions cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ type Config struct {
middlewares.SaveConfig `mapstructure:",squash"`
middlewares.MailConfig `mapstructure:",squash"`
}
ExecJobs map[string]*ExecJobConfig `gcfg:"job-exec" mapstructure:",squash"`
RunJobs map[string]*RunJobConfig `gcfg:"job-run" mapstructure:",squash"`
ServiceJobs map[string]*RunServiceConfig `gcfg:"job-service-run" mapstructure:",squash"`
LocalJobs map[string]*LocalJobConfig `gcfg:"job-local" mapstructure:",squash"`
ExecJobs map[string]*ExecJobConfig `gcfg:"job-exec" mapstructure:"job-exec,squash"`
RunJobs map[string]*RunJobConfig `gcfg:"job-run" mapstructure:"job-run,squash"`
ServiceJobs map[string]*RunServiceConfig `gcfg:"job-service-run" mapstructure:"job-service-run,squash"`
LocalJobs map[string]*LocalJobConfig `gcfg:"job-local" mapstructure:"job-local,squash"`
}

// BuildFromDockerLabels buils a scheduler using the config from a docker labels
Expand Down
24 changes: 23 additions & 1 deletion cli/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/mcuadros/ofelia/core"
"github.com/mcuadros/ofelia/middlewares"
. "gopkg.in/check.v1"
)

Expand Down Expand Up @@ -179,11 +180,32 @@ func (s *SuiteConfig) TestLabelsConfig(c *C) {
},
Comment: "Exec jobs from non-service container, saves container name to be able to exect to",
},
{
Labels: map[string]map[string]string{
"some": map[string]string{
requiredLabel: "true",
serviceLabel: "true",
labelPrefix + "." + jobExec + ".job1.schedule": "schedule1",
labelPrefix + "." + jobExec + ".job1.command": "command1",
labelPrefix + "." + jobExec + ".job1.no-overlap": "true",
},
},
ExpectedConfig: Config{
ExecJobs: map[string]*ExecJobConfig{
"job1": &ExecJobConfig{ExecJob: core.ExecJob{BareJob: core.BareJob{
Schedule: "schedule1",
Command: "command1",
}},
OverlapConfig: middlewares.OverlapConfig{NoOverlap: true},
},
},
},
Comment: "Test job with 'no-overlap' set",
},
}

for _, t := range testcases {
var conf = Config{}

err := conf.buildFromDockerLabels(t.Labels)
c.Assert(err, IsNil)
c.Assert(conf, DeepEquals, t.ExpectedConfig)
Expand Down
8 changes: 4 additions & 4 deletions cli/docker-labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,25 +115,25 @@ func (c *Config) buildFromDockerLabels(labels map[string]map[string]string) erro
}

if len(execJobs) > 0 {
if err := mapstructure.Decode(execJobs, &c.ExecJobs); err != nil {
if err := mapstructure.WeakDecode(execJobs, &c.ExecJobs); err != nil {
return err
}
}

if len(localJobs) > 0 {
if err := mapstructure.Decode(localJobs, &c.LocalJobs); err != nil {
if err := mapstructure.WeakDecode(localJobs, &c.LocalJobs); err != nil {
return err
}
}

if len(serviceJobs) > 0 {
if err := mapstructure.Decode(serviceJobs, &c.ServiceJobs); err != nil {
if err := mapstructure.WeakDecode(serviceJobs, &c.ServiceJobs); err != nil {
return err
}
}

if len(runJobs) > 0 {
if err := mapstructure.Decode(runJobs, &c.RunJobs); err != nil {
if err := mapstructure.WeakDecode(runJobs, &c.RunJobs); err != nil {
return err
}
}
Expand Down
14 changes: 7 additions & 7 deletions middlewares/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ import (

// MailConfig configuration for the Mail middleware
type MailConfig struct {
SMTPHost string `gcfg:"smtp-host"`
SMTPPort int `gcfg:"smtp-port"`
SMTPUser string `gcfg:"smtp-user"`
SMTPPassword string `gcfg:"smtp-password"`
EmailTo string `gcfg:"email-to"`
EmailFrom string `gcfg:"email-from"`
MailOnlyOnError bool `gcfg:"mail-only-on-error"`
SMTPHost string `gcfg:"smtp-host" mapstructure:"smtp-host"`
SMTPPort int `gcfg:"smtp-port" mapstructure:"smtp-port"`
SMTPUser string `gcfg:"smtp-user" mapstructure:"smtp-user"`
SMTPPassword string `gcfg:"smtp-password" mapstructure:"smtp-password"`
EmailTo string `gcfg:"email-to" mapstructure:"email-to"`
EmailFrom string `gcfg:"email-from" mapstructure:"email-from"`
MailOnlyOnError bool `gcfg:"mail-only-on-error" mapstructure:"mail-only-on-error"`
}

// NewMail returns a Mail middleware if the given configuration is not empty
Expand Down
2 changes: 1 addition & 1 deletion middlewares/overlap.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "github.com/mcuadros/ofelia/core"

// OverlapConfig configuration for the Overlap middleware
type OverlapConfig struct {
NoOverlap bool `gcfg:"no-overlap"`
NoOverlap bool `gcfg:"no-overlap" mapstructure:"no-overlap"`
}

// NewOverlap returns a Overlap middleware if the given configuration is not empty
Expand Down
4 changes: 2 additions & 2 deletions middlewares/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (

// SaveConfig configuration for the Save middleware
type SaveConfig struct {
SaveFolder string `gcfg:"save-folder"`
SaveOnlyOnError bool `gcfg:"save-only-on-error"`
SaveFolder string `gcfg:"save-folder" mapstructure:"save-folder"`
SaveOnlyOnError bool `gcfg:"save-only-on-error" mapstructure:"save-only-on-error"`
}

// NewSave returns a Save middleware if the given configuration is not empty
Expand Down
4 changes: 2 additions & 2 deletions middlewares/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ var (

// SlackConfig configuration for the Slack middleware
type SlackConfig struct {
SlackWebhook string `gcfg:"slack-webhook"`
SlackOnlyOnError bool `gcfg:"slack-only-on-error"`
SlackWebhook string `gcfg:"slack-webhook" mapstructure:"webhook"`
SlackOnlyOnError bool `gcfg:"slack-only-on-error" mapstructure:"slack-only-on-error"`
}

// NewSlack returns a Slack middleware if the given configuration is not empty
Expand Down

0 comments on commit 589398e

Please sign in to comment.