From 774e8d1d08382cd4c83bc18665569eb1d81c1868 Mon Sep 17 00:00:00 2001 From: ALMAS Date: Fri, 5 May 2023 21:09:54 +0800 Subject: [PATCH] feat: replaced color package to support Windows (#3207) --- tools/goctl/api/apigen/gen.go | 4 +-- tools/goctl/api/gogen/gen.go | 8 +++--- tools/goctl/api/javagen/gen.go | 4 +-- tools/goctl/api/tsgen/gen.go | 6 ++--- tools/goctl/api/validate/validate.go | 4 +-- tools/goctl/cmd/root.go | 4 +-- tools/goctl/cmd/usage.go | 31 +++++++----------------- tools/goctl/compare/testdata/testdata.go | 8 +++--- tools/goctl/docker/docker.go | 4 +-- tools/goctl/go.mod | 4 ++- tools/goctl/go.sum | 4 +++ tools/goctl/kube/kube.go | 4 +-- tools/goctl/migrate/migrate.go | 9 ++----- tools/goctl/tpl/templates.go | 12 ++++----- tools/goctl/util/console/console.go | 30 +++++------------------ tools/goctl/util/pathx/file.go | 4 +-- 16 files changed, 55 insertions(+), 85 deletions(-) diff --git a/tools/goctl/api/apigen/gen.go b/tools/goctl/api/apigen/gen.go index b0022623..1e77c578 100644 --- a/tools/goctl/api/apigen/gen.go +++ b/tools/goctl/api/apigen/gen.go @@ -8,7 +8,7 @@ import ( "path/filepath" "strings" - "github.com/logrusorgru/aurora" + "github.com/gookit/color" "github.com/spf13/cobra" "github.com/zeromicro/go-zero/tools/goctl/util" "github.com/zeromicro/go-zero/tools/goctl/util/pathx" @@ -73,6 +73,6 @@ func CreateApiTemplate(_ *cobra.Command, _ []string) error { return err } - fmt.Println(aurora.Green("Done.")) + fmt.Println(color.Green.Render("Done.")) return nil } diff --git a/tools/goctl/api/gogen/gen.go b/tools/goctl/api/gogen/gen.go index 74489872..6f568fe5 100644 --- a/tools/goctl/api/gogen/gen.go +++ b/tools/goctl/api/gogen/gen.go @@ -11,7 +11,7 @@ import ( "sync" "time" - "github.com/logrusorgru/aurora" + "github.com/gookit/color" "github.com/spf13/cobra" "github.com/zeromicro/go-zero/core/logx" apiformat "github.com/zeromicro/go-zero/tools/goctl/api/format" @@ -109,7 +109,7 @@ func DoGenProject(apiFile, dir, style string) error { return err } - fmt.Println(aurora.Green("Done.")) + fmt.Println(color.Green.Render("Done.")) return nil } @@ -152,14 +152,14 @@ func sweep() error { seconds, err := strconv.ParseInt(timestamp, 10, 64) if err != nil { // print error and ignore - fmt.Println(aurora.Red(fmt.Sprintf("sweep ignored file: %s", fpath))) + fmt.Println(color.Red.Sprintf("sweep ignored file: %s", fpath)) return nil } tm := time.Unix(seconds, 0) if tm.Before(keepTime) { if err := os.RemoveAll(fpath); err != nil { - fmt.Println(aurora.Red(fmt.Sprintf("failed to remove file: %s", fpath))) + fmt.Println(color.Red.Sprintf("failed to remove file: %s", fpath)) return err } } diff --git a/tools/goctl/api/javagen/gen.go b/tools/goctl/api/javagen/gen.go index 34103b75..bf001e67 100644 --- a/tools/goctl/api/javagen/gen.go +++ b/tools/goctl/api/javagen/gen.go @@ -5,7 +5,7 @@ import ( "fmt" "strings" - "github.com/logrusorgru/aurora" + "github.com/gookit/color" "github.com/spf13/cobra" "github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/tools/goctl/api/parser" @@ -45,6 +45,6 @@ func JavaCommand(_ *cobra.Command, _ []string) error { logx.Must(genPacket(dir, packetName, api)) logx.Must(genComponents(dir, packetName, api)) - fmt.Println(aurora.Green("Done.")) + fmt.Println(color.Green.Render("Done.")) return nil } diff --git a/tools/goctl/api/tsgen/gen.go b/tools/goctl/api/tsgen/gen.go index ddad72ac..34732b67 100644 --- a/tools/goctl/api/tsgen/gen.go +++ b/tools/goctl/api/tsgen/gen.go @@ -4,7 +4,7 @@ import ( "errors" "fmt" - "github.com/logrusorgru/aurora" + "github.com/gookit/color" "github.com/spf13/cobra" "github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/tools/goctl/api/parser" @@ -45,7 +45,7 @@ func TsCommand(_ *cobra.Command, _ []string) error { api, err := parser.Parse(apiFile) if err != nil { - fmt.Println(aurora.Red("Failed")) + fmt.Println(color.Red.Render("Failed")) return err } @@ -59,6 +59,6 @@ func TsCommand(_ *cobra.Command, _ []string) error { logx.Must(genHandler(dir, webAPI, caller, api, unwrapAPI)) logx.Must(genComponents(dir, api)) - fmt.Println(aurora.Green("Done.")) + fmt.Println(color.Green.Render("Done.")) return nil } diff --git a/tools/goctl/api/validate/validate.go b/tools/goctl/api/validate/validate.go index 2660e28e..cb893c80 100644 --- a/tools/goctl/api/validate/validate.go +++ b/tools/goctl/api/validate/validate.go @@ -4,7 +4,7 @@ import ( "errors" "fmt" - "github.com/logrusorgru/aurora" + "github.com/gookit/color" "github.com/spf13/cobra" "github.com/zeromicro/go-zero/tools/goctl/api/parser" ) @@ -27,7 +27,7 @@ func GoValidateApi(_ *cobra.Command, _ []string) error { err = spec.Validate() if err == nil { - fmt.Println(aurora.Green("api format ok")) + fmt.Println(color.Green.Render("api format ok")) } return err } diff --git a/tools/goctl/cmd/root.go b/tools/goctl/cmd/root.go index 870ecd1b..c19b9c7b 100644 --- a/tools/goctl/cmd/root.go +++ b/tools/goctl/cmd/root.go @@ -8,7 +8,7 @@ import ( "strings" "text/template" - "github.com/logrusorgru/aurora" + "github.com/gookit/color" "github.com/spf13/cobra" "github.com/withfig/autocomplete-tools/integrations/cobra" "github.com/zeromicro/go-zero/tools/goctl/api" @@ -44,7 +44,7 @@ var ( func Execute() { os.Args = supportGoStdFlag(os.Args) if err := rootCmd.Execute(); err != nil { - fmt.Println(aurora.Red(err.Error())) + fmt.Println(color.Red.Render(err.Error())) os.Exit(codeFailure) } } diff --git a/tools/goctl/cmd/usage.go b/tools/goctl/cmd/usage.go index 9daa3dcc..ff17bead 100644 --- a/tools/goctl/cmd/usage.go +++ b/tools/goctl/cmd/usage.go @@ -2,53 +2,40 @@ package cmd import ( "fmt" - "runtime" - "github.com/logrusorgru/aurora" - "github.com/zeromicro/go-zero/tools/goctl/vars" + "github.com/gookit/color" ) var colorRender = []func(v any) string{ func(v any) string { - return aurora.BrightRed(v).String() + return color.LightRed.Render(v) }, func(v any) string { - return aurora.BrightGreen(v).String() + return color.LightGreen.Render(v) }, func(v any) string { - return aurora.BrightYellow(v).String() + return color.LightYellow.Render(v) }, func(v any) string { - return aurora.BrightBlue(v).String() + return color.LightBlue.Render(v) }, func(v any) string { - return aurora.BrightMagenta(v).String() + return color.LightMagenta.Render(v) }, func(v any) string { - return aurora.BrightCyan(v).String() + return color.LightCyan.Render(v) }, } func blue(s string) string { - if runtime.GOOS == vars.OsWindows { - return s - } - - return aurora.BrightBlue(s).String() + return color.LightBlue.Render(s) } func green(s string) string { - if runtime.GOOS == vars.OsWindows { - return s - } - - return aurora.BrightGreen(s).String() + return color.LightGreen.Render(s) } func rainbow(s string) string { - if runtime.GOOS == vars.OsWindows { - return s - } s0 := s[0] return colorRender[int(s0)%(len(colorRender)-1)](s) } diff --git a/tools/goctl/compare/testdata/testdata.go b/tools/goctl/compare/testdata/testdata.go index b2092641..efed937c 100644 --- a/tools/goctl/compare/testdata/testdata.go +++ b/tools/goctl/compare/testdata/testdata.go @@ -9,7 +9,7 @@ import ( "path/filepath" "strings" - "github.com/logrusorgru/aurora" + "github.com/gookit/color" "github.com/zeromicro/go-zero/tools/goctl/util/pathx" ) @@ -34,7 +34,7 @@ func (f File) execute(goctl string) error { } printCommand := strings.ReplaceAll(fmt.Sprintf("cd %s && %s", printDir, f.Cmd), "goctl", filepath.Base(goctl)) command := strings.ReplaceAll(fmt.Sprintf("cd %s && %s", dir, f.Cmd), "goctl", goctl) - fmt.Println(aurora.BrightGreen(printCommand)) + fmt.Println(color.LightGreen.Render(printCommand)) cmd := exec.Command("sh", "-c", command) cmd.Env = os.Environ() cmd.Stdout = os.Stdout @@ -106,10 +106,10 @@ func MustRun(baseDir string) { must(err) goctlNew, err := exec.LookPath("goctl") must(err) - fmt.Println(aurora.BrightBlue("========================goctl.old=======================")) + fmt.Println(color.LightBlue.Render("========================goctl.old=======================")) must(oldFiles.execute(goctlOld)) fmt.Println() - fmt.Println(aurora.BrightBlue("========================goctl.new=======================")) + fmt.Println(color.LightBlue.Render("========================goctl.new=======================")) must(newFiles.execute(goctlNew)) } diff --git a/tools/goctl/docker/docker.go b/tools/goctl/docker/docker.go index 15ad115a..3859697a 100644 --- a/tools/goctl/docker/docker.go +++ b/tools/goctl/docker/docker.go @@ -9,7 +9,7 @@ import ( "strings" "text/template" - "github.com/logrusorgru/aurora" + "github.com/gookit/color" "github.com/spf13/cobra" "github.com/zeromicro/go-zero/tools/goctl/util" "github.com/zeromicro/go-zero/tools/goctl/util/env" @@ -42,7 +42,7 @@ type Docker struct { func dockerCommand(_ *cobra.Command, _ []string) (err error) { defer func() { if err == nil { - fmt.Println(aurora.Green("Done.")) + fmt.Println(color.Green.Render("Done.")) } }() diff --git a/tools/goctl/go.mod b/tools/goctl/go.mod index c8829157..62d1da0b 100644 --- a/tools/goctl/go.mod +++ b/tools/goctl/go.mod @@ -7,8 +7,8 @@ require ( github.com/emicklei/proto v1.11.2 github.com/fatih/structtag v1.2.0 github.com/go-sql-driver/mysql v1.7.1 + github.com/gookit/color v1.5.3 github.com/iancoleman/strcase v0.2.0 - github.com/logrusorgru/aurora v2.0.3+incompatible github.com/spf13/cobra v1.7.0 github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.8.2 @@ -56,6 +56,7 @@ require ( github.com/jackc/pgx/v5 v5.3.1 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect + github.com/logrusorgru/aurora v2.0.3+incompatible // indirect github.com/mailru/easyjson v0.7.7 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.17 // indirect @@ -71,6 +72,7 @@ require ( github.com/prometheus/common v0.42.0 // indirect github.com/prometheus/procfs v0.9.0 // indirect github.com/spaolacci/murmur3 v1.1.0 // indirect + github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect github.com/yuin/gopher-lua v1.1.0 // indirect go.etcd.io/etcd/api/v3 v3.5.8 // indirect go.etcd.io/etcd/client/pkg/v3 v3.5.8 // indirect diff --git a/tools/goctl/go.sum b/tools/goctl/go.sum index fa1b679b..7756188f 100644 --- a/tools/goctl/go.sum +++ b/tools/goctl/go.sum @@ -185,6 +185,8 @@ github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= +github.com/gookit/color v1.5.3 h1:twfIhZs4QLCtimkP7MOxlF3A0U/5cDPseRT9M/+2SCE= +github.com/gookit/color v1.5.3/go.mod h1:NUzwzeehUfl7GIb36pqId+UGmRfQcU/WiiyTTeNjHtE= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0/go.mod h1:hgWBS7lorOAVIJEQMi4ZsPv9hVvWI6+ch50m39Pf2Ks= github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.0 h1:1JYBfzqrWPcCclBwxFCPAou9n+q86mfnu7NAeHfte7A= @@ -286,6 +288,8 @@ github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/withfig/autocomplete-tools/integrations/cobra v1.2.1 h1:+dBg5k7nuTE38VVdoroRsT0Z88fmvdYrI2EjzJst35I= github.com/withfig/autocomplete-tools/integrations/cobra v1.2.1/go.mod h1:nmuySobZb4kFgFy6BptpXp/BBw+xFSyvVPP6auoJB4k= +github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8= +github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= diff --git a/tools/goctl/kube/kube.go b/tools/goctl/kube/kube.go index 03ba2028..c3b13263 100644 --- a/tools/goctl/kube/kube.go +++ b/tools/goctl/kube/kube.go @@ -6,7 +6,7 @@ import ( "fmt" "text/template" - "github.com/logrusorgru/aurora" + "github.com/gookit/color" "github.com/spf13/cobra" "github.com/zeromicro/go-zero/tools/goctl/util" "github.com/zeromicro/go-zero/tools/goctl/util/pathx" @@ -111,7 +111,7 @@ func deploymentCommand(_ *cobra.Command, _ []string) error { return err } - fmt.Println(aurora.Green("Done.")) + fmt.Println(color.Green.Render("Done.")) return nil } diff --git a/tools/goctl/migrate/migrate.go b/tools/goctl/migrate/migrate.go index f6c5bcfb..4f4b80a0 100644 --- a/tools/goctl/migrate/migrate.go +++ b/tools/goctl/migrate/migrate.go @@ -11,15 +11,13 @@ import ( "io/ioutil" "os" "path/filepath" - "runtime" "strings" "time" - "github.com/logrusorgru/aurora" + "github.com/gookit/color" "github.com/spf13/cobra" "github.com/zeromicro/go-zero/tools/goctl/util/console" "github.com/zeromicro/go-zero/tools/goctl/util/ctx" - "github.com/zeromicro/go-zero/tools/goctl/vars" ) const defaultMigrateVersion = "v1.3.0" @@ -246,10 +244,7 @@ It's recommended to use the replacement package, do you want to replace? ['Y' for yes, 'N' for no, 'A' for all, 'I' for ignore]: `, deprecated, replacement) - if runtime.GOOS != vars.OsWindows { - msg = aurora.Yellow(msg).String() - } - fmt.Print(msg) + fmt.Print(color.Yellow.Render(msg)) for { var in string diff --git a/tools/goctl/tpl/templates.go b/tools/goctl/tpl/templates.go index 2171f08b..9357eb00 100644 --- a/tools/goctl/tpl/templates.go +++ b/tools/goctl/tpl/templates.go @@ -4,7 +4,7 @@ import ( "fmt" "path/filepath" - "github.com/logrusorgru/aurora" + "github.com/gookit/color" "github.com/spf13/cobra" "github.com/zeromicro/go-zero/core/errorx" "github.com/zeromicro/go-zero/tools/goctl/api/apigen" @@ -70,8 +70,8 @@ func genTemplates(_ *cobra.Command, _ []string) error { return err } - fmt.Printf("Templates are generated in %s, %s\n", aurora.Green(abs), - aurora.Red("edit on your risk!")) + fmt.Printf("Templates are generated in %s, %s\n", color.Green.Render(abs), + color.Red.Render("edit on your risk!")) return nil } @@ -116,7 +116,7 @@ func cleanTemplates(_ *cobra.Command, _ []string) error { return err } - fmt.Printf("%s\n", aurora.Green("templates are cleaned!")) + fmt.Printf("%s\n", color.Green.Render("templates are cleaned!")) return nil } @@ -131,7 +131,7 @@ func updateTemplates(_ *cobra.Command, _ []string) (err error) { defer func() { if err == nil { - fmt.Println(aurora.Green(fmt.Sprintf("%s template are update!", category)).String()) + fmt.Println(color.Green.Sprintf("%s template are update!", category)) } }() switch category { @@ -170,7 +170,7 @@ func revertTemplates(_ *cobra.Command, _ []string) (err error) { defer func() { if err == nil { - fmt.Println(aurora.Green(fmt.Sprintf("%s template are reverted!", filename)).String()) + fmt.Println(color.Green.Sprintf("%s template are reverted!", filename)) } }() switch category { diff --git a/tools/goctl/util/console/console.go b/tools/goctl/util/console/console.go index 8e758583..9647d8f7 100644 --- a/tools/goctl/util/console/console.go +++ b/tools/goctl/util/console/console.go @@ -3,10 +3,8 @@ package console import ( "fmt" "os" - "runtime" - "github.com/logrusorgru/aurora" - "github.com/zeromicro/go-zero/tools/goctl/vars" + "github.com/gookit/color" ) type ( @@ -63,32 +61,28 @@ func (c *colorConsole) Debug(format string, a ...any) { if !c.enable { return } - msg := fmt.Sprintf(format, a...) - println(aurora.BrightCyan(msg)) + println(color.LightCyan.Sprintf(format, a...)) } func (c *colorConsole) Success(format string, a ...any) { if !c.enable { return } - msg := fmt.Sprintf(format, a...) - println(aurora.BrightGreen(msg)) + println(color.LightGreen.Sprintf(format, a...)) } func (c *colorConsole) Warning(format string, a ...any) { if !c.enable { return } - msg := fmt.Sprintf(format, a...) - println(aurora.BrightYellow(msg)) + println(color.LightYellow.Sprintf(format, a...)) } func (c *colorConsole) Error(format string, a ...any) { if !c.enable { return } - msg := fmt.Sprintf(format, a...) - println(aurora.BrightRed(msg)) + println(color.LightRed.Sprintf(format, a...)) } func (c *colorConsole) Fatalln(format string, a ...any) { @@ -126,8 +120,7 @@ func (i *ideaConsole) Info(format string, a ...any) { } func (i *ideaConsole) Debug(format string, a ...any) { - msg := fmt.Sprintf(format, a...) - fmt.Println(aurora.BrightCyan(msg)) + fmt.Println(color.LightCyan.Sprintf(format, a...)) } func (i *ideaConsole) Success(format string, a ...any) { @@ -161,17 +154,6 @@ func (i *ideaConsole) Must(err error) { } func println(msg any) { - value, ok := msg.(aurora.Value) - if !ok { - fmt.Println(msg) - } - - goos := runtime.GOOS - if goos == vars.OsWindows { - fmt.Println(value.Value()) - return - } - fmt.Println(msg) } diff --git a/tools/goctl/util/pathx/file.go b/tools/goctl/util/pathx/file.go index 5b1c62fc..6cbd037d 100644 --- a/tools/goctl/util/pathx/file.go +++ b/tools/goctl/util/pathx/file.go @@ -12,7 +12,7 @@ import ( "path/filepath" "strings" - "github.com/logrusorgru/aurora" + "github.com/gookit/color" "github.com/zeromicro/go-zero/tools/goctl/internal/version" ) @@ -58,7 +58,7 @@ func RemoveOrQuit(filename string) error { } fmt.Printf("%s exists, overwrite it?\nEnter to overwrite or Ctrl-C to cancel...", - aurora.BgRed(aurora.Bold(filename))) + color.New(color.BgRed, color.Bold).Render(filename)) bufio.NewReader(os.Stdin).ReadBytes('\n') return os.Remove(filename)