code cleanup; add python functionality; implement suggestions from artur

This commit is contained in:
Greg Gauthier 2021-03-11 17:26:50 +00:00
parent a17941238e
commit 9a70cac7f2
3 changed files with 94 additions and 35 deletions

2
.gitignore vendored
View File

@ -2,3 +2,5 @@
build/ build/
vendor/ vendor/
*.code-workspace *.code-workspace
.DS_Store

View File

@ -5,6 +5,14 @@ import (
"strings" "strings"
) )
func isInstalled(name string) bool {
cmd := exec.Command("/bin/sh", "-c", "command -v " + name)
if err := cmd.Run(); err != nil {
return false
}
return true
}
func execute(cmdstr string) (string, error) { func execute(cmdstr string) (string, error) {
cmdargs := strings.Split(cmdstr, " ") // string arrayified cmdargs := strings.Split(cmdstr, " ") // string arrayified
cmd := cmdargs[0] // command cmd := cmdargs[0] // command

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"strings"
) )
func createGenericFiles(fpath string) error { func createGenericFiles(fpath string) error {
@ -15,58 +16,99 @@ func createGenericFiles(fpath string) error {
func initGit(fpath string) (outp string, err error) { func initGit(fpath string) (outp string, err error) {
sep := string(filepath.Separator) sep := string(filepath.Separator)
fmt.Println("Initializing git repo...") err = createFile(fpath + sep + ".gitignore")
_ = createFile(fpath + sep + ".gitignore") result, err := execute("git init")
return execute("git init") return result, err
} }
func createProject(projectType string, projectName string, projectPath string, git bool) (resultlist []string, errorlist []error) {
func createProject(projectType string, projectName string, projectPath string, git bool) (result string, err error) {
sep := string(filepath.Separator) sep := string(filepath.Separator)
fpath := projectPath + sep + projectName fpath := projectPath + sep + projectName
switch projectType { switch projectType {
case "python": case "python":
result = fmt.Sprintf("Created a python project named '%s' at directory '%s'\n", projectName, projectPath) if err := createGenericFiles(fpath); err != nil {
case "go": errorlist = append(errorlist, err)
var errors []error }
errors = append(errors, createGenericFiles(fpath)) // Also creates base directory if err := os.Chdir(fpath); err != nil {
errors = append(errors, os.Chdir(fpath)) // CD into the base directory errorlist = append(errorlist, err)
errors = append(errors, createDir( "vendor")) // create the vendor dir }
errors = append(errors, createDir("build")) // create the build directory for binaries if err := createDir(projectName); err != nil { //the application goes in a folder by the same name
_, ee := execute("go mod init " + projectName) // use mod init to generate the go.mod errorlist = append(errorlist, err)
errors = append(errors, ee) }
errors = append(errors, createFile(fpath + sep + "go.sum")) // manually create the go.sum if err := createDir(projectName + "/tests"); err != nil { //the app tests go with the app
for _, erline := range errors { errorlist = append(errorlist, err)
if erline != nil { }
fmt.Println(erline) 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())
} }
} }
for _, line := range dirlist() { result, err := execute("pipenv install") // use pipenv to generate dependency files
fmt.Println(line) if err != nil {
fmt.Println(err.Error(), result)
errorlist = append(errorlist, err)
} }
result = fmt.Sprintf("Created a go project named '%s' at directory '%s'\n", projectName, projectPath) 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)
}
default: default:
return "", errors.New(fmt.Sprintf("Project type '%s' is not supported.\n", projectType)) err := errors.New(fmt.Sprintf("Project type '%s' is not supported.\n", projectType))
errorlist = append(errorlist, err)
} }
if git { if git {
res, errr := initGit(fpath) result, err := initGit(fpath)
if errr != nil { result = strings.Split(result, "\n")[0] //strip the carriage return
fmt.Println(res) if err != nil {
errorlist = append(errorlist, err)
} }
resultlist = append(resultlist, result)
} }
return result, err return resultlist, errorlist
} }
func main() { func main() {
argCount := len(os.Args[1:]) argCount := len(os.Args[1:])
var projectName string var (
var projectType string projectName string
var projectPath string projectType string
var git bool projectPath string
git bool
)
flag.StringVar(&projectName, "n", "", "Name of project.") flag.StringVar(&projectName, "n", "", "Name of project.")
flag.StringVar(&projectType, "t", "go", "Type of project.") flag.StringVar(&projectType, "t", "go", "Type of project.")
@ -83,11 +125,18 @@ func main() {
fmt.Println("Oops! No project name is provided. What do you want to call your project?") fmt.Println("Oops! No project name is provided. What do you want to call your project?")
os.Exit(1) os.Exit(1)
} }
result, err := createProject(projectType, projectName, projectPath, git) results, errs := createProject(projectType, projectName, projectPath, git)
if err != nil { if errs != nil {
fmt.Println(err) 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)
} }
fmt.Println(result)
} }
} }