fuzzyworld

template / commit

Added a flag to use default config settings instead of loading from disk.

5e424f1b0adc9b748055a644a2fb7ba99e92d890 · Pavel Safronov · 2022-11-12 00:42:20 -0800 · parents 52e9ad7cab94

Added a flag to use default config settings instead of loading from disk.
Did a bit of reorg as well, to simplify.
Creating subfiles' subfolders now.
 .vscode/launch.json | 14 +++++++-----
 config_test.go      |  1 +
 default_config.go   | 19 +++++++++++++++
 main.go             | 66 +++++++++++++++++++++++------------------------------
 monkey-patch.go     |  1 +
 profile.go          |  9 ++++++++
 profile_test.go     |  2 +-
 7 files changed, 68 insertions(+), 44 deletions(-)

diff --git a/.vscode/launch.json b/.vscode/launch.json
index 22ee5d9..8602236 100644
--- a/.vscode/launch.json
+++ b/.vscode/launch.json
@@ -72,14 +72,16 @@
             ]
         },
         {
-            "name": "Run: template work",
+            "name": "Run: template work test-project --use-defaults",
             "type": "go",
             "request": "launch",
             "mode": "auto",
             "program": ".",
             "args": [
-                "work"
-            ]
+                "work",
+                "test-project",
+                "--use-defaults"
+            ],
         },
         {
             "name": "Run: template --profile work --project testProj",
@@ -121,7 +123,7 @@
                 "--log-level",
                 "debug",
             ]
-            ]
-        }
-    ]
+        ]
+    }
+]
 }
\ No newline at end of file
diff --git a/config_test.go b/config_test.go
index 46103ae..2453a0e 100644
--- a/config_test.go
+++ b/config_test.go
@@ -14,6 +14,7 @@ func compareObjectYaml(t *testing.T, expected, actual interface{}) {
 	assert.Nil(t, err, "Error marshalling expected")
 	actualYaml, err := getYaml(actual)
 	assert.Nil(t, err, "Error marshalling actual")
+	assert.Equal(t, len(expectedYaml), len(actualYaml), "Expected YAML and actual YAML are different lengths")
 	assert.Equal(t, expectedYaml, actualYaml)
 }
 
