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

cli: Use MarkFlagsOneRequired for mutex but required flags #2763

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"time"
Expand Down Expand Up @@ -56,7 +55,7 @@
_ = 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 @@
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 @@
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 @@
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
}
7 changes: 3 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 @@
)
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,8 @@
}

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

// 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 @@

import (
"context"
"errors"
"fmt"
"strconv"
"time"
Expand Down Expand Up @@ -52,9 +51,6 @@

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 @@
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 @@
_ = 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 @@

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
Loading