chore: add sub cmd for cache and repo

This commit is contained in:
qloog
2022-01-08 11:23:16 +08:00
parent d55b9420dc
commit fbe9df89b8
11 changed files with 136 additions and 94 deletions

1
.gitignore vendored
View File

@@ -44,4 +44,3 @@ vendor
# custom # custom
eagle eagle
config/local/config.yaml

View File

@@ -1,4 +1,4 @@
package cache package add
import ( import (
"fmt" "fmt"
@@ -9,10 +9,10 @@ import (
) )
// CmdCache represents the new command. // CmdCache represents the new command.
var CmdCache = &cobra.Command{ var CmdAdd = &cobra.Command{
Use: "cache", Use: "add",
Short: "Create a cache by template", Short: "Create a cache file by template",
Long: "Create a cache using the cache template. Example: eagle cache UserCache", Long: "Create a cache file using the cache template. Example: eagle cache add UserCache",
Run: run, Run: run,
} }
@@ -21,7 +21,7 @@ var (
) )
func init() { 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) { func run(cmd *cobra.Command, args []string) {

42
cmd/eagle/internal/cache/add/cache.go vendored Normal file
View File

@@ -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)
}

View File

@@ -1,4 +1,4 @@
package cache package add
import ( import (
"bytes" "bytes"
@@ -15,10 +15,12 @@ import (
"fmt" "fmt"
"time" "time"
"{{.ModName}}/internal/model"
"github.com/go-eagle/eagle/pkg/cache" "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/log"
"github.com/go-eagle/eagle/pkg/redis" "github.com/go-eagle/eagle/pkg/redis"
"{{.ModName}}/internal/model"
) )
const ( const (
@@ -28,15 +30,15 @@ const (
// {{.Name}}Cache define a cache struct // {{.Name}}Cache define a cache struct
type {{.Name}}Cache struct { type {{.Name}}Cache struct {
cache cache.Driver cache cache.Cache
} }
// New{{.Name}} new a cache // New{{.Name}} new a cache
func New{{.Name}}() *Cache { func New{{.Name}}() *Cache {
encoding := cache.JSONEncoding{} jsonEncoding := encoding.JSONEncoding{}
cachePrefix := "" cachePrefix := ""
return &Cache{ return &Cache{
cache: cache.NewRedisCache(redis.RedisClient, cachePrefix, encoding, func() interface{} { cache: cache.NewRedisCache(redis.RedisClient, cachePrefix, jsonEncoding, func() interface{} {
return &model.{{.Name}}Model{} return &model.{{.Name}}Model{}
}), }),
} }

View File

@@ -1,42 +1,21 @@
package cache package cache
import ( import (
"fmt" "github.com/go-eagle/eagle/cmd/eagle/internal/cache/add"
"io/ioutil" "github.com/spf13/cobra"
"os"
"path"
"github.com/go-eagle/eagle/cmd/eagle/internal/utils"
) )
// Cache is a cache generator. // CmdProto represents the proto command.
type Cache struct { var CmdCache = &cobra.Command{
Name string Use: "cache",
Path string Short: "Generate the cache file",
Service string Long: "Generate the cache file.",
Package string Run: run,
ModName string
} }
// Generate generate a cache template. func init() {
func (c *Cache) Generate() error { CmdCache.AddCommand(add.CmdAdd)
body, err := c.execute() }
if err != nil {
return err func run(cmd *cobra.Command, args []string) {
}
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)
} }

View File

@@ -13,7 +13,7 @@ import (
var CmdAdd = &cobra.Command{ var CmdAdd = &cobra.Command{
Use: "add", Use: "add",
Short: "Add a proto API template", 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, Run: run,
} }

View File

@@ -1,4 +1,4 @@
package repo package add
import ( import (
"fmt" "fmt"
@@ -8,11 +8,11 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
// CmdRepo represents the new command. // CmdAdd represents the new command.
var CmdRepo = &cobra.Command{ var CmdAdd = &cobra.Command{
Use: "repo", Use: "add",
Short: "Create a repo by template", Short: "Create a repo file by template",
Long: "Create a repo using the repo template. Example: eagle repo UserCache", Long: "Create a repo file using the repo template. Example: eagle repo add UserCache",
Run: run, Run: run,
} }
@@ -21,7 +21,7 @@ var (
) )
func init() { 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) { func run(cmd *cobra.Command, args []string) {

View File

@@ -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)
}

View File

@@ -1,4 +1,4 @@
package repo package add
import ( import (
"bytes" "bytes"

View File

@@ -1,42 +1,22 @@
package repo package repo
import ( import (
"fmt" "github.com/spf13/cobra"
"io/ioutil"
"os"
"path"
"github.com/go-eagle/eagle/cmd/eagle/internal/utils" "github.com/go-eagle/eagle/cmd/eagle/internal/repo/add"
) )
// Repo is a cache generator. // CmdProto represents the proto command.
type Repo struct { var CmdRepo = &cobra.Command{
Name string Use: "repo",
Path string Short: "Generate the repo file",
Service string Long: "Generate the repo file.",
Package string Run: run,
ModName string
} }
// Generate generate a cache template. func init() {
func (r *Repo) Generate() error { CmdRepo.AddCommand(add.CmdAdd)
body, err := r.execute() }
if err != nil {
return err func run(cmd *cobra.Command, args []string) {
}
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)
} }

View File

@@ -3,21 +3,19 @@ package main
import ( import (
"log" "log"
"github.com/go-eagle/eagle/cmd/eagle/internal/repo" "github.com/spf13/cobra"
"github.com/go-eagle/eagle/cmd/eagle/internal/proto"
"github.com/go-eagle/eagle/cmd/eagle/internal/cache" "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/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/run"
"github.com/go-eagle/eagle/cmd/eagle/internal/upgrade" "github.com/go-eagle/eagle/cmd/eagle/internal/upgrade"
"github.com/spf13/cobra"
) )
var ( var (
// Version is the version of the compiled software. // Version is the version of the compiled software.
Version = "v0.7.1" Version = "v0.7.2"
rootCmd = &cobra.Command{ rootCmd = &cobra.Command{
Use: "eagle", Use: "eagle",