snivilised / cobrass

🐲 Assistant for cli applications using cobra
https://pkg.go.dev/github.com/snivilised/cobrass
MIT License
1 stars 0 forks source link

panic occurs when defining a flag without default #232

Open plastikfan opened 11 months ago

plastikfan commented 11 months ago

eg:

• [PANICKED] [0.013 seconds]
SamplerRunner sampler [It] 🧪 ===> given: 'profile without no-files in args', should: 'sample(first) with glob filter, using no-files from config'
/Users/plastikfan/dev/github/snivilised/pixa/src/app/proxy/sampler-runner_test.go:157

  [PANICKED] Test Panicked
  In [It] at: /usr/local/Cellar/go/1.21.3/libexec/src/runtime/iface.go:263 @ 12/06/23 14:12:48.45

  interface conversion: interface {} is nil, not uint

  Full Stack Trace
    github.com/snivilised/cobrass/src/assistant.(*ParamSet[...]).BindUint(0x2586f88, 0x22005b8?, 0x50?)
        /Users/plastikfan/go/pkg/mod/github.com/snivilised/cobrass@v0.3.6/src/assistant/param-set-auto.go:725 +0xee
    github.com/snivilised/cobrass/src/assistant.(*ParamSet[...]).BindValidatedUintWithin(0x196db80, 0xc0000aee10, 0xc0003c0b18, 0x1, 0x80)
        /Users/plastikfan/go/pkg/mod/github.com/snivilised/cobrass@v0.3.6/src/assistant/param-set-binder-helpers-auto.go:2720 +0x4b
    github.com/snivilised/pixa/src/app/command.(*Bootstrap).buildRootCommand(0x1821aa0?, 0xc0000c7548)
        /Users/plastikfan/dev/github/snivilised/pixa/src/app/command/root-cmd.go:75 +0x425
    github.com/snivilised/pixa/src/app/command.(*Bootstrap).Root(0xc0003642d0, {0xc000107740, 0x1, 0x0?})
        /Users/plastikfan/dev/github/snivilised/pixa/src/app/command/bootstrap.go:146 +0x57f
    github.com/snivilised/pixa/src/app/proxy_test.glob..func3.3(0xc0001c4620)
        /Users/plastikfan/dev/github/snivilised/pixa/src/app/proxy/sampler-runner_test.go:103 +0x1f9
    reflect.Value.call({0x1714640?, 0xc0000c68d0?, 0x13?}, {0x1821bc0, 0x4}, {0xc0000c7128, 0x1, 0x1?})
        /usr/local/Cellar/go/1.21.3/libexec/src/reflect/value.go:596 +0xce7
    reflect.Value.Call({0x1714640?, 0xc0000c68d0?, 0x1964008?}, {0xc0000c7128?, 0x1011545?, 0x50?})
        /usr/local/Cellar/go/1.21.3/libexec/src/reflect/value.go:380 +0xb9
------------------------------

caused in pixa from the following definition:

    // --no-files (pending: sampling-family)
    //
    paramSet.BindValidatedUintWithin(
        &assistant.FlagInfo{
            Name: "no-files",
            Usage: i18n.LeadsWith(
                "no-files",
                xi18n.Text(i18n.RootCmdNoFilesUsageTemplData{}),
            ),
            // Default:            defFSItems,
            AlternativeFlagSet: rootCommand.PersistentFlags(),
        },
        &paramSet.Native.NoFiles,
        minFSItems,
        maxFSItems,
    )

Should be handled more gracefully.

could be caused by this code:

// BindUint binds uint slice flag with a shorthand if
// 'info.Short' has been set otherwise binds without a short name.
func (params *ParamSet[N]) BindUint(info *FlagInfo, to *uint) *ParamSet[N] {
    flagSet := params.ResolveFlagSet(info)
    if info.Short == "" {
        flagSet.UintVar(to, info.FlagName(), info.Default.(uint), info.Usage)
    } else {
        flagSet.UintVarP(to, info.FlagName(), info.Short, info.Default.(uint), info.Usage)
    }

    return params
}

ie, Default is being used without being checked.