forked from go-eagle/eagle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
185 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,5 @@ bin/ | |
vendor | ||
|
||
# custom | ||
cmd/eagle | ||
eagle | ||
config/config.yaml | ||
config/config.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} |