Skip to content

Commit

Permalink
check only one flag is used
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangchiqing committed Jan 13, 2025
1 parent 5e834eb commit ad8e349
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion cmd/util/cmd/read-protocol-state/cmd/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ func run(*cobra.Command, []string) {

reader := NewReader(state, storages)

// making sure only one flag is being used
err = checkOnlyOneFlagIsUsed(flagHeight, flagBlockID, flagFinal, flagSealed, flagExecuted)
if err != nil {
log.Fatal().Err(err).Msg("could not get block")
}

if flagHeight > 0 {
log.Info().Msgf("get block by height: %v", flagHeight)
block, err := reader.GetBlockByHeight(flagHeight)
Expand Down Expand Up @@ -237,5 +243,30 @@ func run(*cobra.Command, []string) {
log.Fatal().Msg("could not find executed block")
}

log.Fatal().Msgf("missing flag, try --final or --sealed or --height or --executed or --block-id")
log.Fatal().Msgf("missing flag, try --final or --sealed or --height or --executed or --block-id, note that only one flag can be used at a time")
}

func checkOnlyOneFlagIsUsed(height uint64, blockID string, final, sealed, executed bool) error {
flags := make([]string, 0, 5)
if height > 0 {
flags = append(flags, "height")
}
if blockID != "" {
flags = append(flags, "blockID")
}
if final {
flags = append(flags, "final")
}
if sealed {
flags = append(flags, "sealed")
}
if executed {
flags = append(flags, "executed")
}

if len(flags) != 1 {
return fmt.Errorf("only one flag can be used at a time, used flags: %v", flags)
}

return nil
}

0 comments on commit ad8e349

Please sign in to comment.