-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
84 lines (72 loc) · 2.75 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"flag"
"fmt"
"os"
"runtime"
"scaffolder/helper"
"scaffolder/utils"
)
func main() {
// Project name and YAML config filename
var name, yaml string
// Initialize Git?
var git bool
var remember bool
var yamlPath string
var configPath string
var yamlVariables helper.YamlVariableMap = map[string]string{}
// Define and parse command-line flags
flag.StringVar(&name, "name", "", "Project name")
flag.StringVar(&yaml, "yaml", "", "Config to use")
flag.StringVar(&configPath, "configdir", "", "Path to custom config")
flag.BoolVar(&git, "git", false, "Use git in project")
flag.BoolVar(&remember, "remember", false, "Remember the config path")
flag.Var(&yamlVariables, "variables", "Set variables to be used as comma seperated key value pairs eg key:value,key2:value2 ")
flag.Parse()
// If the project name or path to the YAML file was not provided, print usage and exit with code 1
if name == "" || yaml == "" {
helper.Fatal("Usage: scaffold --name <projname> --yaml <configname> --configdir? <custom config path> --variables? <set variables> --git? <boolean> --remember? <boolean> (without angle brackets, ? - optional) ", false)
}
// Initialize Git repository if 'git' flag is true (user agreed)
if git {
helper.Git(name)
}
// Check and set the path to the YAML config file
if configPath == "" {
savedPath, err := helper.GetConfigDir()
helper.Fatal(fmt.Sprintf("Could not get config path: %s", err), true, err)
if savedPath == "" {
// Construct default paths for the YAML file based on the user's operating system
var unixDefaultPath string = helper.UnixPath(yaml)
var winDefaultPath string = os.Getenv("USERPROFILE") + "\\.scaffolder\\" + yaml + ".yaml"
var defaultPathExists bool
if runtime.GOOS == "windows" {
defaultPathExists = helper.ValidateYamlPath(winDefaultPath, &yamlPath)
} else {
defaultPathExists = helper.ValidateYamlPath(unixDefaultPath, &yamlPath)
}
// If the default path does not exist, try the YAML file in the current directory
if !defaultPathExists {
if !helper.ValidateYamlPath(fmt.Sprintf("./%s.yaml", yaml), &yamlPath) {
helper.Fatal(fmt.Sprintf("Could not find %s.yaml", yaml), false)
}
}
} else {
yamlPath = savedPath
}
} else {
// If a custom config path was provided, validate and use it
if !helper.ValidateYamlPath(fmt.Sprintf("%s/%s.yaml", configPath, yaml), &yamlPath) {
configPath = ""
// Store the config path in the database
}
}
// Store the path in the database
if remember {
err := helper.SaveConfigDir(yamlPath)
helper.Fatal(fmt.Sprintf("Could not save config path: %s", err), true, err)
}
// Scaffold the directory structure using the provided project name and YAML config path
utils.Scaffold(name, yamlPath, *&yamlVariables)
}