Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dl-tg committed Jul 21, 2023
0 parents commit ddef36b
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 0 deletions.
5 changes: 5 additions & 0 deletions go.mod
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
3 changes: 3 additions & 0 deletions go.sum
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=
29 changes: 29 additions & 0 deletions helper/fatal.go
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)
}
}
}
20 changes: 20 additions & 0 deletions helper/git.go
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!")
}
34 changes: 34 additions & 0 deletions main.go
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)

}
52 changes: 52 additions & 0 deletions utils/scaffold.go
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)
}
}
}

0 comments on commit ddef36b

Please sign in to comment.