From 4e9980d35900345f9bc380f1cb9b9cbccfc55e8a Mon Sep 17 00:00:00 2001 From: Inhere Date: Sun, 4 Sep 2022 12:37:25 +0800 Subject: [PATCH] chore: prog,example - replace the interface{} to go1.18 any --- _examples/cliapp/main.go | 2 +- _examples/cmd/env_info.go | 2 +- _examples/cmd/git_pull_multi.go | 2 +- _examples/cmd/progress_demo.go | 4 ++-- builtin/gen_auto_complete.go | 6 +++--- builtin/sflag/parser.go | 27 ++++++++++++++------------- builtin/sflag/value_getter.go | 2 +- helper/utils.go | 2 +- progress/progress.go | 11 ++++++----- 9 files changed, 30 insertions(+), 28 deletions(-) diff --git a/_examples/cliapp/main.go b/_examples/cliapp/main.go index 99b4e7b..0e57537 100644 --- a/_examples/cliapp/main.go +++ b/_examples/cliapp/main.go @@ -21,7 +21,7 @@ func main() { app := gcli.NewApp(func(app *gcli.App) { app.Version = "3.0.0" app.Desc = "this is my cli application" - app.On(gcli.EvtAppInit, func(data ...interface{}) bool { + app.On(gcli.EvtAppInit, func(data ...any) bool { // do something... // fmt.Println("init app") return false diff --git a/_examples/cmd/env_info.go b/_examples/cmd/env_info.go index bec0733..b159f21 100644 --- a/_examples/cmd/env_info.go +++ b/_examples/cmd/env_info.go @@ -32,7 +32,7 @@ var EnvInfo = &gcli.Command{ Func: func(c *gcli.Command, _ []string) error { eAble, _ := os.Executable() - data := map[string]interface{}{ + data := map[string]any{ "os": runtime.GOOS, "binName": c.BinName(), "workDir": c.WorkDir(), diff --git a/_examples/cmd/git_pull_multi.go b/_examples/cmd/git_pull_multi.go index dc5b9a3..e74594f 100644 --- a/_examples/cmd/git_pull_multi.go +++ b/_examples/cmd/git_pull_multi.go @@ -26,7 +26,7 @@ var GitPullMulti = &gcli.Command{ true, ). WithValue("./"). - WithValidator(func(v interface{}) (i interface{}, e error) { + WithValidator(func(v any) (i any, e error) { if !fsutil.IsDir(v.(string)) { return nil, fmt.Errorf("the base path must be an exist dir") } diff --git a/_examples/cmd/progress_demo.go b/_examples/cmd/progress_demo.go index c3d6158..6a810c4 100644 --- a/_examples/cmd/progress_demo.go +++ b/_examples/cmd/progress_demo.go @@ -10,7 +10,7 @@ import ( ) var pdOpts = struct { - maxSteps int + maxSteps int overwrite, random bool handlers map[string]func(int) @@ -31,7 +31,7 @@ var ProgressDemo = &gcli.Command{ Desc: "progress bar type name. allow: bar,txt,dtxt,loading,roundTrip", Required: true, - // Validator: func(val interface{}) (interface{}, error) { + // Validator: func(val any) (any, error) { // name := val.(string) // }, }) diff --git a/builtin/gen_auto_complete.go b/builtin/gen_auto_complete.go index 98266e9..ebcceb8 100644 --- a/builtin/gen_auto_complete.go +++ b/builtin/gen_auto_complete.go @@ -93,7 +93,7 @@ func doGen(c *gcli.Command, _ []string) (err error) { } // color.Info.Tips("\n %+v\n", genOpts) - data := map[string]interface{}{ + data := map[string]any{ "Shell": genOpts.shell, "BinName": genOpts.binName, "FileName": genOpts.output, @@ -172,7 +172,7 @@ _complete_for_{{.BinName}} () { complete -F _complete_for_{{.BinName}} {{.BinName}} {{.BinName}}.exe ` -func buildForBashShell(app *gcli.App, data map[string]interface{}) map[string]interface{} { +func buildForBashShell(app *gcli.App, data map[string]any) map[string]any { var cNames []string // {cmd name: opts} @@ -264,7 +264,7 @@ compdef _complete_for_{{.BinName}} {{.BinName}} compdef _complete_for_{{.BinName}} {{.BinName}}.exe ` -func buildForZshShell(app *gcli.App, data map[string]interface{}) map[string]interface{} { +func buildForZshShell(app *gcli.App, data map[string]any) map[string]any { type opInfos []string // {cmd name: cmd des}. in zsh eg: 'build[compile packages and dependencies]' diff --git a/builtin/sflag/parser.go b/builtin/sflag/parser.go index c1d231d..167280e 100644 --- a/builtin/sflag/parser.go +++ b/builtin/sflag/parser.go @@ -29,9 +29,9 @@ type ArgsParser struct { // raw args length length int // parsed longs options. value allow: bool, string, array - longOpts map[string]interface{} + longOpts map[string]any // parsed shorts options. value allow: bool, string, array - shortOpts map[string]interface{} + shortOpts map[string]any // parsed arguments args []string } @@ -58,8 +58,8 @@ func ParseArgs(args []string, boolOpts []string, arrayOpts []string) *ArgsParser } // Opts get parsed opts -func (p *ArgsParser) Opts() map[string]interface{} { - return map[string]interface{}{ +func (p *ArgsParser) Opts() map[string]any { + return map[string]any{ "longs": p.longOpts, "shorts": p.shortOpts, } @@ -84,8 +84,8 @@ func (p *ArgsParser) prepare() { p.arrayOpts = p.flipSlice(p.ArrayOpts) } - p.longOpts = make(map[string]interface{}) - p.shortOpts = make(map[string]interface{}) + p.longOpts = make(map[string]any) + p.shortOpts = make(map[string]any) } /************************************************************* @@ -95,13 +95,14 @@ func (p *ArgsParser) prepare() { // Parse args list to options // // Supports options format: -// -e // bool, short option -// -e // short option -// -e= -// -aux // multi short bool options -// --bool-opt // bool, lang option -// --long-opt // lang option -// --long-opt= +// +// -e // bool, short option +// -e // short option +// -e= +// -aux // multi short bool options +// --bool-opt // bool, lang option +// --long-opt // lang option +// --long-opt= func (p *ArgsParser) Parse(args []string) { p.rawArgs = args p.prepare() diff --git a/builtin/sflag/value_getter.go b/builtin/sflag/value_getter.go index e60525d..62fc168 100644 --- a/builtin/sflag/value_getter.go +++ b/builtin/sflag/value_getter.go @@ -9,7 +9,7 @@ import ( // ValueGetter struct type ValueGetter struct { // value store parsed argument data. (type: string, []string) - Value interface{} + Value any // is array Arrayed bool } diff --git a/helper/utils.go b/helper/utils.go index b9a2bee..b191e9e 100644 --- a/helper/utils.go +++ b/helper/utils.go @@ -24,7 +24,7 @@ var ( ) // RenderText render text template with data -func RenderText(input string, data interface{}, fns template.FuncMap, isFile ...bool) string { +func RenderText(input string, data any, fns template.FuncMap, isFile ...bool) string { t := template.New("cli") t.Funcs(template.FuncMap{ // don't escape content diff --git a/progress/progress.go b/progress/progress.go index 1dd1ce6..11761af 100644 --- a/progress/progress.go +++ b/progress/progress.go @@ -23,12 +23,13 @@ type Progresser interface { Advance(steps ...uint) AdvanceTo(step uint) Finish(msg ...string) - Bound() interface{} + Bound() any } // Progress definition // Refer: -// https://github.com/inhere/php-console/blob/master/src/utils/ProgressBar.php +// +// https://github.com/inhere/php-console/blob/master/src/utils/ProgressBar.php type Progress struct { // Format string the bar format Format string @@ -52,7 +53,7 @@ type Progress struct { // current step value step uint // bound user custom data. - bound interface{} + bound any // mark start status started bool // completed percent. eg: "83.8" @@ -139,13 +140,13 @@ func (p *Progress) WithMaxSteps(maxSteps ...int) *Progress { } // Binding user custom data to instance -func (p *Progress) Binding(data interface{}) *Progress { +func (p *Progress) Binding(data any) *Progress { p.bound = data return p } // Bound get bound sub struct instance -func (p *Progress) Bound() interface{} { +func (p *Progress) Bound() any { return p.bound }