Skip to content

Commit

Permalink
add cmd proto add
Browse files Browse the repository at this point in the history
  • Loading branch information
qloog committed Dec 10, 2021
1 parent a1feee5 commit 9b67d57
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 2 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,5 @@ bin/
vendor

# custom
cmd/eagle
eagle
config/config.yaml
config/config.yaml
69 changes: 69 additions & 0 deletions cmd/eagle/internal/proto/add/add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package add

import (
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
"golang.org/x/mod/modfile"
)

// CmdAdd represents the add command.
var CmdAdd = &cobra.Command{
Use: "add",
Short: "Add a proto API template",
Long: "Add a proto API template. Example: kratos add helloworld/v1/hello.proto",
Run: run,
}

// eg: eagle proto add helloworld/v1/helloworld.proto
func run(cmd *cobra.Command, args []string) {
input := args[0]
n := strings.LastIndex(input, "/")
if n == -1 {
fmt.Println("The proto path needs to be hierarchical.")
return
}
path := input[:n]
fileName := input[n+1:]
pkgName := strings.ReplaceAll(path, "/", ".")

p := &Proto{
Name: fileName,
Path: path,
Package: pkgName,
GoPackage: goPackage(path),
JavaPackage: javaPackage(pkgName),
Service: serviceName(fileName),
}
if err := p.Generate(); err != nil {
fmt.Println(err)
return
}
}

func modName() string {
modBytes, err := os.ReadFile("go.mod")
if err != nil {
if modBytes, err = os.ReadFile("../go.mod"); err != nil {
return ""
}
}
return modfile.ModulePath(modBytes)
}

func goPackage(path string) string {
s := strings.Split(path, "/")
return modName() + "/" + path + ";" + s[len(s)-1]
}

func javaPackage(name string) string {
return name
}

func serviceName(name string) string {
return export(strings.Split(name, ".")[0])
}

func export(s string) string { return strings.ToUpper(s[:1]) + s[1:] }
41 changes: 41 additions & 0 deletions cmd/eagle/internal/proto/add/proto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package add

import (
"fmt"
"os"
"path"
)

// Proto define a proto generator.
type Proto struct {
Name string
Path string
Package string
Service string
GoPackage string
JavaPackage string
}

// Generate generate a proto template.
func (p *Proto) Generate() error {
body, err := p.execute()
if err != nil {
return err
}
wd, err := os.Getwd()
if err != nil {
panic(err)
}
to := path.Join(wd, p.Path)
if _, err := os.Stat(to); os.IsNotExist(err) {
if err := os.MkdirAll(to, 0700); err != nil {
return err
}
}
name := path.Join(to, p.Name)
if _, err := os.Stat(name); !os.IsNotExist(err) {
return fmt.Errorf("%s already exists", p.Name)
}
return os.WriteFile(name, body, 0644)
return nil
}
52 changes: 52 additions & 0 deletions cmd/eagle/internal/proto/add/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package add

import (
"bytes"
"html/template"
"strings"
)

const protoTemplate = `
syntax = "proto3";
package {{.Package}};
option go_package = "{{.GoPackage}}";
option java_multiple_files = true;
option java_package = "{{.JavaPackage}}";
service {{.Service}} {
rpc Create{{.Service}} (Create{{.Service}}Request) returns (Create{{.Service}}Reply);
rpc Update{{.Service}} (Update{{.Service}}Request) returns (Update{{.Service}}Reply);
rpc Delete{{.Service}} (Delete{{.Service}}Request) returns (Delete{{.Service}}Reply);
rpc Get{{.Service}} (Get{{.Service}}Request) returns (Get{{.Service}}Reply);
rpc List{{.Service}} (List{{.Service}}Request) returns (List{{.Service}}Reply);
}
message Create{{.Service}}Request {}
message Create{{.Service}}Reply {}
message Update{{.Service}}Request {}
message Update{{.Service}}Reply {}
message Delete{{.Service}}Request {}
message Delete{{.Service}}Reply {}
message Get{{.Service}}Request {}
message Get{{.Service}}Reply {}
message List{{.Service}}Request {}
message List{{.Service}}Reply {}
`

func (p *Proto) execute() ([]byte, error) {
buf := new(bytes.Buffer)
tmpl, err := template.New("proto").Parse(strings.TrimSpace(protoTemplate))
if err != nil {
return nil, err
}
if err := tmpl.Execute(buf, p); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
22 changes: 22 additions & 0 deletions cmd/eagle/internal/proto/proto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package proto

import (
"github.com/go-eagle/eagle/cmd/eagle/internal/proto/add"

"github.com/spf13/cobra"
)

// CmdProto represents the proto command.
var CmdProto = &cobra.Command{
Use: "proto",
Short: "Generate the proto files",
Long: "Generate the proto files.",
Run: run,
}

func init() {
CmdProto.AddCommand(add.CmdAdd)
}

func run(cmd *cobra.Command, args []string) {
}

0 comments on commit 9b67d57

Please sign in to comment.