code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/flags/errors.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package flags
    17  
    18  import (
    19  	"fmt"
    20  	"strings"
    21  )
    22  
    23  type FlagError struct {
    24  	message string
    25  }
    26  
    27  func (f FlagError) Error() string {
    28  	return f.message
    29  }
    30  
    31  func MutuallyExclusiveError(n1, n2 string) error {
    32  	return FlagError{
    33  		message: fmt.Sprintf("--%s and --%s flags are mutually exclusive", n1, n2),
    34  	}
    35  }
    36  
    37  func MustBeSpecifiedError(name string) error {
    38  	return FlagError{
    39  		message: fmt.Sprintf("--%s flag must be specified", name),
    40  	}
    41  }
    42  
    43  func RequireLessThanFlagError(less, greater string) error {
    44  	return FlagError{
    45  		message: fmt.Sprintf("--%s flag must be greater than --%s", greater, less),
    46  	}
    47  }
    48  
    49  func ArgMustBeSpecifiedError(name string) error {
    50  	return FlagError{
    51  		message: fmt.Sprintf("%s argument must be specified", name),
    52  	}
    53  }
    54  
    55  func TooManyArgsError(names ...string) error {
    56  	return FlagError{
    57  		message: fmt.Sprintf("too many arguments specified, only expect: %v", strings.Join(names, ", ")),
    58  	}
    59  }
    60  
    61  func OneOfFlagsMustBeSpecifiedError(n1, n2 string) error {
    62  	return FlagError{
    63  		message: fmt.Sprintf("--%s or --%s flags must be specified", n1, n2),
    64  	}
    65  }
    66  
    67  func InvalidFlagFormatError(name string) error {
    68  	return FlagError{
    69  		message: fmt.Sprintf("--%s flag has not a valid format", name),
    70  	}
    71  }
    72  
    73  func UnsupportedFlagValueError(name string, unsupported interface{}, supported []interface{}) error {
    74  	return FlagError{
    75  		message: fmt.Sprintf("--%s flag doesn't support value %s, only %v", name, unsupported, supported),
    76  	}
    77  }
    78  
    79  func OneOfParentsFlagMustBeSpecifiedError(name string, parents ...string) error {
    80  	var resultFmt string
    81  	if len(parents) > 1 {
    82  		fmtFlags := make([]string, len(parents))
    83  		for i, pf := range parents {
    84  			fmtFlags[i] = fmt.Sprintf("--%s", pf)
    85  		}
    86  		flagsFmt := strings.Join([]string{
    87  			strings.Join(parents[0:len(fmtFlags)-1], ", "),
    88  			parents[len(fmtFlags)-1],
    89  		}, " or ")
    90  		resultFmt = fmt.Sprintf("%s flags", flagsFmt)
    91  	} else {
    92  		resultFmt = fmt.Sprintf("--%s flag", parents[0])
    93  	}
    94  
    95  	return FlagError{
    96  		message: fmt.Sprintf("--%s flag requires %s to be set", name, resultFmt),
    97  	}
    98  }
    99  
   100  func MustBase64EncodedError(name string) error {
   101  	return FlagError{
   102  		message: fmt.Sprintf("--%s flag value must be base64-encoded", name),
   103  	}
   104  }