diff --git a/default_config.go b/default_config.go
index 6c9dc88..7b83074 100644
--- a/default_config.go
+++ b/default_config.go
@@ -183,6 +183,25 @@ echo "Let's test {{.ProjectName}}!"`,
 						},
 						Name: "repro.sh",
 					},
+					{
+						File: File{
+							Template: `{
+    "version": "0.2.0",
+    "configurations": [
+        {
+            "type": "bashdb",
+            "request": "launch",
+            "name": "Debug repo.sh",
+            "cwd": "${workspaceFolder}",
+            "program": "${workspaceFolder}/repro.sh",
+            "terminalKind": "integrated"
+        }
+    ]
+}
+							`,
+						},
+						Name: ".vscode/launch.json",
+					},
 				},
 			},
 			{
diff --git a/main.go b/main.go
index b033a45..b6aaf1f 100644
--- a/main.go
+++ b/main.go
@@ -22,6 +22,7 @@ type ArgumentValues struct {
 	userConfigPath string
 	extrasFilePath string
 	logLevel       string
+	useDefaults    bool
 }
 type Settings struct {
 	ArgumentValues
@@ -87,6 +88,13 @@ func main() {
 				Value:       defaultLogLevel,
 				Destination: &argumentValues.logLevel,
 			},
+			&cli.BoolFlag{
+				Name:        "use-defaults",
+				Aliases:     []string{"d"},
+				Usage:       "use default values for config",
+				Value:       false,
+				Destination: &argumentValues.useDefaults,
+			},
 		},
 		Commands: []*cli.Command{
 			{
@@ -97,10 +105,6 @@ func main() {
 					if err != nil {
 						return fmt.Errorf("error creating settings: %v", err)
 					}
-					err = setConfig(&settings)
-					if err != nil {
-						return fmt.Errorf("error setting config: %w", err)
-					}
 
 					config := settings.config
 					// list profiles
@@ -119,11 +123,6 @@ func main() {
 					if err != nil {
 						return fmt.Errorf("error creating settings: %v", err)
 					}
-					err = setConfig(&settings)
-					if err != nil {
-						return fmt.Errorf("error setting config: %w", err)
-					}
-
 					config := settings.config
 
 					// get profile information
@@ -170,10 +169,6 @@ func main() {
 			if err != nil {
 				return fmt.Errorf("error parsing arguments: %w", err)
 			}
-			err = setConfig(&settings)
-			if err != nil {
-				return fmt.Errorf("error setting config: %w", err)
-			}
 			err = mainLogic(settings)
 			if err != nil {
 				return fmt.Errorf("error in main logic: %w", err)
@@ -256,38 +251,35 @@ func createSettings(
 	fullConfigPath := getFullPath(settings.configFilePath, *usr)
 	settings.fullConfigPath = fullConfigPath
 
-	return settings, nil
-}
-
-func setConfig(settings *Settings) error {
-	fullConfigPath := settings.fullConfigPath
-	usr := settings.usr
-
-	config, err := loadConfig(fullConfigPath)
-	if err != nil {
-		return fmt.Errorf("error loading config from %v: %w", fullConfigPath, err)
+	var config Config
+	if settings.useDefaults {
+		config = defaultConfig()
 	} else {
-		fmt.Printf("Loaded config from %v\n", fullConfigPath)
-	}
-
-	// load user config, if present
-	fullUserConfigPath := getFullPath(settings.userConfigPath, *usr)
-	_, err = os.Stat(fullUserConfigPath)
-	if err == nil {
-		userConfig, err := loadConfig(fullUserConfigPath)
+		config, err = loadConfig(fullConfigPath)
 		if err != nil {
-			return fmt.Errorf("error loading user config from %v: %w", fullUserConfigPath, err)
+			return settings, fmt.Errorf("error loading config from %v: %w", fullConfigPath, err)
 		} else {
-			fmt.Printf("Loaded user config from %v, combining with global config\n", fullUserConfigPath)
-			config, err = mergeConfigs(config, userConfig)
+			fmt.Printf("Loaded config from %v\n", fullConfigPath)
+		}
+		// load user config, if present
+		fullUserConfigPath := getFullPath(settings.userConfigPath, *usr)
+		_, err = os.Stat(fullUserConfigPath)
+		if err == nil {
+			userConfig, err := loadConfig(fullUserConfigPath)
 			if err != nil {
-				return fmt.Errorf("error merging configs: %w", err)
+				return settings, fmt.Errorf("error loading user config from %v: %w", fullUserConfigPath, err)
+			} else {
+				fmt.Printf("Loaded user config from %v, combining with global config\n", fullUserConfigPath)
+				config, err = mergeConfigs(config, userConfig)
+				if err != nil {
+					return settings, fmt.Errorf("error merging configs: %w", err)
+				}
 			}
 		}
 	}
-
 	settings.config = config
-	return nil
+
+	return settings, nil
 }
 
 func findProfile(settings Settings) (Profile, error) {
diff --git a/monkey-patch.go b/monkey-patch.go
index 44e3e63..97c20d9 100644
--- a/monkey-patch.go
+++ b/monkey-patch.go
@@ -19,6 +19,7 @@ type monkeyPatch struct {
 	OSChdir     func(dir string) error
 	OSMkdir     func(path string, perm os.FileMode) error
 	OSReadDir   func(dirname string) ([]os.DirEntry, error)
+	OSStat      func(name string) (os.FileInfo, error)
 }
 type monkeyPatchCalls struct {
 	ExecCommandCalls []struct {
diff --git a/profile.go b/profile.go
index e1faa57..d04edae 100644
--- a/profile.go
+++ b/profile.go
@@ -239,6 +239,15 @@ func createEntity(
 				}
 				existingFiles = append(existingFiles, fileName)
 				subFilePath := filepath.Join(fullPath, fileName)
+				subDirPath := filepath.Dir(subFilePath)
+
+				if _, err := os.Stat(subDirPath); os.IsNotExist(err) {
+					fmt.Printf("Creating subdirectory: %v\n", subDirPath)
+					err = monkey.OSMkdir(subDirPath, os.ModePerm)
+					if err != nil {
+						return "", fmt.Errorf("error creating sub directory for file index %v : %v", index, err)
+					}
+				}
 				fmt.Printf("Creating sub file: %v\n", subFilePath)
 				err = createFile(filePrePatternData, file.File, subFilePath)
 				if err != nil {
diff --git a/profile_test.go b/profile_test.go
index 7d27ac0..f75e0ac 100644
--- a/profile_test.go
+++ b/profile_test.go
@@ -167,7 +167,7 @@ func TestCreateEntity(t *testing.T) {
 		assert.Equal(t, expectedFullPath, fullPath)
 		assert.Equal(t, 1, len(monkeyPatchCalls.OSChdirCalls))
 		assert.Equal(t, expectedFullPath, monkeyPatchCalls.OSChdirCalls[0].dir)
-		assert.Equal(t, 1, len(monkeyPatchCalls.OSMkdirCalls))
+		assert.Equal(t, 3, len(monkeyPatchCalls.OSMkdirCalls))
 		assert.Equal(t, expectedFullPath, monkeyPatchCalls.OSMkdirCalls[0].path)
 		assert.Equal(t, 2, len(monkeyPatchCalls.OSWriteFileCalls))
 		assert.Equal(t, filepath.Clean(expectedFullPath+"/readme.md"), monkeyPatchCalls.OSWriteFileCalls[0].filename)