Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
24951f7
chore: update thriftgo
li-jin-gou Mar 7, 2025
44d3a9d
chore: update thriftgo
li-jin-gou Mar 7, 2025
f7aeea6
chore: update thriftgo
li-jin-gou Mar 7, 2025
4d0fa28
chore: update thriftgo
li-jin-gou Mar 7, 2025
21c87ac
chore: update thriftgo
li-jin-gou Mar 7, 2025
9103286
chore: update thriftgo
li-jin-gou Mar 7, 2025
bc4c307
chore: update thriftgo
li-jin-gou Mar 7, 2025
9f23f41
chore: update thriftgo
li-jin-gou Mar 7, 2025
f221274
chore: update thriftgo
li-jin-gou Mar 7, 2025
60ab452
chore: update thriftgo
li-jin-gou Mar 7, 2025
66a686a
chore: update thriftgo
li-jin-gou Mar 8, 2025
c4a0f8f
feat: support hz sdk
li-jin-gou Mar 8, 2025
08695e9
feat: support hz sdk
li-jin-gou Mar 8, 2025
b3919ec
feat: support hz sdk
li-jin-gou Mar 8, 2025
43f69ef
feat: support hz sdk
li-jin-gou Mar 8, 2025
e2bb31d
feat: support hz sdk
li-jin-gou Mar 8, 2025
7c1234e
feat: support hz sdk
li-jin-gou Mar 9, 2025
b65c830
feat: support hz sdk
li-jin-gou Mar 9, 2025
5831ead
feat: support hz sdk
li-jin-gou Mar 9, 2025
2256067
feat: support hz sdk
li-jin-gou Mar 9, 2025
d552c83
feat: support hz sdk
li-jin-gou Mar 9, 2025
815b45d
feat: support hz sdk
li-jin-gou Mar 10, 2025
04696a6
feat: support hz sdk
li-jin-gou Mar 10, 2025
be257c9
feat: support hz sdk
li-jin-gou Mar 10, 2025
2ed77fe
feat: update wdf
li-jin-gou Mar 10, 2025
cca5a91
feat: update wd
li-jin-gou Mar 10, 2025
d5803ba
feat: update wd
li-jin-gou Mar 10, 2025
46dfb2a
feat: update wd
li-jin-gou Mar 10, 2025
84b6f9e
fix hz abspath
li-jin-gou Jul 10, 2025
ff4b4e0
Rename Protoc plugin constant to 'protoc-gen-rgo'
li-jin-gou Dec 14, 2025
f600609
Replace Hertz plugin with RGO plugin in cmd.go
li-jin-gou Dec 14, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cmd/hz/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ import (
// global args. MUST fork it when use
var globalArgs = config.NewArgument()

func GetGlobalArgs() *config.Argument {
return globalArgs
}

func New(c *cli.Context) error {
args, err := globalArgs.Parse(c, meta.CmdNew)
if err != nil {
Expand Down
16 changes: 13 additions & 3 deletions cmd/hz/config/argument.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ package config

import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/cloudwego/thriftgo/utils/dir_utils"

"github.com/cloudwego/hertz/cmd/hz/meta"
"github.com/cloudwego/hertz/cmd/hz/util"
"github.com/cloudwego/hertz/cmd/hz/util/logs"
Expand Down Expand Up @@ -149,16 +150,25 @@ func (arg *Argument) UpdateByManifest(m *meta.Manifest) {

// checkPath sets the project path and verifies that the model、handler、router and client path is compliant
func (arg *Argument) checkPath() error {
dir, err := os.Getwd()
dir, err := dir_utils.Getwd()
if err != nil {
return fmt.Errorf("get current path failed: %s", err)
}

if !filepath.IsAbs(dir) {
dir, err = filepath.Abs(dir)
if err != nil {
return fmt.Errorf("get abs path failed: %s", err)
}
}

arg.Cwd = dir
if arg.OutDir == "" {
arg.OutDir = dir
}

if !filepath.IsAbs(arg.OutDir) {
ap := filepath.Join(arg.Cwd, arg.OutDir)
ap, _ := filepath.Abs(arg.OutDir)
arg.OutDir = ap
}
if arg.ModelDir != "" && filepath.IsAbs(arg.ModelDir) {
Expand Down
30 changes: 24 additions & 6 deletions cmd/hz/config/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@ func link(src, dst string) error {
return nil
}

// BuildSdkPluginCmd build cmd for hz plugin sdk
func BuildSdkPluginCmd(args *Argument) (*exec.Cmd, error) {
exe, err := os.Executable()
if err != nil {
return nil, fmt.Errorf("failed to detect current executable, err: %v", err)
}

argPacks, err := args.Pack()
if err != nil {
return nil, err
}
kas := strings.Join(argPacks, ",")
return wrapCmd(args, "", exe, kas)
}

// BuildPluginCmd build cmd for hz plugin cmd
func BuildPluginCmd(args *Argument) (*exec.Cmd, error) {
exe, err := os.Executable()
if err != nil {
Expand All @@ -103,10 +119,12 @@ func BuildPluginCmd(args *Argument) (*exec.Cmd, error) {
if err != nil {
return nil, err
}
cmd := &exec.Cmd{
Path: path,
}

return wrapCmd(args, path, exe, kas)
}

func wrapCmd(args *Argument, path, exe, kas string) (*exec.Cmd, error) {
cmd := &exec.Cmd{Path: path}
if args.IdlType == meta.IdlThrift {
// thriftgo
os.Setenv(meta.EnvPluginMode, meta.ThriftPluginName)
Expand Down Expand Up @@ -144,9 +162,9 @@ func BuildPluginCmd(args *Argument) (*exec.Cmd, error) {
cmd.Args = append(cmd.Args, "-I", filepath.Dir(inc))
}
cmd.Args = append(cmd.Args,
"--plugin=protoc-gen-hertz="+exe,
"--hertz_out="+args.OutDir,
"--hertz_opt="+kas,
"--plugin=protoc-gen-rgo="+exe,
"--rgo_out="+args.OutDir,
"--rgo_opt="+kas,
)
for _, p := range args.ProtobufPlugins {
pluginParams := strings.Split(p, ":")
Expand Down
2 changes: 1 addition & 1 deletion cmd/hz/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.16

require (
github.com/Masterminds/sprig/v3 v3.2.3
github.com/cloudwego/thriftgo v0.1.7
github.com/cloudwego/thriftgo v0.3.19
github.com/hashicorp/go-version v1.5.0
github.com/jhump/protoreflect v1.12.0
github.com/urfave/cli/v2 v2.23.0
Expand Down
13 changes: 8 additions & 5 deletions cmd/hz/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ github.com/Masterminds/semver/v3 v3.2.0 h1:3MEsd0SM6jqZojhjLWWeBY+Kcjy9i6MQAeY7Y
github.com/Masterminds/semver/v3 v3.2.0/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
github.com/Masterminds/sprig/v3 v3.2.3 h1:eL2fZNezLomi0uOLqjQoN6BfsDD+fyLtgbJMAj9n6YA=
github.com/Masterminds/sprig/v3 v3.2.3/go.mod h1:rXcFaZ2zZbLRJv/xSysmlgIM1u11eBaRMhvYXJNkGuM=
github.com/apache/thrift v0.13.0 h1:5hryIiq9gtn+MiLVn0wP37kb/uTeRZgN08WoCsAhIhI=
github.com/apache/thrift v0.13.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cloudwego/thriftgo v0.1.7 h1:mTGRv6Dtwfp0hTPZXuIHwm3vtGOuZVTrWarI0xVzUYg=
github.com/cloudwego/thriftgo v0.1.7/go.mod h1:LzeafuLSiHA9JTiWC8TIMIq64iadeObgRUhmVG1OC/w=
github.com/cloudwego/thriftgo v0.3.19 h1:G5W8sLFXjuip8Kwl+bv+am48WFiyCJIydN6AiYlwsoo=
github.com/cloudwego/thriftgo v0.3.19/go.mod h1:AdLEJJVGW/ZJYvkkYAZf5SaJH+pA3OyC801WSwqcBwI=
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI=
github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.9-0.20210217033140-668b12f5399d/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
Expand Down Expand Up @@ -135,8 +135,9 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM=
golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
Expand Down Expand Up @@ -175,13 +176,15 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
2 changes: 1 addition & 1 deletion cmd/hz/meta/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const (

// hz Plugins
const (
ProtocPluginName = "protoc-gen-hertz"
ProtocPluginName = "protoc-gen-rgo"
ThriftPluginName = "thrift-gen-hertz"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/hz/protobuf/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func (plugin *Plugin) fixModelPathAndPackage(pkg string) (impt, path string) {
}
path = util.ImportToPath(impt, "")
// bugfix: impt may have "/" suffix
//impt = plugin.Package + "/" + impt
// impt = plugin.Package + "/" + impt
impt = filepath.Join(plugin.Package, impt)
if util.IsWindows() {
impt = util.PathToImport(impt, "")
Expand Down
196 changes: 196 additions & 0 deletions cmd/hz/sdk/hertz_sdk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
// Copyright 2025 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package sdk

import (
"flag"
"fmt"
"os/exec"
"path/filepath"
"strings"

"github.com/cloudwego/hertz/cmd/hz/meta"
"github.com/cloudwego/hertz/cmd/hz/util"

"github.com/cloudwego/hertz/cmd/hz/app"
"github.com/cloudwego/hertz/cmd/hz/config"
"github.com/cloudwego/hertz/cmd/hz/thrift"
"github.com/cloudwego/thriftgo/plugin"
"github.com/cloudwego/thriftgo/sdk"
"github.com/urfave/cli/v2"
)

// RunHertzTool Run hz SDK
func RunHertzTool(wd, cmdType string, plugins []plugin.SDKPlugin, hertzArgs ...string) error {
hertzPlugin, args, err := GetHertzSDKPlugin(wd, cmdType, hertzArgs)
if err != nil {
return err
}
s := []plugin.SDKPlugin{hertzPlugin}
s = append(s, plugins...)

manifest := new(meta.Manifest)
switch cmdType {
case meta.CmdNew:
exist, err := util.PathExist(filepath.Join(args.OutDir, meta.ManifestFile))
if err != nil {
return err
}

if exist && !args.ForceNew {
return fmt.Errorf("the current is already a hertz project, if you want to regenerate it you can specify \"-force\"")
}

err = app.GenerateLayout(args)
if err != nil {
return err
}
case meta.CmdUpdate:
err = manifest.InitAndValidate(args.OutDir)
if err != nil {
return err
}
// update argument by ".hz", can automatically get "handler_dir"/"model_dir"/"router_dir"
args.UpdateByManifest(manifest)
}

err = sdk.RunThriftgoAsSDK(wd, s, hertzPlugin.GetThriftgoParameters()...)
if err != nil {
if !strings.Contains(err.Error(), meta.TheUseOptionMessage) {
return err
}
}

switch cmdType {
case meta.CmdNew:
args.InitManifest(manifest)
err = manifest.Persist(args.OutDir)
if err != nil {
return fmt.Errorf("persist manifest failed: %v", err)
}
case meta.CmdUpdate:
args.UpdateManifest(manifest)
err = manifest.Persist(args.OutDir)
if err != nil {
return cli.Exit(fmt.Errorf("persist manifest failed: %v", err), meta.PersistError)
}
}

return nil
}

type HertzSDKPlugin struct {
HertzParams []string
ThriftgoParams []string
Pwd string
Args *config.Argument
}

func (k *HertzSDKPlugin) Invoke(req *plugin.Request) (res *plugin.Response) {
r := thrift.Plugin{}
return r.HandleRequest(k.Args, req)
}

func (k *HertzSDKPlugin) GetName() string {
return "hz"
}

func (k *HertzSDKPlugin) GetPluginParameters() []string {
return k.HertzParams
}

func (k *HertzSDKPlugin) GetThriftgoParameters() []string {
return k.ThriftgoParams
}

func findCommon(client *cli.App, cmdType string) *cli.Command {
for _, cmd := range client.Commands {
if cmd.Name == cmdType {
return cmd
}
}
return nil
}

func GetHertzSDKPlugin(pwd, cmdType string, rawHertzArgs []string) (*HertzSDKPlugin, *config.Argument, error) {
client := app.Init()

c := findCommon(client, cmdType)
if c == nil {
return nil, nil, fmt.Errorf("command not found: %s", cmdType)
}

flagSet := flag.NewFlagSet(cmdType+"-parse", flag.ContinueOnError)

for _, f := range c.Flags {
if err := f.Apply(flagSet); err != nil {
return nil, nil, err
}
}

if err := flagSet.Parse(rawHertzArgs); err != nil {
return nil, nil, err
}

ctx := cli.NewContext(client, flagSet, nil)

args, err := app.GetGlobalArgs().Parse(ctx, cmdType)
if err != nil {
return nil, nil, err
}

cmd, err := config.BuildSdkPluginCmd(args)
if err != nil {
return nil, nil, err
}

hertzPlugin := &HertzSDKPlugin{}

hertzPlugin.ThriftgoParams, hertzPlugin.HertzParams, err = ParseHertzCmd(cmd)
hertzPlugin.Args = args

if err != nil {
return nil, nil, err
}
hertzPlugin.Pwd = pwd

return hertzPlugin, args, nil
}

func ParseHertzCmd(cmd *exec.Cmd) (thriftgoParams, hertzParams []string, err error) {
cmdArgs := cmd.Args
// thriftgo -r -o kitex_gen -g go:xxx -p kitex=xxxx -p otherplugin xxx.thrift
// ignore first argument, and remove -p kitex=xxxx

thriftgoParams = []string{}
hertzParams = []string{}
if len(cmdArgs) < 1 {
return nil, nil, fmt.Errorf("cmd args too short: %s", cmdArgs)
}

for i := 1; i < len(cmdArgs); i++ {
arg := cmdArgs[i]
if arg == "-p" && i+1 < len(cmdArgs) {
pluginArgs := cmdArgs[i+1]
if strings.HasPrefix(pluginArgs, "hertz") {
hertzParams = strings.Split(pluginArgs, ",")
i++
continue
}
}
thriftgoParams = append(thriftgoParams, arg)
}
return thriftgoParams, hertzParams, nil
}
Loading
Loading