Skip to content

Commit

Permalink
cli: Use MarkFlagsOneRequired for mutex but required flags
Browse files Browse the repository at this point in the history
The feature came with github.com/spf13/[email protected]. We don't need to do
manual checks with it. For example, following command result changed
from:
```
$ neofs-cli bearer create -i 1 -n 2 --out b -o c
expiration epoch or lifetime period is required
```
to
```
Error: at least one of the flags in the group [expire-at lifetime] is required
```

Ret code remains = 1. Below are command checklists.
CLI:
 * `bearer create`;
 * `control shards evacuate`;
 * `control shards flush-cache`;
 * `control shards set-mode`;
 * `object lock`;
 * `session create`;
 * `storagegroup put`.
ADM:
 * `morph verified-nodes-domain set-access-list`;
 * `morph refill-gas`.

Closes #2595.

Signed-off-by: Leonard Lyubich <[email protected]>
  • Loading branch information
cthulhu-rider committed Mar 5, 2024
1 parent 14b650c commit 9c077c1
Show file tree
Hide file tree
Showing 9 changed files with 10 additions and 32 deletions.
7 changes: 2 additions & 5 deletions cmd/neofs-adm/internal/modules/morph/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,6 @@ var (
} else {
// storage wallet path is not part of the config
storageWalletPath, _ := cmd.Flags().GetString(storageWalletFlag)
if storageWalletPath == "" {
return fmt.Errorf("missing wallet path (use '--%s <out.json>')", storageWalletFlag)
}

w, err := wallet.NewWalletFromFile(storageWalletPath)
if err != nil {
Expand Down Expand Up @@ -429,7 +426,7 @@ func init() {
refillGasCmd.Flags().String(storageWalletFlag, "", "Path to storage node wallet")
refillGasCmd.Flags().String(walletAddressFlag, "", "Address of wallet")
refillGasCmd.Flags().String(refillGasAmountFlag, "", "Additional amount of GAS to transfer")
refillGasCmd.MarkFlagsMutuallyExclusive(walletAddressFlag, storageWalletFlag)
refillGasCmd.MarkFlagsOneRequired(walletAddressFlag, storageWalletFlag)

RootCmd.AddCommand(depositNotaryCmd)
depositNotaryCmd.Flags().StringP(endpointFlag, "r", "", "N3 RPC node endpoint")
Expand Down Expand Up @@ -462,7 +459,7 @@ func init() {
_ = cmd.MarkFlagRequired(domainFlag)
fs.StringSlice(neoAddressesFlag, nil, "Neo addresses resolved from public keys of the storage nodes")
fs.StringSlice(publicKeysFlag, nil, "HEX-encoded public keys of the storage nodes")
cmd.MarkFlagsMutuallyExclusive(publicKeysFlag, neoAddressesFlag)
cmd.MarkFlagsOneRequired(publicKeysFlag, neoAddressesFlag)

verifiedNodesDomainCmd.AddCommand(cmd)

Expand Down
6 changes: 0 additions & 6 deletions cmd/neofs-adm/internal/modules/morph/verified_domains.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package morph

import (
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -71,11 +70,6 @@ func verifiedNodesDomainSetAccessList(cmd *cobra.Command, _ []string) error {

strNeoAddresses := vpr.GetStringSlice(neoAddressesFlag)
strPublicKeys := vpr.GetStringSlice(publicKeysFlag)
if len(strNeoAddresses)+len(strPublicKeys) == 0 {
// Track https://github.com/nspcc-dev/neofs-node/issues/2595.
return errors.New("neither Neo addresses nor public keys are set")
}

if len(strNeoAddresses)*len(strPublicKeys) != 0 {
// just to make sure
panic("mutually exclusive flags bypassed Cobra")
Expand Down
6 changes: 1 addition & 5 deletions cmd/neofs-cli/modules/bearer/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package bearer
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"time"
Expand Down Expand Up @@ -56,7 +55,7 @@ func init() {
_ = cobra.MarkFlagRequired(createCmd.Flags(), notValidBeforeFlag)
_ = cobra.MarkFlagRequired(createCmd.Flags(), ownerFlag)
_ = cobra.MarkFlagRequired(createCmd.Flags(), outFlag)
createCmd.MarkFlagsMutuallyExclusive(commonflags.ExpireAt, commonflags.Lifetime)
createCmd.MarkFlagsOneRequired(commonflags.ExpireAt, commonflags.Lifetime)

Check warning on line 58 in cmd/neofs-cli/modules/bearer/create.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-cli/modules/bearer/create.go#L58

Added line #L58 was not covered by tests
}

func createToken(cmd *cobra.Command, _ []string) {
Expand All @@ -66,9 +65,6 @@ func createToken(cmd *cobra.Command, _ []string) {
lifetime, _ := cmd.Flags().GetUint64(commonflags.Lifetime)
exp, expRelative, err := common.ParseEpoch(cmd, commonflags.ExpireAt)
common.ExitOnErr(cmd, "can't parse --"+commonflags.ExpireAt+" flag: %w", err)
if exp == 0 && lifetime == 0 {
common.ExitOnErr(cmd, "", errors.New("expiration epoch or lifetime period is required"))
}

nvb, nvbRelative, err := common.ParseEpoch(cmd, notValidBeforeFlag)
common.ExitOnErr(cmd, "can't parse --"+notValidBeforeFlag+" flag: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/neofs-cli/modules/control/evacuate_shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@ func initControlEvacuateShardCmd() {
flags.Bool(shardAllFlag, false, "Process all shards")
flags.Bool(dumpIgnoreErrorsFlag, false, "Skip invalid/unreadable objects")

evacuateShardCmd.MarkFlagsMutuallyExclusive(shardIDFlag, shardAllFlag)
evacuateShardCmd.MarkFlagsOneRequired(shardIDFlag, shardAllFlag)

Check warning on line 57 in cmd/neofs-cli/modules/control/evacuate_shard.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-cli/modules/control/evacuate_shard.go#L57

Added line #L57 was not covered by tests
}
2 changes: 1 addition & 1 deletion cmd/neofs-cli/modules/control/flush_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ func initControlFlushCacheCmd() {
ff.StringSlice(shardIDFlag, nil, "List of shard IDs in base58 encoding")
ff.Bool(shardAllFlag, false, "Process all shards")

flushCacheCmd.MarkFlagsMutuallyExclusive(shardIDFlag, shardAllFlag)
flushCacheCmd.MarkFlagsOneRequired(shardIDFlag, shardAllFlag)

Check warning on line 53 in cmd/neofs-cli/modules/control/flush_cache.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-cli/modules/control/flush_cache.go#L53

Added line #L53 was not covered by tests
}
6 changes: 2 additions & 4 deletions cmd/neofs-cli/modules/control/shards_set_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func initControlSetShardModeCmd() {
)
flags.Bool(shardClearErrorsFlag, false, "Set shard error count to 0")

setShardModeCmd.MarkFlagsMutuallyExclusive(shardIDFlag, shardAllFlag)
setShardModeCmd.MarkFlagsOneRequired(shardIDFlag, shardAllFlag)

Check warning on line 100 in cmd/neofs-cli/modules/control/shards_set_mode.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-cli/modules/control/shards_set_mode.go#L100

Added line #L100 was not covered by tests
}

func setShardMode(cmd *cobra.Command, _ []string) {
Expand Down Expand Up @@ -155,9 +155,7 @@ func getShardIDList(cmd *cobra.Command) [][]byte {
}

sidList, _ := cmd.Flags().GetStringSlice(shardIDFlag)
if len(sidList) == 0 {
common.ExitOnErr(cmd, "", fmt.Errorf("either --%s or --%s flag must be provided", shardIDFlag, shardAllFlag))
}
// guaranteed by MarkFlagsMutuallyExclusive

// We can sort the ID list and perform this check without additional allocations,
// but preserving the user order is a nice thing to have.
Expand Down
6 changes: 1 addition & 5 deletions cmd/neofs-cli/modules/object/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package object

import (
"context"
"errors"
"fmt"
"strconv"
"time"
Expand Down Expand Up @@ -52,9 +51,6 @@ var objectLockCmd = &cobra.Command{

exp, _ := cmd.Flags().GetUint64(commonflags.ExpireAt)
lifetime, _ := cmd.Flags().GetUint64(commonflags.Lifetime)
if exp == 0 && lifetime == 0 { // mutual exclusion is ensured by cobra
common.ExitOnErr(cmd, "", errors.New("expiration epoch or lifetime period is required"))
}

if lifetime != 0 {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
Expand Down Expand Up @@ -110,5 +106,5 @@ func initCommandObjectLock() {
ff.Uint64P(commonflags.ExpireAt, "e", 0, "The last active epoch for the lock")

ff.Uint64(commonflags.Lifetime, 0, "Lock lifetime")
objectLockCmd.MarkFlagsMutuallyExclusive(commonflags.ExpireAt, commonflags.Lifetime)
objectLockCmd.MarkFlagsOneRequired(commonflags.ExpireAt, commonflags.Lifetime)

Check warning on line 109 in cmd/neofs-cli/modules/object/lock.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-cli/modules/object/lock.go#L109

Added line #L109 was not covered by tests
}
2 changes: 1 addition & 1 deletion cmd/neofs-cli/modules/session/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func init() {
_ = cobra.MarkFlagRequired(createCmd.Flags(), commonflags.WalletPath)
_ = cobra.MarkFlagRequired(createCmd.Flags(), outFlag)
_ = cobra.MarkFlagRequired(createCmd.Flags(), commonflags.RPC)
createCmd.MarkFlagsMutuallyExclusive(commonflags.ExpireAt, commonflags.Lifetime)
createCmd.MarkFlagsOneRequired(commonflags.ExpireAt, commonflags.Lifetime)

Check warning on line 59 in cmd/neofs-cli/modules/session/create.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-cli/modules/session/create.go#L59

Added line #L59 was not covered by tests
}

func createSession(cmd *cobra.Command, _ []string) {
Expand Down
5 changes: 1 addition & 4 deletions cmd/neofs-cli/modules/storagegroup/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,13 @@ func initSGPutCmd() {

flags.Uint64(commonflags.Lifetime, 0, "Storage group lifetime in epochs")
flags.Uint64P(commonflags.ExpireAt, "e", 0, "The last active epoch of the storage group")
sgPutCmd.MarkFlagsMutuallyExclusive(commonflags.ExpireAt, commonflags.Lifetime)
sgPutCmd.MarkFlagsOneRequired(commonflags.ExpireAt, commonflags.Lifetime)

Check warning on line 48 in cmd/neofs-cli/modules/storagegroup/put.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-cli/modules/storagegroup/put.go#L48

Added line #L48 was not covered by tests
}

func putSG(cmd *cobra.Command, _ []string) {
// Track https://github.com/nspcc-dev/neofs-node/issues/2595.
exp, _ := cmd.Flags().GetUint64(commonflags.ExpireAt)
lifetime, _ := cmd.Flags().GetUint64(commonflags.Lifetime)
if exp == 0 && lifetime == 0 { // mutual exclusion is ensured by cobra
common.ExitOnErr(cmd, "", errors.New("expiration epoch or lifetime period is required"))
}
ctx, cancel := commonflags.GetCommandContext(cmd)
defer cancel()

Expand Down

0 comments on commit 9c077c1

Please sign in to comment.