Skip to content

Commit

Permalink
Using a Config struct to avoid duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
simcap committed Feb 28, 2017
1 parent 68594ff commit b9139ad
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 18 deletions.
4 changes: 4 additions & 0 deletions commands/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ func initCloudServicesHook(cmd *cobra.Command, args []string) error {
return nil
}

func initConfigStruct(cmd *cobra.Command, args []string) error {
return config.LoadConfig()
}

func initSyncerHook(cmd *cobra.Command, args []string) error {
sync.DefaultSyncer = sync.NewSyncer()
sync.DefaultSyncer.SetLogger(logger.DefaultLogger)
Expand Down
4 changes: 2 additions & 2 deletions commands/revert.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func init() {
var revertCmd = &cobra.Command{
Use: "revert",
Short: "Revert an template execution given an revert ID (see `awless log` to list revert ids)",
PersistentPreRun: applyHooks(initLoggerHook, initAwlessEnvHook, initCloudServicesHook, initSyncerHook, verifyNewVersionHook),
PersistentPreRun: applyHooks(initLoggerHook, initAwlessEnvHook, initConfigStruct, initCloudServicesHook, initSyncerHook, verifyNewVersionHook),
PersistentPostRunE: saveHistoryHook,

RunE: func(c *cobra.Command, args []string) error {
Expand All @@ -52,7 +52,7 @@ var revertCmd = &cobra.Command{

fmt.Printf("%s\n", reverted)

exitOn(runTemplate(reverted, getCurrentDefaults()))
exitOn(runTemplate(reverted))

return nil
},
Expand Down
24 changes: 8 additions & 16 deletions commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/spf13/cobra"
"github.com/wallix/awless/cloud"
awscloud "github.com/wallix/awless/cloud/aws"
"github.com/wallix/awless/config"
"github.com/wallix/awless/database"
"github.com/wallix/awless/graph"
"github.com/wallix/awless/logger"
Expand All @@ -52,7 +53,7 @@ func init() {
var runCmd = &cobra.Command{
Use: "run",
Short: "Run a template given a filepath",
PersistentPreRun: applyHooks(initLoggerHook, initAwlessEnvHook, initCloudServicesHook, initSyncerHook, verifyNewVersionHook),
PersistentPreRun: applyHooks(initLoggerHook, initAwlessEnvHook, initConfigStruct, initCloudServicesHook, initSyncerHook, verifyNewVersionHook),
PersistentPostRunE: saveHistoryHook,

RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -68,16 +69,16 @@ var runCmd = &cobra.Command{
templ, err := template.Parse(string(content))
exitOn(err)

exitOn(runTemplate(templ, getCurrentDefaults()))
exitOn(runTemplate(templ))

return nil
},
}

func runTemplate(templ *template.Template, defaults map[string]interface{}) error {
func runTemplate(templ *template.Template) error {
validateTemplate(templ)

resolved, err := templ.ResolveHoles(defaults)
resolved, err := templ.ResolveHoles(config.Config.Defaults)
exitOn(err)

if len(resolved) > 0 {
Expand Down Expand Up @@ -141,7 +142,7 @@ func runTemplate(templ *template.Template, defaults map[string]interface{}) erro
db.AddTemplateExecution(executed)

if err == nil && !executed.HasErrors() {
if autoSync, ok := defaults[database.SyncAuto]; ok && autoSync.(bool) {
if autoSync, ok := config.Config.Defaults[database.SyncAuto]; ok && autoSync.(bool) {
runSyncFor(newTempl)
}
}
Expand Down Expand Up @@ -206,15 +207,15 @@ func createDriverCommands(action string, entities []string) *cobra.Command {

templ.MergeParams(cliTpl.GetNormalizedParams())

exitOn(runTemplate(templ, getCurrentDefaults()))
exitOn(runTemplate(templ))
return nil
}
}

actionCmd.AddCommand(
&cobra.Command{
Use: templDef.Entity,
PersistentPreRun: applyHooks(initLoggerHook, initAwlessEnvHook, initCloudServicesHook, initSyncerHook, verifyNewVersionHook),
PersistentPreRun: applyHooks(initLoggerHook, initAwlessEnvHook, initConfigStruct, initCloudServicesHook, initSyncerHook, verifyNewVersionHook),
PersistentPostRunE: saveHistoryHook,
Short: fmt.Sprintf("%s a %s", strings.Title(action), templDef.Entity),
Long: fmt.Sprintf("%s a %s\n\tRequired params: %s\n\tExtra params: %s", strings.Title(templDef.Action), templDef.Entity, strings.Join(templDef.Required(), ", "), strings.Join(templDef.Extra(), ", ")),
Expand Down Expand Up @@ -264,15 +265,6 @@ func runSyncFor(tpl *template.Template) {
}
}

func getCurrentDefaults() map[string]interface{} {
db, err, dbclose := database.Current()
exitOn(err)
defaults, err := db.GetDefaults()
exitOn(err)
dbclose()
return defaults
}

func printReport(t *template.TemplateExecution) {
for _, done := range t.Executed {
var line bytes.Buffer
Expand Down
30 changes: 30 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package config

import (
"fmt"

"github.com/wallix/awless/database"
)

var Config *config

type config struct {
Defaults map[string]interface{}
}

func LoadConfig() error {
db, err, dbclose := database.Current()
if err != nil {
return fmt.Errorf("load config: %s", err)
}
defer dbclose()

defaults, err := db.GetDefaults()
if err != nil {
return fmt.Errorf("config: load defaults: %s", err)
}

Config = &config{defaults}

return nil
}
File renamed without changes.

0 comments on commit b9139ad

Please sign in to comment.