-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ddef36b
Showing
6 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module scaffolder | ||
|
||
go 1.20 | ||
|
||
require gopkg.in/yaml.v3 v3.0.1 // indirect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package helper | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
/* | ||
Helper function to handle errors | ||
message is the error message to be sent, | ||
checkErr is if we should check err variable passed in err argument, like | ||
if err != nil { | ||
... | ||
} | ||
*/ | ||
func Fatal(message string, checkErr bool, err ...error) { | ||
if !checkErr { | ||
fmt.Println(message) | ||
os.Exit(1) | ||
} else { | ||
if len(err) > 0 && err[0] != nil { | ||
fmt.Println(message) | ||
os.Exit(1) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package helper | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
) | ||
|
||
// Initialize Git repository for specified project | ||
func Git(name string) { | ||
// Navigate to project directory | ||
os.Chdir(name) | ||
|
||
// Initialize git repository | ||
cmd := exec.Command("git", "init") | ||
err := cmd.Run() | ||
Fatal(fmt.Sprintf("Error initializing Git repository: %s", err), true, err) | ||
|
||
fmt.Println("Git repository initialized!") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"scaffolder/helper" | ||
"scaffolder/utils" | ||
) | ||
|
||
func main() { | ||
// Project name and path to YAML | ||
var name, yaml string | ||
// Initialize Git? | ||
var git bool | ||
|
||
// Define and parse flags | ||
flag.StringVar(&name, "name", "", "Project name") | ||
flag.StringVar(&yaml, "yaml", "", "Path to YAML with directory structure") | ||
flag.BoolVar(&git, "git", false, "Use git in project") | ||
flag.Parse() | ||
|
||
// If project name or path to yaml was not provied, print usage and exit with code 1 | ||
if name == "" || yaml == "" { | ||
helper.Fatal("Usage: scaffold --name <projname> --yaml <path> --git? <boolean> (without angle brackets, ? - optional)", false) | ||
} | ||
|
||
// Initialize Git repository if git is true (user agreed) | ||
if git { | ||
helper.Git(name) | ||
} | ||
|
||
// Scaffold the directory structure | ||
utils.Scaffold(name, yaml) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"scaffolder/helper" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
// Scaffold the directory structure | ||
func Scaffold(name string, yamlpath string) { | ||
// Read and get YAML data | ||
yamlData, err := os.ReadFile(yamlpath) | ||
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 | ||
|
||
// Unmarshal the YAML into our map | ||
err = yaml.Unmarshal(yamlData, &dirs) | ||
helper.Fatal(fmt.Sprintf("Error unmarshalling YAML: %s", err), true, err) | ||
|
||
// Create project folder | ||
err = os.Mkdir(name, 0755) | ||
helper.Fatal(fmt.Sprintf("Error creating project folder: %s", err), true, err) | ||
|
||
// Navigate to the project folder | ||
err = os.Chdir(name) | ||
helper.Fatal(fmt.Sprintf("Failed to navigate to project folder: %s", err), true, err) | ||
|
||
// Scaffold the directory structure :: iterating over the map | ||
for folder, files := range dirs { | ||
// Create the folders and subdirectories if necessary | ||
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 | ||
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) | ||
|
||
// 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) | ||
} | ||
} | ||
} |