Skip to content

Commit

Permalink
cmd/cue: various minor cleanups
Browse files Browse the repository at this point in the history
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í <mvdan@mvdan.cc>
Change-Id: I5ae60963d1b2c54d75b3e54e2c4549a3a693b206
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1183760
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Paul Jolly <paul@myitcv.io>
TryBot-Result: CUEcueckoo <cueckoo@gmail.com>
  • Loading branch information
mvdan committed Mar 17, 2024
1 parent 93b1a79 commit c248f4e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 21 deletions.
2 changes: 1 addition & 1 deletion cmd/cue/cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions cmd/cue/cmd/get_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(),
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
Expand Down
18 changes: 8 additions & 10 deletions cmd/cue/cmd/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion cmd/cue/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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".
Expand Down

0 comments on commit c248f4e

Please sign in to comment.