Skip to content

Commit 5c623fa

Browse files
committed
Merge branch 'develop' into refactor/kitex_tool
2 parents 1427831 + c8e8365 commit 5c623fa

File tree

11 files changed

+118
-69
lines changed

11 files changed

+118
-69
lines changed

tool/cmd/kitex/args/args.go

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@ type ExtraFlag struct {
5454
// Arguments .
5555
type Arguments struct {
5656
generator.Config
57-
extends []*ExtraFlag
58-
queryVersion bool
59-
dependencyChecker *versions.DependencyChecker
57+
extends []*ExtraFlag
58+
queryVersion bool
6059
}
6160

6261
const (
@@ -98,7 +97,6 @@ func (a *Arguments) buildFlags(version string) *flag.FlagSet {
9897
"Turn on verbose mode.")
9998
f.BoolVar(&a.GenerateInvoker, "invoker", false,
10099
"Generate invoker side codes when service name is specified.")
101-
f.StringVar(&a.IDLType, "type", "unknown", "Specify the type of IDL: 'thrift' or 'protobuf'.")
102100
f.Var(&a.Includes, "I", "Add an IDL search path for includes.")
103101
f.Var(&a.ThriftOptions, "thrift", "Specify arguments for the thrift go compiler.")
104102
f.Var(&a.Hessian2Options, "hessian2", "Specify arguments for the hessian2 codec.")
@@ -196,10 +194,20 @@ func (a *Arguments) ParseArgs(version, curpath string, kitexArgs []string) (err
196194
a.ThriftOptions = append(a.ThriftOptions, "streamx")
197195
}
198196
if a.Record {
199-
a.RecordCmd = os.Args
197+
a.RecordCmd = append([]string{"kitex"}, kitexArgs...)
200198
}
201199
log.Verbose = a.Verbose
202200

201+
for i, inc := range a.Includes {
202+
if util.IsGitURL(inc) {
203+
localGitPath, gitErr := util.RunGitCommand(inc)
204+
if gitErr != nil {
205+
return fmt.Errorf("failed to pull IDL from git: %s, errMsg: %s\nYou can execute 'rm -rf ~/.kitex' to clean the git cache and try again", inc, gitErr.Error())
206+
}
207+
a.Includes[i] = localGitPath
208+
}
209+
}
210+
203211
// format -thrift xxx,xxx to -thrift xx -thrift xx
204212
thriftOptions := make([]string, len(a.ThriftOptions))
205213
for i := range a.ThriftOptions {
@@ -255,16 +263,11 @@ func (a *Arguments) checkIDL(files []string) error {
255263
}
256264
a.IDL = files[0]
257265

258-
switch a.IDLType {
259-
case Thrift, Protobuf:
260-
case Unknown:
261-
if typ, ok := guessIDLType(a.IDL); ok {
262-
a.IDLType = typ
263-
} else {
264-
return fmt.Errorf("can not guess an IDL type from %q (unknown suffix), please specify with the '-type' flag", a.IDL)
265-
}
266-
default:
267-
return fmt.Errorf("unsupported IDL type: %s", a.IDLType)
266+
if typ, ok := guessIDLType(a.IDL); ok {
267+
a.IDLType = typ
268+
} else {
269+
return fmt.Errorf("the last parameter shoule be IDL filename (xxx.thrift or xxx.proto), for example: " +
270+
"\"kitex -service demo idl.thrift\"")
268271
}
269272
return nil
270273
}
@@ -369,19 +372,6 @@ func (a *Arguments) BuildCmd(out io.Writer) (*exec.Cmd, error) {
369372
return nil, fmt.Errorf("failed to detect current executable: %s", err.Error())
370373
}
371374

372-
for i, inc := range a.Includes {
373-
if strings.HasPrefix(inc, "git@") || strings.HasPrefix(inc, "http://") || strings.HasPrefix(inc, "https://") {
374-
localGitPath, errMsg, gitErr := util.RunGitCommand(inc)
375-
if gitErr != nil {
376-
if errMsg == "" {
377-
errMsg = gitErr.Error()
378-
}
379-
return nil, fmt.Errorf("failed to pull IDL from git:%s\nYou can execute 'rm -rf ~/.kitex' to clean the git cache and try again", errMsg)
380-
}
381-
a.Includes[i] = localGitPath
382-
}
383-
}
384-
385375
configkv := a.Config.Pack()
386376
kas := strings.Join(configkv, ",")
387377
cmd := &exec.Cmd{

tool/cmd/kitex/generator/generator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func RunKitexThriftgoGen(wd string, plugins []plugin.SDKPlugin, kitexOsArgs ...s
3030
return t.RunKitexThriftgoGenFromArgs(wd, plugins, &args)
3131
}
3232

33-
func RunKitexToolCmd(curpath string, osArgs []string, args *kargs.Arguments, toolVersion string) (err error, retCode code.ToolExitCode) {
33+
func RunKitexTool(curpath string, osArgs []string, args *kargs.Arguments, toolVersion string) (err error, retCode code.ToolExitCode) {
3434
// try run as thriftgo/protoc plugin process
3535
mode := os.Getenv(kargs.EnvPluginMode)
3636
if len(osArgs) <= 1 && mode != "" {

tool/cmd/kitex/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func main() {
4242
// 2. parse user input to arguments
4343
// 3. check dependency version ( details: versions/dependencies_v2.go)
4444
// 4. execute code generation by choosing specific tool (thriftgo、prutal、protoc?)
45-
err, retCode := generator.RunKitexToolCmd(curpath, os.Args, &args, kitex.Version)
45+
err, retCode := generator.RunKitexTool(curpath, os.Args, &args, kitex.Version)
4646
if err != nil || !code.Good(retCode) {
4747
log.Errorf(err.Error())
4848
os.Exit(int(retCode))

tool/cmd/kitex/utils/utils.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,13 @@
1515
package utils
1616

1717
import (
18-
"os/exec"
19-
"strings"
20-
2118
kargs "github.com/cloudwego/kitex/tool/cmd/kitex/args"
2219
"github.com/cloudwego/kitex/tool/internal_pkg/log"
2320
"github.com/cloudwego/kitex/tool/internal_pkg/pluginmode/thriftgo"
2421
)
2522

2623
func OnKitexToolNormalExit(args kargs.Arguments) {
2724
log.Info("Code Generation is Done!")
28-
29-
if args.IDLType == "thrift" {
30-
cmd := "go mod edit -replace github.com/apache/thrift=github.com/apache/thrift@v0.13.0"
31-
argv := strings.Split(cmd, " ")
32-
err := exec.Command(argv[0], argv[1:]...).Run()
33-
if err != nil {
34-
log.Warnf("Tips: please execute the following command to fix apache thrift lib version.\n%s", cmd)
35-
}
36-
}
37-
3825
// If hessian option is java_extension, replace *java.Object to java.Object
3926
if thriftgo.EnableJavaExtension(args.Config) {
4027
if err := thriftgo.Hessian2PatchByReplace(args.Config, ""); err != nil {

tool/cmd/kitex/versions/dependencies_v2.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func CheckVersion() (ok bool, err error) {
8585
if !currentVersion.greatOrEqual(minDepRepoVersion) {
8686
prompt := cr.promptFunc(cr.minDepRepoPath, currentVersionStr, kitex.Version)
8787
log.Infof(prompt)
88-
return false, fmt.Errorf("kitex cmd tool dependency compatibility check failed")
88+
return false, ErrDependencyVersionNotCompatible
8989
}
9090

9191
return true, nil

tool/internal_pkg/log/log.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"fmt"
1919
"log"
2020
"os"
21+
22+
"github.com/cloudwego/kitex/tool/internal_pkg/util"
2123
)
2224

2325
// Verbose decides whether the informatc logs should be output.
@@ -48,24 +50,28 @@ func SetDefaultLogger(l Logger) {
4850
defaultLogger = l
4951
}
5052

53+
var errorPrefix = util.Bold + util.FgRed + "[ERROR] " + util.Reset
54+
55+
var warningPrefix = util.Bold + util.FgYellow + "[WARN] " + util.Reset
56+
5157
// Error ...
5258
func Error(v ...interface{}) {
53-
defaultLogger.Printf("[ERROR] %s", fmt.Sprintln(v...))
59+
defaultLogger.Printf("%s%s", errorPrefix, fmt.Sprintln(v...))
5460
}
5561

5662
// Errorf ...
5763
func Errorf(format string, v ...interface{}) {
58-
defaultLogger.Printf("[ERROR] "+format, v...)
64+
defaultLogger.Printf(errorPrefix+format, v...)
5965
}
6066

6167
// Warn ...
6268
func Warn(v ...interface{}) {
63-
defaultLogger.Printf("[WARN] %s", fmt.Sprintln(v...))
69+
defaultLogger.Printf("%s %s", warningPrefix, fmt.Sprintln(v...))
6470
}
6571

6672
// Warnf ...
6773
func Warnf(format string, v ...interface{}) {
68-
defaultLogger.Printf("[WARN] "+format, v...)
74+
defaultLogger.Printf(warningPrefix+format, v...)
6975
}
7076

7177
// Info ...

tool/internal_pkg/pluginmode/thriftgo/convertor.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ func (c *converter) init(req *plugin.Request) error {
6666
}
6767

6868
func (c *converter) initLogs() backend.LogFunc {
69+
// todo fix log
70+
// todo 单步验证 thriftgo log
6971
lf := backend.LogFunc{
7072
Info: func(v ...interface{}) {},
7173
Warn: func(v ...interface{}) {
@@ -75,13 +77,13 @@ func (c *converter) initLogs() backend.LogFunc {
7577
c.Warnings = append(c.Warnings, warns...)
7678
},
7779
}
78-
if c.Config.Verbose {
79-
lf.Info = lf.Warn
80-
}
80+
//if c.Config.Verbose {
81+
// lf.Info = lf.Warn
82+
//}
8183

82-
log.SetDefaultLogger(log.LoggerFunc(func(format string, a ...interface{}) {
83-
c.Warnings = append(c.Warnings, fmt.Sprintf(format, a...))
84-
}))
84+
//log.SetDefaultLogger(log.LoggerFunc(func(format string, a ...interface{}) {
85+
// c.Warnings = append(c.Warnings, fmt.Sprintf(format, a...))
86+
//}))
8587
return lf
8688
}
8789

tool/internal_pkg/pluginmode/thriftgo/plugin.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
"fmt"
2020
"os"
2121

22+
"github.com/cloudwego/kitex/tool/internal_pkg/log"
23+
2224
"github.com/cloudwego/thriftgo/plugin"
2325

2426
"github.com/cloudwego/kitex/tool/internal_pkg/generator"
@@ -129,8 +131,7 @@ func HandleRequest(req *plugin.Request) *plugin.Response {
129131
if conv.Config.Use != "" {
130132
err := conv.persist(res)
131133
if err == nil {
132-
// todo 检测这里的效果?修改打印!
133-
fmt.Println("kitex_gen is not generated due to the -use option")
134+
log.Info("kitex_gen is not generated due to the -use option")
134135
return res
135136
}
136137
return conv.failResp(err)

tool/internal_pkg/thriftgo/thriftgo.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ package thriftgo
1616

1717
import (
1818
"bytes"
19+
"fmt"
1920

2021
"github.com/cloudwego/kitex"
2122
"github.com/cloudwego/kitex/tool/internal_pkg/pluginmode/thriftgo"
2223

2324
kargs "github.com/cloudwego/kitex/tool/cmd/kitex/args"
2425
"github.com/cloudwego/kitex/tool/cmd/kitex/code"
25-
"github.com/cloudwego/kitex/tool/internal_pkg/log"
2626
"github.com/cloudwego/thriftgo/plugin"
2727
"github.com/cloudwego/thriftgo/sdk"
2828
)
@@ -51,6 +51,9 @@ func (k *KitexThriftgoPlugin) GetThriftgoParameters() []string {
5151

5252
// RunKitexThriftgoGenFromArgs kitex tool to generate thrift as sdk
5353
func RunKitexThriftgoGenFromArgs(wd string, plugins []plugin.SDKPlugin, argsParsed *kargs.Arguments) (error, code.ToolExitCode) {
54+
if !argsParsed.IsThrift() {
55+
return fmt.Errorf("only support thrift idl"), code.ToolArgsError
56+
}
5457
p, err := GetKitexThriftgoPluginFromArgs(wd, argsParsed)
5558
if err != nil {
5659
if code.IsInterrupted(err) {
@@ -64,10 +67,6 @@ func RunKitexThriftgoGenFromArgs(wd string, plugins []plugin.SDKPlugin, argsPars
6467

6568
plugins = append([]plugin.SDKPlugin{p}, plugins...)
6669

67-
// fixme
68-
l := log.DefaultLogger() // pluginmode/thriftgo/convertor.go will change the logger
69-
defer log.SetDefaultLogger(l) // revert it back
70-
7170
err = sdk.RunThriftgoAsSDK(wd, plugins, p.GetThriftgoParameters()...)
7271
if err == nil {
7372
return nil, code.ToolSuccess

tool/internal_pkg/util/color.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package util
2+
3+
// Define common ANSI escape code constants
4+
const (
5+
Reset = "\x1b[0m" // Reset all attributes
6+
// Regular Colors (Foreground)
7+
FgBlack = "\x1b[30m"
8+
FgRed = "\x1b[31m"
9+
FgGreen = "\x1b[32m"
10+
FgYellow = "\x1b[33m"
11+
FgBlue = "\x1b[34m"
12+
FgMagenta = "\x1b[35m"
13+
FgCyan = "\x1b[36m"
14+
FgWhite = "\x1b[37m"
15+
// Bright/High-Intensity Colors (Foreground)
16+
FgBrightBlack = "\x1b[90m" // Often called Gray
17+
FgBrightRed = "\x1b[91m"
18+
FgBrightGreen = "\x1b[92m"
19+
FgBrightYellow = "\x1b[93m"
20+
FgBrightBlue = "\x1b[94m"
21+
FgBrightMagenta = "\x1b[95m"
22+
FgBrightCyan = "\x1b[96m"
23+
FgBrightWhite = "\x1b[97m"
24+
// Background Colors
25+
BgBlack = "\x1b[40m"
26+
BgRed = "\x1b[41m"
27+
BgGreen = "\x1b[42m"
28+
BgYellow = "\x1b[43m"
29+
BgBlue = "\x1b[44m"
30+
BgMagenta = "\x1b[45m"
31+
BgCyan = "\x1b[46m"
32+
BgWhite = "\x1b[47m"
33+
// Bright/High-Intensity Background Colors
34+
BgBrightBlack = "\x1b[100m"
35+
BgBrightRed = "\x1b[101m"
36+
BgBrightGreen = "\x1b[102m"
37+
BgBrightYellow = "\x1b[103m"
38+
BgBrightBlue = "\x1b[104m"
39+
BgBrightMagenta = "\x1b[105m"
40+
BgBrightCyan = "\x1b[106m"
41+
BgBrightWhite = "\x1b[107m"
42+
// Styles
43+
Bold = "\x1b[1m"
44+
Dim = "\x1b[2m" // Dim/Faint
45+
Italic = "\x1b[3m" // Italic (not supported by all terminals)
46+
Underline = "\x1b[4m" // Underline
47+
Blink = "\x1b[5m" // Blink (not supported by all terminals)
48+
Reverse = "\x1b[7m" // Reverse foreground and background colors
49+
Hidden = "\x1b[8m" // Hidden (useful for password input)
50+
)

0 commit comments

Comments
 (0)