fuzzyworld

template / commit

Added some more profile validations:

b65fb5e95ac5a22f5c0c1460a0ef45865f2bc304 · Pavel Safronov · 2022-09-23 15:03:26 -0700 · parents 0e962bb62d17

Added some more profile validations:
- profiles must have names
- profiles must have script shell if postscript is set
- treating OS="" as "All"
 config.go  | 6 ++++++
 file.go    | 9 +++++++--
 profile.go | 3 ++-
 3 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/config.go b/config.go
index 21bff55..e092919 100644
--- a/config.go
+++ b/config.go
@@ -127,6 +127,9 @@ func validateConfig(config Config) error {
 
 	foundDefault := false
 	for _, profile := range config.Profiles {
+		if profile.Name == "" {
+			return fmt.Errorf("Profile name is required")
+		}
 		if profile.IsDefault {
 			if foundDefault {
 				return fmt.Errorf("Multiple default profiles found")
@@ -142,6 +145,9 @@ func validateConfig(config Config) error {
 		if profile.Type != FileType && profile.FileTemplatePath != "" {
 			return fmt.Errorf("File template path is not supported for profile %s, which has Type=%s", profile.Name, profile.Type)
 		}
+		if profile.ScriptShell == "" && profile.PostScript != nil && len(profile.PostScript) > 0 {
+			return fmt.Errorf("Script shell is required for profile %s", profile.Name)
+		}
 	}
 	return nil
 }
diff --git a/file.go b/file.go
index fdc1af9..149dd4f 100644
--- a/file.go
+++ b/file.go
@@ -26,19 +26,24 @@ func createTemplateFile(srcPath string, destPath string, projectName string, nam
 	now := time.Now()
 	filePatternData := createFilePatternData(now, projectName, name)
 
+	// standardize paths
 	srcPath = getFullPath(srcPath)
-	srcName := filepath.Base(srcPath)
+	destPath = getFullPath(destPath)
+
+	// parse template
+	srcName := filepath.Base(srcPath) // srcName is required because we are sending files to ParseFiles
 	template, err := template.New(srcName).ParseFiles(srcPath)
 	if err != nil {
 		return fmt.Errorf("Error parsing source file '%v': %v", srcPath, err)
 	}
 
-	destPath = getFullPath(destPath)
+	// create output file
 	writer, err := os.Create(destPath)
 	if err != nil {
 		return fmt.Errorf("Error creating destination file '%v': %v", destPath, err)
 	}
 
+	// write template to output file
 	err = template.Execute(writer, filePatternData)
 	if err != nil {
 		return fmt.Errorf("Error executing template: %v", err)
diff --git a/profile.go b/profile.go
index 8bc19d8..5049734 100644
--- a/profile.go
+++ b/profile.go
@@ -40,7 +40,8 @@ func processProfile(profile Profile, projectName string) error {
 	// decide if the profile should run on this OS
 	profileOs := profile.OS
 	isWindows := runtime.GOOS == "windows"
-	shouldRun := profileOs == "All" ||
+	shouldRun := profileOs == "" ||
+		profileOs == "All" ||
 		(profileOs == "Windows" && isWindows) ||
 		(profileOs == "Mac" && !isWindows)
 	if !shouldRun {