From 6abaa4368b6fc41601cc04bd57f0f33c3d9ce117 Mon Sep 17 00:00:00 2001 From: astaxie Date: Mon, 11 Aug 2014 10:01:56 +0800 Subject: [PATCH] bee generate views --- g.go | 13 +++++++++++-- g_views.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 g_views.go diff --git a/g.go b/g.go index 86c1a14..993bbae 100644 --- a/g.go +++ b/g.go @@ -33,8 +33,8 @@ bee generate migration [filename] bee generate controller [controllerfile] generate RESTFul controllers -bee generate router [controllerfile] - generate router based on controllerfile +bee generate view [viewpath] + generate CRUD view in viewpath bee generate docs generate swagger doc file @@ -111,6 +111,15 @@ func generateCode(cmd *Command, args []string) { ColorLog("[HINT] Usage: bee generate controller [filename]\n") os.Exit(2) } + case "view": + if len(args) == 2 { + cname := args[1] + generateView(cname, curpath) + } else { + ColorLog("[ERRO] Wrong number of arguments\n") + ColorLog("[HINT] Usage: bee generate view [filename]\n") + os.Exit(2) + } default: ColorLog("[ERRO] command is missing\n") } diff --git a/g_views.go b/g_views.go new file mode 100644 index 0000000..160efb6 --- /dev/null +++ b/g_views.go @@ -0,0 +1,49 @@ +package main + +import ( + "os" + "path" +) + +// recipe +// admin/recipe +func generateView(vpath, crupath string) { + absvpath := path.Join(crupath, vpath) + os.MkdirAll(absvpath, os.ModePerm) + cfile := path.Join(absvpath, "index.tpl") + if f, err := os.OpenFile(cfile, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666); err == nil { + defer f.Close() + f.WriteString(cfile) + ColorLog("[INFO] Created: %v\n", cfile) + } else { + ColorLog("[ERRO] Could not create view file: %s\n", err) + os.Exit(2) + } + cfile = path.Join(absvpath, "show.tpl") + if f, err := os.OpenFile(cfile, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666); err == nil { + defer f.Close() + f.WriteString(cfile) + ColorLog("[INFO] Created: %v\n", cfile) + } else { + ColorLog("[ERRO] Could not create view file: %s\n", err) + os.Exit(2) + } + cfile = path.Join(absvpath, "create.tpl") + if f, err := os.OpenFile(cfile, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666); err == nil { + defer f.Close() + f.WriteString(cfile) + ColorLog("[INFO] Created: %v\n", cfile) + } else { + ColorLog("[ERRO] Could not create view file: %s\n", err) + os.Exit(2) + } + cfile = path.Join(absvpath, "edit.tpl") + if f, err := os.OpenFile(cfile, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666); err == nil { + defer f.Close() + f.WriteString(cfile) + ColorLog("[INFO] Created: %v\n", cfile) + } else { + ColorLog("[ERRO] Could not create view file: %s\n", err) + os.Exit(2) + } +}