Skip to content

Commit

Permalink
Merge pull request #92 from daedaleanai/ja/OptionToPersistFlags
Browse files Browse the repository at this point in the history
Add config to persist flags optionally
  • Loading branch information
Javier-varez committed Nov 28, 2022
2 parents 33d20d6 + 31818d7 commit c83673d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
31 changes: 15 additions & 16 deletions RULES/core/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package core

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
Expand Down Expand Up @@ -225,31 +224,31 @@ func initializeFlag(flag flagInterface, name string, isInitialized *bool) {
Fatal("flag '%s' has disallowed value '%s'", name, info.Value)
}

func lockAndGetFlags() map[string]flagInfo {
func lockAndGetFlags(storePersistedFlags bool) map[string]flagInfo {
flagsLocked = true

flagInfo := map[string]flagInfo{}
flagValues := map[string]string{}
flagStrings := []string{}
for name, flag := range registeredFlags {
info := flag.info()
flagInfo[name] = info
flagValues[name] = info.Value
flagStrings = append(flagStrings, fmt.Sprintf("%s=%s", name, info.Value))
}

// Store current flag values in FLAGS.json file.
data, err := json.Marshal(flagValues)
if err != nil {
Fatal("failed to marshal config flag values: %s", err)
}
flagsFilePath := path.Join(input.OutputDir, flagsFileName)
if err := os.MkdirAll(filepath.Dir(flagsFilePath), os.ModePerm); err != nil {
Fatal("Failed to create directory for flags file: %s", err)
}
err = ioutil.WriteFile(flagsFilePath, data, fileMode)
if err != nil {
Fatal("failed to write config flag values: %s", err)
if storePersistedFlags {
// Store current flag values in FLAGS.json file.
data, err := json.Marshal(flagValues)
if err != nil {
Fatal("failed to marshal config flag values: %s", err)
}
flagsFilePath := path.Join(input.OutputDir, flagsFileName)
if err := os.MkdirAll(filepath.Dir(flagsFilePath), os.ModePerm); err != nil {
Fatal("Failed to create directory for flags file: %s", err)
}
err = ioutil.WriteFile(flagsFilePath, data, fileMode)
if err != nil {
Fatal("failed to write config flag values: %s", err)
}
}

return flagInfo
Expand Down
9 changes: 4 additions & 5 deletions RULES/core/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type generatorInput struct {
Layout string
SelectedTargets []string
BuildAnalyzerTargets bool
PersistFlags bool
}

type generatorOutput struct {
Expand All @@ -57,10 +58,8 @@ func checkHasAnySelectedTargetsOtherThanReports(vars map[string]interface{}) boo
}

func isAnalyzerReportTarget(target interface{}) bool {
if _, ok := target.(analyzerReportInterface); ok {
return true
}
return false
_, ok := target.(analyzerReportInterface)
return ok
}

func isTargetSelected(targetPath string) bool {
Expand All @@ -75,7 +74,7 @@ func isTargetSelected(targetPath string) bool {
func GeneratorMain(vars map[string]interface{}) {
output := generatorOutput{
Targets: map[string]targetInfo{},
Flags: lockAndGetFlags(),
Flags: lockAndGetFlags(input.PersistFlags),
}

for targetPath, variable := range vars {
Expand Down
6 changes: 5 additions & 1 deletion RULES/core/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ func loadInput() generatorInput {
os.Exit(1)
}

var input generatorInput
input := generatorInput{
// If the dbt binary is too old and does not provide this value, we must default to true
// to keep the old behavior
PersistFlags: true,
}
if err := json.Unmarshal(data, &input); err != nil {
fmt.Fprintf(os.Stderr, "Error: Could not parse DBT input: %s.\n", err)
os.Exit(1)
Expand Down

0 comments on commit c83673d

Please sign in to comment.