Created
August 29, 2019 19:14
-
-
Save EdneyMesquita/9934059d25c5e2036e3f337bc58c2a35 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"os" | |
"strings" | |
) | |
func main() { | |
cmd := os.Args[1] | |
switch cmd { | |
case "make:handler": | |
fmt.Println("Making handler...") | |
makeHandler(os.Args) | |
break | |
case "make:model": | |
fmt.Println("Making model...") | |
makeModel(os.Args) | |
case "version": | |
fmt.Println("0.1.10") | |
break | |
default: | |
fmt.Println("Undefined command") | |
} | |
} | |
//0 bin 1 make:handler 2 TestHandler 3 -package 4 handlers | |
func makeHandler(args []string) { | |
filename := args[2] | |
if i := strings.Index(filename, "/"); i == -1 { | |
filename = "server/handlers/" + filename | |
} | |
pkg := "handlers" | |
if len(args) >= 4 && args[3] == "-package" { | |
pkg = args[4] | |
} | |
createDirs(filename) | |
content := fmt.Sprintf(`package %s | |
import "net/http" | |
//SimpleHandlerFunc is a simple handler created by CLI | |
func SimpleHandlerFunc(w http.ResponseWriter, r *http.Request) { | |
} | |
`, pkg) | |
d1 := []byte(content) | |
err := ioutil.WriteFile(filename+".go", d1, 0644) | |
check(err) | |
fmt.Println("Handler created!") | |
} | |
//0 bin 1 make:model 2 TestModel 3 -package 4 models | |
func makeModel(args []string) { | |
filename := args[2] | |
if i := strings.Index(filename, "/"); i == -1 { | |
filename = "models/" + filename | |
} | |
pkg := "models" | |
if len(args) >= 4 && args[3] == "-package" { | |
pkg = args[4] | |
} | |
createDirs(filename) | |
content := fmt.Sprintf(`package %s | |
type ( | |
//SimpleModel is a simple model created by CLI | |
SimpleModel struct { | |
} | |
) | |
`, pkg) | |
d1 := []byte(content) | |
err := ioutil.WriteFile(filename+".go", d1, 0644) | |
check(err) | |
fmt.Println("Model created!") | |
} | |
func createDirs(filepath string) { | |
parts := strings.Split(filepath, "/") | |
n := len(parts) - 1 | |
parts = parts[0:n] | |
folders := strings.Join(parts[:], "/") | |
err := os.MkdirAll(folders, 0777) | |
check(err) | |
} | |
func check(e error) { | |
if e != nil { | |
panic(e) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment