From 570df8a337028a9b8e905cd2088066490ce3d8cf Mon Sep 17 00:00:00 2001 From: Atharva Date: Wed, 7 Feb 2024 20:46:26 +0000 Subject: [PATCH 1/3] deep nesteed yaml added for better usage --- utils/scaffold.go | 57 ++++++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/utils/scaffold.go b/utils/scaffold.go index 2505ac6..5eaa3c2 100644 --- a/utils/scaffold.go +++ b/utils/scaffold.go @@ -5,6 +5,7 @@ import ( "os" "path/filepath" "regexp" + "strings" "github.com/dl-tg/scaffolder/helper" @@ -66,7 +67,7 @@ func Scaffold(name string, yamlpath string, setVariables map[string]string) { helper.Fatal(fmt.Sprintf("Failed to read YAML file: %s", err), true, err) // Create map for the directory structure - var dirs map[string]map[string]string + var dirs map[string]interface{} // Unmarshal the YAML into our map err = yaml.Unmarshal(yamlData, &dirs) @@ -82,26 +83,40 @@ func Scaffold(name string, yamlpath string, setVariables map[string]string) { } // Scaffold the directory structure :: iterating over the map - for folder, files := range dirs { - // Create the folders and subdirectories if necessary - folder = replaceVariables(folder, setVariables) - err = os.MkdirAll(folder, 0755) - helper.Fatal(fmt.Sprintf("Error creating folder %s: %v", folder, err), true, err) - - // Create the files :: iterating over files from the map and getting specified content - for fileName, content := range files { - // Construct a file path for the file - fileName = replaceVariables(fileName, setVariables) - filePath := filepath.Join(folder, fileName) - // Create the directories before creating the file - err = os.MkdirAll(filepath.Dir(filePath), 0755) - helper.Fatal(fmt.Sprintf("Error creating directories for %s: %v", filePath, err), true, err) - - //replace all variables with values set - content = replaceVariables(content, setVariables) - // Create the files at filePath path and add specified content - err = os.WriteFile(filePath, []byte(content), 0644) - helper.Fatal(fmt.Sprintf("Failed to create file %s: %s", fileName, err), true, err) + scaffoldDirs(".", dirs, setVariables) +} + +func scaffoldDirs(basePath string, dirs map[string]interface{}, setVariables map[string]string) { + for name, item := range dirs { + // Apply variable replacement to the name + name = replaceVariables(name, setVariables) + switch v := item.(type) { + case string: // If it's a file, create the file + createFile(basePath, name, v, setVariables) + case map[string]interface{}: // If it's a directory, recursively call scaffoldDirs + createDirectory(basePath, name, v, setVariables) + default: + if strings.Contains(name, ".") { + createFile(basePath, name, "", setVariables) + } else { + createDirectory(basePath, name, nil, setVariables) + } } } } + +func createFile(basePath, name, content string, setVariables map[string]string) { + filePath := filepath.Join(basePath, name) + content = replaceVariables(content, setVariables) + err := os.WriteFile(filePath, []byte(content), 0644) + helper.Fatal(fmt.Sprintf("Failed to create file %s: %s", filePath, err), true, err) +} + +func createDirectory(basePath, name string, content map[string]interface{}, setVariables map[string]string) { + newPath := filepath.Join(basePath, name) + err := os.Mkdir(newPath, 0755) + helper.Fatal(fmt.Sprintf("Error creating folder %s: %v", newPath, err), true, err) + if content != nil { + scaffoldDirs(newPath, content, setVariables) + } +} From 2bfd32f11c9b9422aeea61bdf25dc9791db70b37 Mon Sep 17 00:00:00 2001 From: Atharva Date: Wed, 7 Feb 2024 21:27:49 +0000 Subject: [PATCH 2/3] code refactoring --- utils/scaffold.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/utils/scaffold.go b/utils/scaffold.go index 5eaa3c2..bd2a5b5 100644 --- a/utils/scaffold.go +++ b/utils/scaffold.go @@ -94,7 +94,8 @@ func scaffoldDirs(basePath string, dirs map[string]interface{}, setVariables map case string: // If it's a file, create the file createFile(basePath, name, v, setVariables) case map[string]interface{}: // If it's a directory, recursively call scaffoldDirs - createDirectory(basePath, name, v, setVariables) + newPath := createDirectory(basePath, name, v, setVariables) + scaffoldDirs(newPath, v, setVariables) default: if strings.Contains(name, ".") { createFile(basePath, name, "", setVariables) @@ -112,11 +113,9 @@ func createFile(basePath, name, content string, setVariables map[string]string) helper.Fatal(fmt.Sprintf("Failed to create file %s: %s", filePath, err), true, err) } -func createDirectory(basePath, name string, content map[string]interface{}, setVariables map[string]string) { +func createDirectory(basePath, name string, content map[string]interface{}, setVariables map[string]string) string { newPath := filepath.Join(basePath, name) err := os.Mkdir(newPath, 0755) helper.Fatal(fmt.Sprintf("Error creating folder %s: %v", newPath, err), true, err) - if content != nil { - scaffoldDirs(newPath, content, setVariables) - } + return newPath } From 01ddae53260a9f8aed94c4eef543526e8e526c24 Mon Sep 17 00:00:00 2001 From: Atharva Date: Wed, 7 Feb 2024 21:29:31 +0000 Subject: [PATCH 3/3] code improvement --- utils/scaffold.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/utils/scaffold.go b/utils/scaffold.go index bd2a5b5..79f767e 100644 --- a/utils/scaffold.go +++ b/utils/scaffold.go @@ -94,13 +94,13 @@ func scaffoldDirs(basePath string, dirs map[string]interface{}, setVariables map case string: // If it's a file, create the file createFile(basePath, name, v, setVariables) case map[string]interface{}: // If it's a directory, recursively call scaffoldDirs - newPath := createDirectory(basePath, name, v, setVariables) + newPath := createDirectory(basePath, name) scaffoldDirs(newPath, v, setVariables) default: if strings.Contains(name, ".") { createFile(basePath, name, "", setVariables) } else { - createDirectory(basePath, name, nil, setVariables) + createDirectory(basePath, name) } } } @@ -113,7 +113,7 @@ func createFile(basePath, name, content string, setVariables map[string]string) helper.Fatal(fmt.Sprintf("Failed to create file %s: %s", filePath, err), true, err) } -func createDirectory(basePath, name string, content map[string]interface{}, setVariables map[string]string) string { +func createDirectory(basePath, name string) string { newPath := filepath.Join(basePath, name) err := os.Mkdir(newPath, 0755) helper.Fatal(fmt.Sprintf("Error creating folder %s: %v", newPath, err), true, err)