Skip to content

Commit c804aa5

Browse files
committed
make sure that selected answers also update the config before processing starts. added config function to set options from answers.
1 parent 3b53a23 commit c804aa5

9 files changed

Lines changed: 93 additions & 19 deletions

File tree

cmd/drawbridge/drawbridge.go

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
log "github.com/sirupsen/logrus"
56
"os"
67
"time"
78

@@ -13,7 +14,6 @@ import (
1314
"github.com/analogj/drawbridge/pkg/version"
1415
"github.com/fatih/color"
1516
"github.com/urfave/cli/v2"
16-
"log"
1717
"strings"
1818
)
1919

@@ -99,6 +99,11 @@ OPTIONS:
9999
//UsageText: "doo - does the dooing",
100100
Action: func(c *cli.Context) error {
101101
fmt.Fprintln(c.App.Writer, c.Command.Usage)
102+
if c.Bool("debug") {
103+
log.SetLevel(log.DebugLevel)
104+
} else {
105+
log.SetLevel(log.InfoLevel)
106+
}
102107

103108
projectList, err := project.CreateProjectListFromProvidedAnswers(config)
104109
if err != nil {
@@ -206,6 +211,7 @@ OPTIONS:
206211
destServer = ""
207212
}
208213

214+
config.SetOptionsFromAnswers(answerData)
209215
connectAction := actions.ConnectAction{Config: config}
210216
return connectAction.Start(answerData, destServer)
211217
},
@@ -276,6 +282,7 @@ OPTIONS:
276282
}
277283
}
278284

285+
config.SetOptionsFromAnswers(answerData)
279286
downloadAction := actions.DownloadAction{Config: config}
280287
return downloadAction.Start(answerData, strRemoteHostname, strRemotePath, strLocalPath)
281288
},
@@ -320,7 +327,7 @@ OPTIONS:
320327
}
321328

322329
//delete one config file.
323-
330+
config.SetOptionsFromAnswers(answerData)
324331
deleteAction := actions.DeleteAction{Config: config}
325332
err = deleteAction.One(answerData, c.Bool("force"))
326333

@@ -404,6 +411,11 @@ func createFlags(appConfig config.Interface) ([]cli.Flag, error) {
404411
Usage: "Dry Run mode. Will print files and paths to STDOUT rather than writing them to disk.",
405412
Value: false,
406413
},
414+
&cli.BoolFlag{
415+
Name: "debug",
416+
Usage: "Enable verbose logging for debugging",
417+
Value: false,
418+
},
407419
}
408420

