diff --git a/cmd/push.go b/cmd/push.go index faac688..1941bff 100644 --- a/cmd/push.go +++ b/cmd/push.go @@ -135,46 +135,58 @@ var pushCmd = &cobra.Command{ case "homerun-demo": fmt.Println("PUSHING TO HOMERUN-DEMO") - // CREATE map string interface for values + // MAP FOR VALUES values := make(map[string]interface{}) // READ PROFILE FOR MAY EXISTING PRE-SURVEY - preSurvey, _ := surveys.LoadQuestionFile(source, "preSurvey") - if len(preSurvey) > 0 { - preSurveyQuestions, preSurveyValues, err := surveys.BuildSurvey(preSurvey) - if err != nil { - log.Fatalf("ERROR BUILDING SURVEY: %v", err) - } - log.Info("PRE-SURVEY FOUND") + preSurveyValues := surveys.RunSurvey(source, "preSurvey") + values = sthingsBase.MergeMaps(preSurveyValues, values) - fmt.Println(preSurvey, preSurveyValues) + // READ PROFILE + var demo surveys.HomerunDemo + err := surveys.ReadProfileFile(source, &demo) + if err != nil { + log.Fatalf("Failed to read profile: %v", err) + } - err = preSurveyQuestions.Run() - if err != nil { - log.Fatalf("ERROR RUNNING SURVEY: %v", err) - } - values = sthingsBase.MergeMaps(preSurveyValues, values) + fmt.Println(demo) + fmt.Println(values) - } else { - log.Info("NO PRE-SURVEY FOUND") - } + // RENDER ALIASES + MERGE w/ VALUES + aliases := surveys.RenderAliases(demo.Aliases, values) + values = sthingsBase.MergeMaps(aliases, values) + + fmt.Println(values) + values = surveys.RunSurveyFiles(demo.Surveys, values) fmt.Println(values) - // READ + OUTPUT GIT PROFILE - gitConfig := surveys.ReadGitProfile(source) - fmt.Println(gitConfig) + // LOAD ALL QUESTION FILES + // for _, questionFile := range gitConfig.Questions { - fmt.Println(preSurvey) + // // RENDER QUESTION FILE + // renderedQuestionFilePath, err := sthingsBase.RenderTemplateInline(questionFile, renderOption, brackets[bracketFormat].begin, brackets[bracketFormat].end, allValues) + // if err != nil { + // log.Error("ERROR RENDERING QUESTION FILE: ", err) + // } + // log.Info("LOADING QUESTION FILE: ", string(renderedQuestionFilePath)) - // GET PRE-SURVEY AND DEFAULTS + // questions, _ := modules.LoadQuestionFile(string(renderedQuestionFilePath)) - // renderedTemplateFileName, err := sthingsBase.RenderTemplateInline(templateFilePaths[0], renderOption, brackets[bracketFormat].begin, brackets[bracketFormat].end, allValues) - // if err != nil { - // fmt.Println(err) + // if len(questions) > 0 { + // log.Info("LOADED QUESTIONS FROM FILE: ", len(questions)) + // } else { + // log.Warn("NO QUESTIONS FOUND IN FILE: ", string(renderedQuestionFilePath)) + // } + + // allQuestions = append(allQuestions, questions...) // } - // RENDER ALIASES + // READ SURVEYS + RUN SURVEYS + + // RENDER THE FIELDS + + // SEND MESSAGE case "homerun": diff --git a/profiles/homerun.yaml b/profiles/homerun.yaml index 66b09b9..5d4c04c 100644 --- a/profiles/homerun.yaml +++ b/profiles/homerun.yaml @@ -18,8 +18,17 @@ aliases: - "gitlab-survey:git" - "github-survey:git" + +values: + - gitlab + - github + + surveys: - tests/{{ .system }}-survey.yaml -templates: - - "tests/vsphere-vm.tf.tpl:{{ .vmName }}.tf" +renderInline: + gitlab: + - "tests/git.tpl" + github: + - "tests/git.tpl" diff --git a/surveys/questions.go b/surveys/questions.go index 2a52ff9..6fd6e6d 100644 --- a/surveys/questions.go +++ b/surveys/questions.go @@ -9,12 +9,29 @@ import ( "math/rand" "os" "strconv" + "strings" "time" "github.com/charmbracelet/huh" + sthingsBase "github.com/stuttgart-things/sthingsBase" "gopkg.in/yaml.v2" ) +type TemplateBracket struct { + begin string `mapstructure:"begin"` + end string `mapstructure:"end"` + regexPattern string `mapstructure:"regex-pattern"` +} + +var ( + renderOption = "missingkey=error" + brackets = map[string]TemplateBracket{ + "curly": {"{{", "}}", `\{\{(.*?)\}\}`}, + "square": {"[[", "]]", `\[\[(.*?)\]\]`}, + } + bracketFormat = "curly" +) + // QUESTION STRUCT TO HOLD THE QUESTION DATA FROM YAML type Question struct { Prompt string `yaml:"prompt"` @@ -66,15 +83,25 @@ func LoadQuestionFile(filename, yamlKey string) ([]*Question, error) { return nil, fmt.Errorf("key '%s' not found in YAML file", yamlKey) } -func ReadGitProfile(filename string) (HomerunDemo HomerunDemo) { - - if err := ReadYAML(filename, &HomerunDemo); err != nil { +func ReadProfileFile(filename string, target interface{}) error { + // Read and unmarshal the YAML file into the provided target + if err := ReadYAML(filename, target); err != nil { fmt.Printf("ERROR READING YAML FILE: %v\n", err) + return err } - return + return nil } +// func ReadGitProfile(filename string) (HomerunDemo HomerunDemo) { + +// if err := ReadYAML(filename, &HomerunDemo); err != nil { +// fmt.Printf("ERROR READING YAML FILE: %v\n", err) +// } + +// return +// } + // READYAML READS AND PARSES A YAML FILE INTO A PROVIDED STRUCT func ReadYAML(filename string, out interface{}) error { file, err := os.Open(filename) @@ -239,3 +266,76 @@ func RunSurvey(profilePath, surveyKey string) (surveyValues map[string]interface return surveyValues } + +func RenderAliases(aliases []string, allValues map[string]interface{}) map[string]interface{} { + + fmt.Println("ALL VALUES: ", allValues) + + for _, alias := range aliases { + + // SPLIT ALIAS KEY/VALUE BY : + aliasValues := strings.Split(alias, ":") + + // RENDER KEY + aliasKey, err := sthingsBase.RenderTemplateInline(aliasValues[0], renderOption, brackets[bracketFormat].begin, brackets[bracketFormat].end, allValues) + if err != nil { + fmt.Println(err) + } + + // RENDER VALUE + aliasValue, err := sthingsBase.RenderTemplateInline(aliasValues[1], renderOption, brackets[bracketFormat].begin, brackets[bracketFormat].end, allValues) + if err != nil { + fmt.Println(err) + } + + // ASSIGN ALIAS TO ALL VALUES + key := string(strings.TrimSpace(string(aliasKey))) + value := string(strings.TrimSpace(string(aliasValue))) + + allValues[string(key)] = string(value) + log.Info("ALIAS ADDED: ", key, ":", string(value)) + } + + return allValues +} + +func RunSurveyFiles(surveys []string, values map[string]interface{}) map[string]interface{} { + + var allQuestions []*Question + + // LOAD ALL QUESTION FILES + for _, questionFile := range surveys { + + // RENDER QUESTION FILE + renderedQuestionFilePath, err := sthingsBase.RenderTemplateInline(questionFile, renderOption, brackets[bracketFormat].begin, brackets[bracketFormat].end, values) + if err != nil { + log.Error("ERROR RENDERING QUESTION FILE: ", err) + } + + log.Info("LOADING QUESTION FILE: ", string(renderedQuestionFilePath)) + + questions, _ := LoadQuestionFile(string(renderedQuestionFilePath), "") + + if len(questions) > 0 { + log.Info("LOADED QUESTIONS FROM FILE: ", len(questions)) + } else { + log.Warn("NO QUESTIONS FOUND IN FILE: ", string(renderedQuestionFilePath)) + } + + allQuestions = append(allQuestions, questions...) + } + + survey, defaults, err := BuildSurvey(allQuestions) + if err != nil { + log.Fatalf("ERROR BUILDING SURVEY: %v", err) + } + + err = survey.Run() + if err != nil { + log.Fatalf("ERROR RUNNING SURVEY: %v", err) + } + + log.Info("DEFAULTS: ", defaults) + + return defaults +} diff --git a/tests/github-survey.yaml b/tests/github-survey.yaml new file mode 100644 index 0000000..2b1200b --- /dev/null +++ b/tests/github-survey.yaml @@ -0,0 +1,18 @@ +--- +- prompt: "Count of vms?" + name: vmCount + options: + - "1" + - "2" + - "3" + - "4" + - "5" + +- prompt: "VM CPU (Cores)?" + name: vmCpu + options: + - "4" + - "6" + - "8" + - "12" + - "16"