From c248f4e401a76eaaf3b535776705f892d59d4593 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Sat, 16 Mar 2024 13:07:52 +0000 Subject: [PATCH] cmd/cue: various minor cleanups MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit cobra.ExactValidArgs is deprecated with a straightforward replacement. typeSignature doesn't need a name, as Go func signatures do not use one. Use strings.ReplaceAll and strings.Cut. newHelpTopics doesn't need to be a function. Signed-off-by: Daniel Martí Change-Id: I5ae60963d1b2c54d75b3e54e2c4549a3a693b206 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1183760 Unity-Result: CUE porcuepine Reviewed-by: Paul Jolly TryBot-Result: CUEcueckoo --- cmd/cue/cmd/completion.go | 2 +- cmd/cue/cmd/get_go.go | 18 +++++++++--------- cmd/cue/cmd/help.go | 18 ++++++++---------- cmd/cue/cmd/root.go | 2 +- 4 files changed, 19 insertions(+), 21 deletions(-) diff --git a/cmd/cue/cmd/completion.go b/cmd/cue/cmd/completion.go index 3deca4648..ff7868fb3 100644 --- a/cmd/cue/cmd/completion.go +++ b/cmd/cue/cmd/completion.go @@ -55,7 +55,7 @@ func newCompletionCmd(c *Command) *cobra.Command { Long: ``, Example: completionExample, ValidArgs: validCompletionArgs, - Args: cobra.ExactValidArgs(1), + Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), RunE: mkRunE(c, runCompletion), } return cmd diff --git a/cmd/cue/cmd/get_go.go b/cmd/cue/cmd/get_go.go index 90b030d48..ebfc3dac0 100644 --- a/cmd/cue/cmd/get_go.go +++ b/cmd/cue/cmd/get_go.go @@ -289,10 +289,10 @@ var ( // They are not used by go/types.Implements. func typeMethod(name string, params, results []types.Type) *types.Func { - return types.NewFunc(token.NoPos, nil, name, typeSignature(name, params, results)) + return types.NewFunc(token.NoPos, nil, name, typeSignature(params, results)) } -func typeSignature(name string, params, results []types.Type) *types.Signature { +func typeSignature(params, results []types.Type) *types.Signature { paramVars := make([]*types.Var, len(params)) for i, param := range params { paramVars[i] = types.NewParam(token.NoPos, nil, "", param) @@ -332,7 +332,7 @@ var toTop = []*types.Interface{ // yaml.Unmarshaler: interface { UnmarshalYAML(func(interface{}) error) error } types.NewInterfaceType([]*types.Func{ typeMethod("UnmarshalYAML", []types.Type{ - typeSignature("", []types.Type{typeAny}, []types.Type{typeError}), + typeSignature([]types.Type{typeAny}, []types.Type{typeError}), }, []types.Type{typeError}), }, nil).Complete(), } @@ -1312,12 +1312,12 @@ func (e *extractor) addFields(x *types.Struct, st *cueast.StructLit) { typeName := f.Type().String() // simplify type names: for path, info := range e.pkgNames { - typeName = strings.Replace(typeName, path+".", info.name+".", -1) + typeName = strings.ReplaceAll(typeName, path+".", info.name+".") } - typeName = strings.Replace(typeName, e.pkg.Types.Path()+".", "", -1) + typeName = strings.ReplaceAll(typeName, e.pkg.Types.Path()+".", "") - cueStr := strings.Replace(cueType, "_#", "", -1) - cueStr = strings.Replace(cueStr, "#", "", -1) + cueStr := strings.ReplaceAll(cueType, "_#", "") + cueStr = strings.ReplaceAll(cueStr, "#", "") // TODO: remove fields in @go attr that are the same as printed? if name != f.Name() || typeName != cueStr { @@ -1362,8 +1362,8 @@ func (e *extractor) addFields(x *types.Struct, st *cueast.StructLit) { tk := tags.Get("protobuf_key") tv := tags.Get("protobuf_val") if tk != "" && tv != "" { - tk = strings.SplitN(tk, ",", 2)[0] - tv = strings.SplitN(tv, ",", 2)[0] + tk, _, _ = strings.Cut(tk, ",") + tv, _, _ = strings.Cut(tv, ",") split[1] = fmt.Sprintf("map[%s]%s", tk, tv) } } diff --git a/cmd/cue/cmd/help.go b/cmd/cue/cmd/help.go index e767454df..1e22e6f71 100644 --- a/cmd/cue/cmd/help.go +++ b/cmd/cue/cmd/help.go @@ -76,16 +76,14 @@ Simply type ` + c.Name() + ` help [path to command] for full details.`, // is taken and works as well as `cue help topic`, which is unnecessary. // Consider removing support for the short form at some point. -func newHelpTopics(c *Command) []*cobra.Command { - return []*cobra.Command{ - inputsHelp, - environmentHelp, - flagsHelp, - filetypeHelp, - injectHelp, - commandsHelp, - registryConfigHelp, - } +var helpTopics = []*cobra.Command{ + inputsHelp, + environmentHelp, + flagsHelp, + filetypeHelp, + injectHelp, + commandsHelp, + registryConfigHelp, } var inputsHelp = &cobra.Command{ diff --git a/cmd/cue/cmd/root.go b/cmd/cue/cmd/root.go index 83487e18b..08d8b16f1 100644 --- a/cmd/cue/cmd/root.go +++ b/cmd/cue/cmd/root.go @@ -198,7 +198,7 @@ For more information on writing CUE configuration files see cuelang.org.`, newAddCmd(c), newLoginCmd(c), } - subCommands = append(subCommands, newHelpTopics(c)...) + subCommands = append(subCommands, helpTopics...) addGlobalFlags(cmd.PersistentFlags()) // We add the injection flags to the root command for the sake of the short form "cue -t foo=bar mycmd".