Skip to content

Commit

Permalink
Puppetfile validation (#118)
Browse files Browse the repository at this point in the history
* move regex for trailing comma to variable to be consistent with other regex

* add `-validate` flag and helper to output validation messages
  • Loading branch information
preisbeck authored and xorpaul committed Nov 29, 2018
1 parent e7f5d6a commit 94c53d4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
11 changes: 10 additions & 1 deletion config.go
Expand Up @@ -99,6 +99,10 @@ func readConfigfile(configFile string) ConfigSettings {
config.MaxExtractworker = 20
}

if validate {
Validatef()
}

return config
}

Expand All @@ -110,6 +114,7 @@ func preparePuppetfile(pf string) string {
}
defer file.Close()

reComma := regexp.MustCompile(",\\s*$")
reComment := regexp.MustCompile("^\\s*#")
reEmpty := regexp.MustCompile("^$")

Expand All @@ -122,7 +127,7 @@ func preparePuppetfile(pf string) string {
Debugf("found inline comment in " + pf + "line: " + line)
line = strings.Split(line, "#")[0]
}
if regexp.MustCompile(",\\s*$").MatchString(line) {
if reComma.MatchString(line) {
pfString += line
Debugf("adding line:" + line)
} else {
Expand Down Expand Up @@ -383,6 +388,10 @@ func readPuppetfile(pf string, sshKey string, source string, forceForgeVersions
moduleDirs = append(moduleDirs, moduleDir)
}

if validate {
Validatef()
}

puppetFile.moduleDirs = moduleDirs
//fmt.Println(puppetFile)
return puppetFile
Expand Down
2 changes: 2 additions & 0 deletions g10k.go
Expand Up @@ -23,6 +23,7 @@ var (
pfMode bool
pfLocation string
dryRun bool
validate bool
check4update bool
checkSum bool
gitObjectSyntaxNotSupported bool
Expand Down Expand Up @@ -187,6 +188,7 @@ func main() {
flag.StringVar(&pfLocation, "puppetfilelocation", "./Puppetfile", "which Puppetfile to use in -puppetfile mode")
flag.BoolVar(&force, "force", false, "purge the Puppet environment directory and do a full sync")
flag.BoolVar(&dryRun, "dryrun", false, "do not modify anything, just print what would be changed")
flag.BoolVar(&validate, "validate", false, "only validate given configuration and exit")
flag.BoolVar(&usemove, "usemove", false, "do not use hardlinks to populate your Puppet environments with Puppetlabs Forge modules. Instead uses simple move commands and purges the Forge cache directory after each run! (Useful for g10k runs inside a Docker container)")
flag.BoolVar(&check4update, "check4update", false, "only check if the is newer version of the Puppet module avaialable. Does implicitly set dryrun to true")
flag.BoolVar(&checkSum, "checksum", false, "get the md5 check sum for each Puppetlabs Forge module and verify the integrity of the downloaded archive. Increases g10k run time!")
Expand Down
23 changes: 21 additions & 2 deletions helper.go
Expand Up @@ -17,6 +17,8 @@ import (
"github.com/kballard/go-shellquote"
)

var validationMessages []string

// Debugf is a helper function for debug logging if global variable debug is set to true
func Debugf(s string) {
if debug != false {
Expand Down Expand Up @@ -45,6 +47,19 @@ func Infof(s string) {
}
}

// Validatef is a helper function for validation logging if global variable validate is set to true
func Validatef() {
if len(validationMessages) > 0 {
for _, message := range validationMessages {
color.New(color.FgRed).Fprintln(os.Stdout, message)
}
os.Exit(1)
} else {
color.New(color.FgGreen).Fprintln(os.Stdout, "Configuration successfully parsed.")
os.Exit(0)
}
}

// Warnf is a helper function for warning logging
func Warnf(s string) {
color.Set(color.FgYellow)
Expand All @@ -54,8 +69,12 @@ func Warnf(s string) {

// Fatalf is a helper function for fatal logging
func Fatalf(s string) {
color.New(color.FgRed).Fprintln(os.Stderr, s)
os.Exit(1)
if validate {
validationMessages = append(validationMessages, s)
} else {
color.New(color.FgRed).Fprintln(os.Stderr, s)
os.Exit(1)
}
}

// fileExists checks if the given file exists and returns a bool
Expand Down

0 comments on commit 94c53d4

Please sign in to comment.