mirror of
https://github.com/go-eagle/eagle.git
synced 2025-09-26 20:41:26 +08:00
chore: add sub cmd for cache and repo
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -44,4 +44,3 @@ vendor
|
||||
|
||||
# custom
|
||||
eagle
|
||||
config/local/config.yaml
|
||||
|
@@ -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) {
|
42
cmd/eagle/internal/cache/add/cache.go
vendored
Normal file
42
cmd/eagle/internal/cache/add/cache.go
vendored
Normal 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)
|
||||
}
|
@@ -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{}
|
||||
}),
|
||||
}
|
45
cmd/eagle/internal/cache/cache.go
vendored
45
cmd/eagle/internal/cache/cache.go
vendored
@@ -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
|
||||
func init() {
|
||||
CmdCache.AddCommand(add.CmdAdd)
|
||||
}
|
||||
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 run(cmd *cobra.Command, args []string) {
|
||||
}
|
||||
|
@@ -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,
|
||||
}
|
||||
|
||||
|
@@ -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) {
|
42
cmd/eagle/internal/repo/add/repo.go
Normal file
42
cmd/eagle/internal/repo/add/repo.go
Normal 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)
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
package repo
|
||||
package add
|
||||
|
||||
import (
|
||||
"bytes"
|
@@ -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
|
||||
func init() {
|
||||
CmdRepo.AddCommand(add.CmdAdd)
|
||||
}
|
||||
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 run(cmd *cobra.Command, args []string) {
|
||||
}
|
||||
|
@@ -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",
|
||||
|
Reference in New Issue
Block a user