From fbe9df89b8076089d7286b515f073397da1e67af Mon Sep 17 00:00:00 2001 From: qloog Date: Sat, 8 Jan 2022 11:23:16 +0800 Subject: [PATCH] chore: add sub cmd for cache and repo --- .gitignore | 1 - cmd/eagle/internal/cache/{ => add}/add.go | 12 ++--- cmd/eagle/internal/cache/add/cache.go | 42 +++++++++++++++++ .../internal/cache/{ => add}/template.go | 12 +++-- cmd/eagle/internal/cache/cache.go | 47 +++++-------------- cmd/eagle/internal/proto/add/add.go | 2 +- cmd/eagle/internal/repo/{ => add}/add.go | 14 +++--- cmd/eagle/internal/repo/add/repo.go | 42 +++++++++++++++++ cmd/eagle/internal/repo/{ => add}/template.go | 2 +- cmd/eagle/internal/repo/repo.go | 46 +++++------------- cmd/eagle/main.go | 10 ++-- 11 files changed, 136 insertions(+), 94 deletions(-) rename cmd/eagle/internal/cache/{ => add}/add.go (65%) create mode 100644 cmd/eagle/internal/cache/add/cache.go rename cmd/eagle/internal/cache/{ => add}/template.go (92%) rename cmd/eagle/internal/repo/{ => add}/add.go (61%) create mode 100644 cmd/eagle/internal/repo/add/repo.go rename cmd/eagle/internal/repo/{ => add}/template.go (99%) diff --git a/.gitignore b/.gitignore index 1c437ef..4d6c86a 100644 --- a/.gitignore +++ b/.gitignore @@ -44,4 +44,3 @@ vendor # custom eagle -config/local/config.yaml diff --git a/cmd/eagle/internal/cache/add.go b/cmd/eagle/internal/cache/add/add.go similarity index 65% rename from cmd/eagle/internal/cache/add.go rename to cmd/eagle/internal/cache/add/add.go index d2478a6..f0c7864 100644 --- a/cmd/eagle/internal/cache/add.go +++ b/cmd/eagle/internal/cache/add/add.go @@ -1,4 +1,4 @@ -package cache +package add import ( "fmt" @@ -9,10 +9,10 @@ import ( ) // CmdCache represents the new command. -var CmdCache = &cobra.Command{ - Use: "cache", - Short: "Create a cache by template", - Long: "Create a cache using the cache template. Example: eagle cache UserCache", +var CmdAdd = &cobra.Command{ + Use: "add", + Short: "Create a cache file by template", + Long: "Create a cache file using the cache template. Example: eagle cache add UserCache", Run: run, } @@ -21,7 +21,7 @@ var ( ) func init() { - CmdCache.Flags().StringVarP(&targetDir, "-target-dir", "t", "internal/cache", "generate target directory") + CmdAdd.Flags().StringVarP(&targetDir, "-target-dir", "t", "internal/cache", "generate target directory") } func run(cmd *cobra.Command, args []string) { diff --git a/cmd/eagle/internal/cache/add/cache.go b/cmd/eagle/internal/cache/add/cache.go new file mode 100644 index 0000000..8b71ba6 --- /dev/null +++ b/cmd/eagle/internal/cache/add/cache.go @@ -0,0 +1,42 @@ +package add + +import ( + "fmt" + "io/ioutil" + "os" + "path" + + "github.com/go-eagle/eagle/cmd/eagle/internal/utils" +) + +// Cache is a cache generator. +type Cache struct { + Name string + Path string + Service string + Package string + ModName string +} + +// Generate generate a cache template. +func (c *Cache) Generate() error { + body, err := c.execute() + if err != nil { + return err + } + wd, err := os.Getwd() + if err != nil { + panic(err) + } + to := path.Join(wd, c.Path) + if _, err := os.Stat(to); os.IsNotExist(err) { + if err := os.MkdirAll(to, 0700); err != nil { + return err + } + } + name := path.Join(to, utils.Camel2Case(c.Name)+".go") + if _, err := os.Stat(name); !os.IsNotExist(err) { + return fmt.Errorf("%s already exists", c.Name) + } + return ioutil.WriteFile(name, body, 0644) +} diff --git a/cmd/eagle/internal/cache/template.go b/cmd/eagle/internal/cache/add/template.go similarity index 92% rename from cmd/eagle/internal/cache/template.go rename to cmd/eagle/internal/cache/add/template.go index f23e2d4..c1f1bf6 100644 --- a/cmd/eagle/internal/cache/template.go +++ b/cmd/eagle/internal/cache/add/template.go @@ -1,4 +1,4 @@ -package cache +package add import ( "bytes" @@ -15,10 +15,12 @@ import ( "fmt" "time" - "{{.ModName}}/internal/model" "github.com/go-eagle/eagle/pkg/cache" + "github.com/go-eagle/eagle/pkg/encoding" "github.com/go-eagle/eagle/pkg/log" "github.com/go-eagle/eagle/pkg/redis" + + "{{.ModName}}/internal/model" ) const ( @@ -28,15 +30,15 @@ const ( // {{.Name}}Cache define a cache struct type {{.Name}}Cache struct { - cache cache.Driver + cache cache.Cache } // New{{.Name}} new a cache func New{{.Name}}() *Cache { - encoding := cache.JSONEncoding{} + jsonEncoding := encoding.JSONEncoding{} cachePrefix := "" return &Cache{ - cache: cache.NewRedisCache(redis.RedisClient, cachePrefix, encoding, func() interface{} { + cache: cache.NewRedisCache(redis.RedisClient, cachePrefix, jsonEncoding, func() interface{} { return &model.{{.Name}}Model{} }), } diff --git a/cmd/eagle/internal/cache/cache.go b/cmd/eagle/internal/cache/cache.go index eb65028..b75ce1f 100644 --- a/cmd/eagle/internal/cache/cache.go +++ b/cmd/eagle/internal/cache/cache.go @@ -1,42 +1,21 @@ package cache import ( - "fmt" - "io/ioutil" - "os" - "path" - - "github.com/go-eagle/eagle/cmd/eagle/internal/utils" + "github.com/go-eagle/eagle/cmd/eagle/internal/cache/add" + "github.com/spf13/cobra" ) -// Cache is a cache generator. -type Cache struct { - Name string - Path string - Service string - Package string - ModName string +// CmdProto represents the proto command. +var CmdCache = &cobra.Command{ + Use: "cache", + Short: "Generate the cache file", + Long: "Generate the cache file.", + Run: run, } -// Generate generate a cache template. -func (c *Cache) Generate() error { - body, err := c.execute() - if err != nil { - return err - } - wd, err := os.Getwd() - if err != nil { - panic(err) - } - to := path.Join(wd, c.Path) - if _, err := os.Stat(to); os.IsNotExist(err) { - if err := os.MkdirAll(to, 0700); err != nil { - return err - } - } - name := path.Join(to, utils.Camel2Case(c.Name)+".go") - if _, err := os.Stat(name); !os.IsNotExist(err) { - return fmt.Errorf("%s already exists", c.Name) - } - return ioutil.WriteFile(name, body, 0644) +func init() { + CmdCache.AddCommand(add.CmdAdd) +} + +func run(cmd *cobra.Command, args []string) { } diff --git a/cmd/eagle/internal/proto/add/add.go b/cmd/eagle/internal/proto/add/add.go index 9b62f0d..aa3f10c 100644 --- a/cmd/eagle/internal/proto/add/add.go +++ b/cmd/eagle/internal/proto/add/add.go @@ -13,7 +13,7 @@ import ( var CmdAdd = &cobra.Command{ Use: "add", Short: "Add a proto API template", - Long: "Add a proto API template. eg: eagle add hello.proto", + Long: "Add a proto API template. eg: eagle proto add hello.proto", Run: run, } diff --git a/cmd/eagle/internal/repo/add.go b/cmd/eagle/internal/repo/add/add.go similarity index 61% rename from cmd/eagle/internal/repo/add.go rename to cmd/eagle/internal/repo/add/add.go index 9a2788c..c70d041 100644 --- a/cmd/eagle/internal/repo/add.go +++ b/cmd/eagle/internal/repo/add/add.go @@ -1,4 +1,4 @@ -package repo +package add import ( "fmt" @@ -8,11 +8,11 @@ import ( "github.com/spf13/cobra" ) -// CmdRepo represents the new command. -var CmdRepo = &cobra.Command{ - Use: "repo", - Short: "Create a repo by template", - Long: "Create a repo using the repo template. Example: eagle repo UserCache", +// CmdAdd represents the new command. +var CmdAdd = &cobra.Command{ + Use: "add", + Short: "Create a repo file by template", + Long: "Create a repo file using the repo template. Example: eagle repo add UserCache", Run: run, } @@ -21,7 +21,7 @@ var ( ) func init() { - CmdRepo.Flags().StringVarP(&targetDir, "-target-dir", "t", "internal/repository", "generate target directory") + CmdAdd.Flags().StringVarP(&targetDir, "-target-dir", "t", "internal/repository", "generate target directory") } func run(cmd *cobra.Command, args []string) { diff --git a/cmd/eagle/internal/repo/add/repo.go b/cmd/eagle/internal/repo/add/repo.go new file mode 100644 index 0000000..0a3d43b --- /dev/null +++ b/cmd/eagle/internal/repo/add/repo.go @@ -0,0 +1,42 @@ +package add + +import ( + "fmt" + "io/ioutil" + "os" + "path" + + "github.com/go-eagle/eagle/cmd/eagle/internal/utils" +) + +// Repo is a cache generator. +type Repo struct { + Name string + Path string + Service string + Package string + ModName string +} + +// Generate generate a cache template. +func (r *Repo) Generate() error { + body, err := r.execute() + if err != nil { + return err + } + wd, err := os.Getwd() + if err != nil { + panic(err) + } + to := path.Join(wd, r.Path) + if _, err := os.Stat(to); os.IsNotExist(err) { + if err := os.MkdirAll(to, 0700); err != nil { + return err + } + } + name := path.Join(to, utils.Camel2Case(r.Name)+"_repo.go") + if _, err := os.Stat(name); !os.IsNotExist(err) { + return fmt.Errorf("%s already exists", r.Name) + } + return ioutil.WriteFile(name, body, 0644) +} diff --git a/cmd/eagle/internal/repo/template.go b/cmd/eagle/internal/repo/add/template.go similarity index 99% rename from cmd/eagle/internal/repo/template.go rename to cmd/eagle/internal/repo/add/template.go index c2b2282..30943b0 100644 --- a/cmd/eagle/internal/repo/template.go +++ b/cmd/eagle/internal/repo/add/template.go @@ -1,4 +1,4 @@ -package repo +package add import ( "bytes" diff --git a/cmd/eagle/internal/repo/repo.go b/cmd/eagle/internal/repo/repo.go index 2134941..841eb39 100644 --- a/cmd/eagle/internal/repo/repo.go +++ b/cmd/eagle/internal/repo/repo.go @@ -1,42 +1,22 @@ package repo import ( - "fmt" - "io/ioutil" - "os" - "path" + "github.com/spf13/cobra" - "github.com/go-eagle/eagle/cmd/eagle/internal/utils" + "github.com/go-eagle/eagle/cmd/eagle/internal/repo/add" ) -// Repo is a cache generator. -type Repo struct { - Name string - Path string - Service string - Package string - ModName string +// CmdProto represents the proto command. +var CmdRepo = &cobra.Command{ + Use: "repo", + Short: "Generate the repo file", + Long: "Generate the repo file.", + Run: run, } -// Generate generate a cache template. -func (r *Repo) Generate() error { - body, err := r.execute() - if err != nil { - return err - } - wd, err := os.Getwd() - if err != nil { - panic(err) - } - to := path.Join(wd, r.Path) - if _, err := os.Stat(to); os.IsNotExist(err) { - if err := os.MkdirAll(to, 0700); err != nil { - return err - } - } - name := path.Join(to, utils.Camel2Case(r.Name)+"_repo.go") - if _, err := os.Stat(name); !os.IsNotExist(err) { - return fmt.Errorf("%s already exists", r.Name) - } - return ioutil.WriteFile(name, body, 0644) +func init() { + CmdRepo.AddCommand(add.CmdAdd) +} + +func run(cmd *cobra.Command, args []string) { } diff --git a/cmd/eagle/main.go b/cmd/eagle/main.go index 5a43422..12222a1 100644 --- a/cmd/eagle/main.go +++ b/cmd/eagle/main.go @@ -3,21 +3,19 @@ package main import ( "log" - "github.com/go-eagle/eagle/cmd/eagle/internal/repo" - - "github.com/go-eagle/eagle/cmd/eagle/internal/proto" + "github.com/spf13/cobra" "github.com/go-eagle/eagle/cmd/eagle/internal/cache" "github.com/go-eagle/eagle/cmd/eagle/internal/project" + "github.com/go-eagle/eagle/cmd/eagle/internal/proto" + "github.com/go-eagle/eagle/cmd/eagle/internal/repo" "github.com/go-eagle/eagle/cmd/eagle/internal/run" "github.com/go-eagle/eagle/cmd/eagle/internal/upgrade" - - "github.com/spf13/cobra" ) var ( // Version is the version of the compiled software. - Version = "v0.7.1" + Version = "v0.7.2" rootCmd = &cobra.Command{ Use: "eagle",