first rough implementation of go project generator

This commit is contained in:
Greg Gauthier 2021-03-10 19:17:13 +00:00
parent d9ce3e24a4
commit a17941238e
5 changed files with 129 additions and 32 deletions

0
README.md Normal file
View File

21
dirlist.go Normal file
View File

@ -0,0 +1,21 @@
package main
import (
"fmt"
"os"
)
func dirlist() []string {
var output []string
wd, _ := os.Getwd()
dirlist, _ := os.ReadDir(wd)
for _, item := range dirlist {
if os.DirEntry.IsDir(item) {
output = append(output, fmt.Sprintf(wd + "/" + os.DirEntry.Name(item) + "/"))
} else {
output = append(output, fmt.Sprintf(wd + "/" + os.DirEntry.Name(item)))
}
}
return output
}

View File

@ -1,26 +1,28 @@
package main package main
import ( import (
"log"
"os" "os"
"path/filepath" "path/filepath"
) )
// for generating the project blanks func createDir(fpath string) error {
func createFile(fpath string) bool { if err := os.Mkdir(filepath.Dir(fpath + string(filepath.Separator)), 0770); err != nil {
fl, err := newFile(fpath) return err
if err != nil {
log.Fatal(err)
return false
} }
fl.Close() return nil
return true
} }
// in case we need a file we can edit // for generating the project blanks
func newFile(fpath string) (*os.File, error) { func createFile(fpath string) error {
// create directory automatically, if non-existent
if err := os.MkdirAll(filepath.Dir(fpath), 0770); err != nil { if err := os.MkdirAll(filepath.Dir(fpath), 0770); err != nil {
return nil, err return err
} }
return os.Create(fpath) //create the file, but that's all
file, err := os.Create(fpath)
if err != nil {
return err
}
file.Close()
return nil
} }

19
main.go
View File

@ -1,19 +0,0 @@
package main
import (
"fmt"
"os"
)
func main() {
wd, _ := os.Getwd()
dirlist, _ := os.ReadDir(wd)
fmt.Println(wd)
for _, item := range dirlist {
if os.DirEntry.IsDir(item) {
fmt.Println(wd + "/" + os.DirEntry.Name(item) + "/")
} else {
fmt.Println(wd + "/" + os.DirEntry.Name(item))
}
}
}

93
projector.go Normal file
View File

@ -0,0 +1,93 @@
package main
import (
"errors"
"flag"
"fmt"
"os"
"path/filepath"
)
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)
fmt.Println("Initializing git repo...")
_ = createFile(fpath + sep + ".gitignore")
return execute("git init")
}
func createProject(projectType string, projectName string, projectPath string, git bool) (result string, err error) {
sep := string(filepath.Separator)
fpath := projectPath + sep + projectName
switch projectType {
case "python":
result = fmt.Sprintf("Created a python project named '%s' at directory '%s'\n", projectName, projectPath)
case "go":
var errors []error
errors = append(errors, createGenericFiles(fpath)) // Also creates base directory
errors = append(errors, os.Chdir(fpath)) // CD into the base directory
errors = append(errors, createDir( "vendor")) // create the vendor dir
errors = append(errors, createDir("build")) // create the build directory for binaries
_, ee := execute("go mod init " + projectName) // use mod init to generate the go.mod
errors = append(errors, ee)
errors = append(errors, createFile(fpath + sep + "go.sum")) // manually create the go.sum
for _, erline := range errors {
if erline != nil {
fmt.Println(erline)
}
}
for _, line := range dirlist() {
fmt.Println(line)
}
result = fmt.Sprintf("Created a go project named '%s' at directory '%s'\n", projectName, projectPath)
default:
return "", errors.New(fmt.Sprintf("Project type '%s' is not supported.\n", projectType))
}
if git {
res, errr := initGit(fpath)
if errr != nil {
fmt.Println(res)
}
}
return result, err
}
func main() {
argCount := len(os.Args[1:])
var projectName string
var projectType string
var projectPath string
var git bool
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)
}
result, err := createProject(projectType, projectName, projectPath, git)
if err != nil {
fmt.Println(err)
}
fmt.Println(result)
}
}