409421
configQuestions, err := appConfig.GetQuestions()
@@ -464,30 +476,40 @@ func createFlagHandler(appConfig config.Interface, answerValues map[string]inter
464476
// flag override
465477

466478
//get default defaultOptions from the config
467-
defaultOptions := map[string]interface{}{}
468-
appConfig.UnmarshalKey("options", &defaultOptions)
479+
options := map[string]interface{}{}
480+
appConfig.UnmarshalKey("options", &options)
481+
log.Debugf("\nDefault Options: %v", options)
482+
483+
optionKeys := []string{}
484+
for key := range options {
485+
optionKeys = append(optionKeys, key)
486+
}
469487

470488
cliAnswers := answerValues
471-
//find answer defaultOptions (and set them)
472-
for optionName, _ := range defaultOptions {
489+
490+
//find optionKeys in answerValues
491+
for _, optionKey := range optionKeys {
473492
//check if the key is set as an answer/default
474-
if answerOptionValue, ok := answerValues[optionName]; ok {
475-
//this answer is actuall for an option. lets set it.
476-
//fmt.Printf("Setting Option from Answer: %v (%v)", optionName, answerOptionValue)
477-
appConfig.SetDefault(fmt.Sprintf("options.%v", optionName), answerOptionValue)
493+
if answerOptionValue, ok := answerValues[optionKey]; ok {
494+
//this answer is actualy for an option. lets set it.
495+
log.Debugf("\nSetting option from Answer: %v (%v)", optionKey, answerOptionValue)
496+
options[optionKey] = answerOptionValue
497+
//appConfig.SetDefault(fmt.Sprintf("options.%v", optionKey), answerOptionValue)
478498
}
479499
}
480500

481501
for _, flagName := range cliFlags {
482502

483-
if _, ok := defaultOptions[flagName]; ok {
503+
if utils.SliceIncludes(optionKeys, flagName) {
484504
//this flag is actually an "option". Lets set it.
485-
appConfig.Set(fmt.Sprintf("options.%v", flagName), c.String(flagName))
505+
log.Debugf("\nSetting option from CLI: %v (%v)", flagName, c.String(flagName))
506+
options[flagName] = c.String(flagName)
507+
//appConfig.SetDefault(fmt.Sprintf("options.%v", flagName), c.String(flagName))
486508
continue
487509
}
488510

489-
//skip dryrun
490-
if flagName == "dryrun" {
511+
//skip dryrun & debug
512+
if flagName == "dryrun" || flagName == "debug" {
491513
continue
492514
}
493515

@@ -511,5 +533,14 @@ func createFlagHandler(appConfig config.Interface, answerValues map[string]inter
511533
}
512534
}
513535

536+
//set the config options section
537+
appConfig.Set("options", options)
538+
539+
if log.GetLevel() == log.DebugLevel {
540+
afterOptions := map[string]interface{}{}
541+
appConfig.UnmarshalKey("options", &afterOptions)
542+
log.Debugf("\nOptions after overrides: %v", afterOptions)
543+
}
544+
514545
return cliAnswers, nil
515546
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf
99
github.com/kvz/logstreamer v0.0.0-20150507115422-a635b98146f0
1010
github.com/mitchellh/go-homedir v1.1.0
11+
github.com/sirupsen/logrus v1.2.0
1112
github.com/spf13/viper v1.6.2
1213
github.com/stretchr/testify v1.5.1
1314
github.com/urfave/cli/v2 v2.2.0

pkg/actions/connect.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/analogj/drawbridge/pkg/config"
88
"github.com/analogj/drawbridge/pkg/errors"
99
"github.com/analogj/drawbridge/pkg/utils"
10+
log "github.com/sirupsen/logrus"
1011
"golang.org/x/crypto/ssh"
1112
"golang.org/x/crypto/ssh/agent"
1213
"io/ioutil"
@@ -22,8 +23,7 @@ type ConnectAction struct {
2223
}
2324

2425
func (e *ConnectAction) Start(answerData map[string]interface{}, destHostname string) error {
25-
26-
//"-c", "command1; command2; command3; ..."
26+
log.Debugf("Answer Data: %v", answerData)
2727

2828
tmplData, err := e.Config.GetActiveConfigTemplate()
2929
if err != nil {

pkg/actions/create.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/analogj/drawbridge/pkg/errors"
77
"github.com/analogj/drawbridge/pkg/utils"
88
"github.com/fatih/color"
9+
log "github.com/sirupsen/logrus"
910
"gopkg.in/yaml.v2"
1011
"path"
1112
"sort"
@@ -17,10 +18,12 @@ type CreateAction struct {
1718
}
1819

1920
func (e *CreateAction) Start(cliAnswerData map[string]interface{}, dryRun bool) error {
21+
log.Debugf("Answer Data: %v", cliAnswerData)
2022

2123
// prepare answer data with config.options
2224
answerData := map[string]interface{}{}
2325
e.Config.UnmarshalKey("options", &answerData)
26+
log.Debugf("Current Options: %v", answerData)
2427

2528
// add defaults into answerData
2629
questions, err := e.Config.GetQuestions()
@@ -45,6 +48,9 @@ func (e *CreateAction) Start(cliAnswerData map[string]interface{}, dryRun bool)
4548
questionKeys := utils.MapKeys(answerData)
4649
for _, questionKey := range questionKeys {
4750
if utils.SliceIncludes(e.Config.InternalQuestionKeys(), questionKey) {
51+
log.Debugf("%v: %v\n",
52+
questionKey,
53+
color.GreenString(fmt.Sprintf("%v", answerData[questionKey])))
4854
continue
4955
}
5056

@@ -158,7 +164,6 @@ func (e *CreateAction) queryResponse(questionKey string, question config.Questio
158164
err = question.Validate(questionKey, answerTyped)
159165
if err != nil {
160166
color.HiRed("%v\n", err)
161-
//fmt.Printf("%v\n", err)
162167
} else {
163168
return answerTyped
164169
}

pkg/actions/delete.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/analogj/drawbridge/pkg/config"
66
"github.com/analogj/drawbridge/pkg/utils"
77
"github.com/fatih/color"
8+
log "github.com/sirupsen/logrus"
89
"path"
910
"strings"
1011
)
@@ -24,6 +25,7 @@ func (e *DeleteAction) All(answerDataList []map[string]interface{}, force bool)
2425
return nil
2526
}
2627
func (e *DeleteAction) One(answerData map[string]interface{}, force bool) error {
28+
log.Debugf("Answer Data: %v", answerData)
2729

2830
//delete the config file by answerData
2931
renderedConfigFilePath := answerData["config"].(map[string]interface{})["filepath"].(string)

pkg/actions/download.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/analogj/drawbridge/pkg/config"
66
"github.com/analogj/drawbridge/pkg/errors"
77
"github.com/analogj/drawbridge/pkg/utils"
8+
log "github.com/sirupsen/logrus"
89
"os"
910
"os/exec"
1011
"path/filepath"
@@ -17,6 +18,7 @@ type DownloadAction struct {
1718
}
1819

1920
func (e *DownloadAction) Start(answerData map[string]interface{}, destHostname string, remoteFilePath string, localFilePath string) error {
21+
log.Debugf("Answer Data: %v", answerData)
2022

2123
tmplData, err := e.Config.GetActiveConfigTemplate()
2224
if err != nil {

pkg/config/config.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,3 +515,28 @@ func (c *configuration) GetActiveCustomTemplates() ([]template.FileTemplate, err
515515
}
516516
return activeTemplates, nil
517517
}
518+
519+
func (c *configuration) SetOptionsFromAnswers(answerValues map[string]interface{}) {
520+
521+
// get current options
522+
options := map[string]interface{}{}
523+
c.UnmarshalKey("options", &options)
524+
525+
optionKeys := []string{}
526+
for key := range options {
527+
optionKeys = append(optionKeys, key)
528+
}
529+
530+
//find optionKeys in answerValues
531+
for _, optionKey := range optionKeys {
532+
//check if the key is set as an answer/default
533+
if answerOptionValue, ok := answerValues[optionKey]; ok {
534+
//this answer is actualy for an option. lets set it.
535+
//logger.Debugf("\nSetting option from Answer: %v (%v)", optionKey, answerOptionValue)
536+
options[optionKey] = answerOptionValue
537+
}
538+
}
539+
540+
//set the updated options in the config.
541+
c.Set("options", options)
542+
}

pkg/config/interface.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ type Interface interface {
1212
ReadConfig(configFilePath string) error
1313
Set(key string, value interface{})
1414
SetDefault(key string, value interface{})
15+
SetOptionsFromAnswers(answerValues map[string]interface{})
16+
1517
AllSettings() map[string]interface{}
1618
IsSet(key string) bool
1719
Get(key string) interface{}

pkg/project/factory.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,17 @@ func parseAnswerFile(answerFilePath string) (projectData, error) {
107107

108108
//TODO: warn the user if the answer data would no longer render the same answers.yaml file.
109109

110+
answerDataConfig := answerData["config"].(map[string]interface{})
111+
pemFilePath := "" //this is an optional field (may be unset/nil in some configs)
112+
if val, ok := answerDataConfig["pem_filepath"]; ok {
113+
pemFilePath = val.(string)
114+
}
115+
110116
return projectData{
111117
Answers: answerData,
112118
AnswerFilePath: answerFilePath,
113-
ConfigFilePath: answerData["config"].(map[string]interface{})["filepath"].(string),
114-
PemFilePath: answerData["config"].(map[string]interface{})["pem_filepath"].(string),
119+
ConfigFilePath: answerDataConfig["filepath"].(string),
120+
PemFilePath: pemFilePath,
115121
}, nil
116122

117123
}

0 commit comments

Comments
 (0)