Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check that all Cobra commands check length slice flags for emptiness if required #2879

Open
cthulhu-rider opened this issue Jun 28, 2024 · 1 comment
Labels
bug Something isn't working neofs-cli NeoFS CLI application issues

Comments

@cthulhu-rider
Copy link
Contributor

cthulhu-rider commented Jun 28, 2024

https://pkg.go.dev/github.com/spf13/cobra is a std engine for NeoFS command line tools. Some commands accept declare flag(s) of slice/array type. They are passed as --flag XXX --flag XXX .... Then Cobra parses them into Go []T where exact T varies, e.g. StringSlice

there is one feature (???) of parsing such flags: a missing optional flag is treated the same as an explicitly specified with empty values:

$ ./cmd
$ ./cmd --flag "" --flag ""
// parsed to nothing
$ ./cmd --flag "" --flag "abc" --flag "" --flag "def"
// parsed to [abc, def]

note that if --flag is marked required, then 1st command will Error: required flag(s) "oid" not set while 2nd is still parsed as an empty slice

this not very obvious behavior can make the execution of some commands quite unexpected. For example, when command does useful actions in for loop

Expected Behavior

when command accepts slice flag and parses each element which must not be an empty string (like container/object/user IDs), passing --flag "" leads to zero-len failure

Current Behavior

if command checks flag len explicitly - fail, otherwise empty element just lost, and command may become even no-op

Possible Solution

always check length of required slice/array flags

Steps to Reproduce (for bugs)

$ neofs-cli object lock --cid F74twfixT89Wqc4qh32Yj5vzP6p9gQ7dxJCzp3C311an --lifetime 100 -r localhost:8080
Error: required flag(s) "oid" not set
$ neofs-cli object lock --cid F74twfixT89Wqc4qh32Yj5vzP6p9gQ7dxJCzp3C311an --oid 'abc' --lifetime 100 -r localhost:8080
Incorrect object arg #1: invalid length 3
$ neofs-cli object lock --cid F74twfixT89Wqc4qh32Yj5vzP6p9gQ7dxJCzp3C311an --oid '' --lifetime 100 -r localhost:8080
Store lock object in NeoFS: finish object stream: status: code = 3072 message = container not found

Context

this may also appear when working with env vars:

$ neofs-cli object lock --cid F74twfixT89Wqc4qh32Yj5vzP6p9gQ7dxJCzp3C311an --oid "$OID" --lifetime 100 -r localhost:8080

if OID will be empty for some reason, this wont be detected

Regression

no

Your Environment

@cthulhu-rider cthulhu-rider added bug Something isn't working neofs-cli NeoFS CLI application issues labels Jun 28, 2024
@cthulhu-rider
Copy link
Contributor Author

the reason is pretty understandable: such flags are CSV. For example, --flag "," is ["", ""]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working neofs-cli NeoFS CLI application issues
Projects
None yet
Development

No branches or pull requests

1 participant