-
Notifications
You must be signed in to change notification settings - Fork 625
【WIP】feat: support hz sdk #1291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
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 44d3a9d
chore: update thriftgo
li-jin-gou f7aeea6
chore: update thriftgo
li-jin-gou 4d0fa28
chore: update thriftgo
li-jin-gou 21c87ac
chore: update thriftgo
li-jin-gou 9103286
chore: update thriftgo
li-jin-gou bc4c307
chore: update thriftgo
li-jin-gou 9f23f41
chore: update thriftgo
li-jin-gou f221274
chore: update thriftgo
li-jin-gou 60ab452
chore: update thriftgo
li-jin-gou 66a686a
chore: update thriftgo
li-jin-gou c4a0f8f
feat: support hz sdk
li-jin-gou 08695e9
feat: support hz sdk
li-jin-gou b3919ec
feat: support hz sdk
li-jin-gou 43f69ef
feat: support hz sdk
li-jin-gou e2bb31d
feat: support hz sdk
li-jin-gou 7c1234e
feat: support hz sdk
li-jin-gou b65c830
feat: support hz sdk
li-jin-gou 5831ead
feat: support hz sdk
li-jin-gou 2256067
feat: support hz sdk
li-jin-gou d552c83
feat: support hz sdk
li-jin-gou 815b45d
feat: support hz sdk
li-jin-gou 04696a6
feat: support hz sdk
li-jin-gou be257c9
feat: support hz sdk
li-jin-gou 2ed77fe
feat: update wdf
li-jin-gou cca5a91
feat: update wd
li-jin-gou d5803ba
feat: update wd
li-jin-gou 46dfb2a
feat: update wd
li-jin-gou 84b6f9e
fix hz abspath
li-jin-gou ff4b4e0
Rename Protoc plugin constant to 'protoc-gen-rgo'
li-jin-gou f600609
Replace Hertz plugin with RGO plugin in cmd.go
li-jin-gou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
li-jin-gou marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,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 | ||
li-jin-gou marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import ( | ||
| "flag" | ||
| "fmt" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/cloudwego/hertz/cmd/hz/meta" | ||
| "github.com/cloudwego/hertz/cmd/hz/util" | ||
|
|
||
li-jin-gou marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "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 | ||
li-jin-gou marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // 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 | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.