2021-03-10 19:17:13 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-03-11 17:26:50 +00:00
|
|
|
"strings"
|
2021-03-10 19:17:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func createGenericFiles(fpath string) error {
|
|
|
|
sep := string(filepath.Separator)
|
|
|
|
return createFile(fpath + sep + "README.md")
|
|
|
|
}
|
|
|
|
|
|
|
|
func initGit(fpath string) (outp string, err error) {
|
|
|
|
sep := string(filepath.Separator)
|
2021-03-12 10:56:01 +00:00
|
|
|
_ = createFile(fpath + sep + ".gitignore")
|
2021-03-11 17:26:50 +00:00
|
|
|
result, err := execute("git init")
|
|
|
|
return result, err
|
2021-03-10 19:17:13 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 17:26:50 +00:00
|
|
|
func createProject(projectType string, projectName string, projectPath string, git bool) (resultlist []string, errorlist []error) {
|
2021-03-10 19:17:13 +00:00
|
|
|
sep := string(filepath.Separator)
|
2021-03-11 17:26:50 +00:00
|
|
|
fpath := projectPath + sep + projectName
|
2021-03-10 19:17:13 +00:00
|
|
|
|
|
|
|
switch projectType {
|
|
|
|
case "python":
|
2021-03-11 17:26:50 +00:00
|
|
|
if err := createGenericFiles(fpath); err != nil {
|
|
|
|
errorlist = append(errorlist, err)
|
|
|
|
}
|
|
|
|
if err := os.Chdir(fpath); err != nil {
|
|
|
|
errorlist = append(errorlist, err)
|
|
|
|
}
|
|
|
|
if err := createDir(projectName); err != nil { //the application goes in a folder by the same name
|
|
|
|
errorlist = append(errorlist, err)
|
|
|
|
}
|
|
|
|
if err := createDir(projectName + "/tests"); err != nil { //the app tests go with the app
|
|
|
|
errorlist = append(errorlist, err)
|
|
|
|
}
|
|
|
|
if err := createFile(fpath + sep + "requirements.txt"); err != nil {
|
|
|
|
errorlist = append(errorlist, err)
|
|
|
|
}
|
|
|
|
if ! isInstalled("pipenv") {
|
|
|
|
_, err := execute("python3 -m pip install pipenv")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Cannot create virtual environment: ", err.Error())
|
2021-03-10 19:17:13 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-11 17:26:50 +00:00
|
|
|
result, err := execute("pipenv install") // use pipenv to generate dependency files
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error(), result)
|
|
|
|
errorlist = append(errorlist, err)
|
|
|
|
}
|
|
|
|
for _, record := range strings.Split(result, "\n") {
|
|
|
|
if strings.Contains(record, "Virtualenv location"){
|
|
|
|
resultlist = append(resultlist, record)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case "go":
|
|
|
|
if err := createGenericFiles(fpath); err != nil {
|
|
|
|
errorlist = append(errorlist, err)
|
|
|
|
}
|
|
|
|
if err := os.Chdir(fpath); err != nil {
|
|
|
|
errorlist = append(errorlist, err)
|
|
|
|
}
|
|
|
|
if err := createDir("vendor"); err != nil {
|
|
|
|
errorlist = append(errorlist, err)
|
|
|
|
}
|
|
|
|
if err := createDir("build"); err != nil {
|
|
|
|
errorlist = append(errorlist, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := execute("go mod init " + projectName) // use mod init to generate the go.mod
|
|
|
|
if err != nil {
|
|
|
|
errorlist = append(errorlist, err)
|
|
|
|
}
|
|
|
|
resultlist = append(resultlist, strings.Split(result, "\n")[0]) //just the first line
|
|
|
|
|
|
|
|
if err := createFile(fpath + sep + "go.sum"); err != nil {
|
|
|
|
errorlist = append(errorlist, err)
|
2021-03-10 19:17:13 +00:00
|
|
|
}
|
2021-03-11 17:26:50 +00:00
|
|
|
|
2021-03-10 19:17:13 +00:00
|
|
|
default:
|
2021-03-12 10:56:01 +00:00
|
|
|
err := fmt.Errorf("Project type '%s' is not supported.\n", projectType)
|
2021-03-11 17:26:50 +00:00
|
|
|
errorlist = append(errorlist, err)
|
2021-03-10 19:17:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if git {
|
2021-03-11 17:26:50 +00:00
|
|
|
result, err := initGit(fpath)
|
|
|
|
result = strings.Split(result, "\n")[0] //strip the carriage return
|
|
|
|
if err != nil {
|
|
|
|
errorlist = append(errorlist, err)
|
2021-03-10 19:17:13 +00:00
|
|
|
}
|
2021-03-11 17:26:50 +00:00
|
|
|
resultlist = append(resultlist, result)
|
2021-03-10 19:17:13 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 17:26:50 +00:00
|
|
|
return resultlist, errorlist
|
2021-03-10 19:17:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
argCount := len(os.Args[1:])
|
|
|
|
|
2021-03-11 17:26:50 +00:00
|
|
|
var (
|
|
|
|
projectName string
|
|
|
|
projectType string
|
|
|
|
projectPath string
|
|
|
|
git bool
|
|
|
|
)
|
2021-03-10 19:17:13 +00:00
|
|
|
|
|
|
|
flag.StringVar(&projectName, "n", "", "Name of project.")
|
|
|
|
flag.StringVar(&projectType, "t", "go", "Type of project.")
|
|
|
|
flag.StringVar(&projectPath, "p", ".", "Directory path for project.")
|
|
|
|
flag.BoolVar(&git, "g", false, "Initialize git repo.")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if argCount == 0 {
|
|
|
|
flag.Usage()
|
|
|
|
}
|
|
|
|
|
|
|
|
if argCount > 0 {
|
|
|
|
if projectName == "" {
|
|
|
|
fmt.Println("Oops! No project name is provided. What do you want to call your project?")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2021-03-11 17:26:50 +00:00
|
|
|
results, errs := createProject(projectType, projectName, projectPath, git)
|
|
|
|
for _, err := range errs {
|
|
|
|
fmt.Println("ERR: ", err)
|
|
|
|
}
|
|
|
|
result := fmt.Sprintf(
|
|
|
|
"Created a '%s' project named '%s' at directory '%s'\n", projectType, projectName, projectPath)
|
|
|
|
results = append(results, result)
|
|
|
|
for _, result := range results {
|
|
|
|
fmt.Println(result)
|
2021-03-10 19:17:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|