diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/dirlist.go b/dirlist.go new file mode 100644 index 0000000..fe82e42 --- /dev/null +++ b/dirlist.go @@ -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 +} + diff --git a/filer.go b/filer.go index c3d3142..9f2a0c8 100644 --- a/filer.go +++ b/filer.go @@ -1,26 +1,28 @@ package main import ( - "log" "os" "path/filepath" ) -// for generating the project blanks -func createFile(fpath string) bool { - fl, err := newFile(fpath) - if err != nil { - log.Fatal(err) - return false +func createDir(fpath string) error { + if err := os.Mkdir(filepath.Dir(fpath + string(filepath.Separator)), 0770); err != nil { + return err } - fl.Close() - return true + return nil } -// in case we need a file we can edit -func newFile(fpath string) (*os.File, error) { +// for generating the project blanks +func createFile(fpath string) error { + // create directory automatically, if non-existent 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 } diff --git a/main.go b/main.go deleted file mode 100644 index bbf66e7..0000000 --- a/main.go +++ /dev/null @@ -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)) - } - } -} diff --git a/projector.go b/projector.go new file mode 100644 index 0000000..7481350 --- /dev/null +++ b/projector.go @@ -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) + } + +}