| 1 |
package errs |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"os" |
| 6 |
"strings" |
| 7 |
|
| 8 |
"github.com/pkg/errors" |
| 9 |
"github.com/urfave/cli" |
| 10 |
) |
| 11 |
|
| 12 |
|
| 13 |
func NewError(format string, args ...interface{}) error { |
| 14 |
return errors.Errorf(format, args...) |
| 15 |
} |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
func NewExitError(err error, exitCode int) error { |
| 20 |
return cli.NewExitError(err, exitCode) |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
func Wrap(err error, format string, args ...interface{}) error { |
| 28 |
if err == nil { |
| 29 |
return nil |
| 30 |
} |
| 31 |
cause := errors.Cause(err) |
| 32 |
if cause == err { |
| 33 |
str := err.Error() |
| 34 |
if i := strings.LastIndexByte(str, ':'); i >= 0 { |
| 35 |
str = strings.TrimSpace(str[i:]) |
| 36 |
return errors.Wrapf(fmt.Errorf(str), format, args...) |
| 37 |
} |
| 38 |
} |
| 39 |
return errors.Wrapf(cause, format, args...) |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
func InsecureCommand(ctx *cli.Context) error { |
| 45 |
return errors.Errorf("'%s %s' requires the '--insecure' flag", ctx.App.Name, ctx.Command.Name) |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
func EqualArguments(ctx *cli.Context, arg1, arg2 string) error { |
| 51 |
return errors.Errorf("positional arguments <%s> and <%s> cannot be equal in '%s'", arg1, arg2, usage(ctx)) |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
func MissingArguments(ctx *cli.Context, argNames ...string) error { |
| 57 |
switch len(argNames) { |
| 58 |
case 0: |
| 59 |
return errors.Errorf("missing positional arguments in '%s'", usage(ctx)) |
| 60 |
case 1: |
| 61 |
return errors.Errorf("missing positional argument <%s> in '%s'", argNames[0], usage(ctx)) |
| 62 |
default: |
| 63 |
args := make([]string, len(argNames)) |
| 64 |
for i, name := range argNames { |
| 65 |
args[i] = "<" + name + ">" |
| 66 |
} |
| 67 |
return errors.Errorf("missing positional argument %s in '%s'", strings.Join(args, " "), usage(ctx)) |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
func NumberOfArguments(ctx *cli.Context, required int) error { |
| 75 |
n := ctx.NArg() |
| 76 |
switch { |
| 77 |
case n < required: |
| 78 |
return TooFewArguments(ctx) |
| 79 |
case n > required: |
| 80 |
return TooManyArguments(ctx) |
| 81 |
default: |
| 82 |
return nil |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
func MinMaxNumberOfArguments(ctx *cli.Context, min, max int) error { |
| 90 |
n := ctx.NArg() |
| 91 |
switch { |
| 92 |
case n < min: |
| 93 |
return TooFewArguments(ctx) |
| 94 |
case n > max: |
| 95 |
return TooManyArguments(ctx) |
| 96 |
default: |
| 97 |
return nil |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
func TooFewArguments(ctx *cli.Context) error { |
| 103 |
return errors.Errorf("not enough positional arguments were provided in '%s'", usage(ctx)) |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
func TooManyArguments(ctx *cli.Context) error { |
| 109 |
return errors.Errorf("too many positional arguments were provided in '%s'", usage(ctx)) |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
func InsecureArgument(ctx *cli.Context, name string) error { |
| 115 |
return errors.Errorf("positional argument <%s> requires the '--insecure' flag", name) |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
func FlagValueInsecure(ctx *cli.Context, flag string, value string) error { |
| 121 |
return errors.Errorf("flag '--%s %s' requires the '--insecure' flag", flag, value) |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
func InvalidFlagValue(ctx *cli.Context, flag string, value string, options string) error { |
| 128 |
var format string |
| 129 |
if len(value) == 0 { |
| 130 |
format = fmt.Sprintf("missing value for flag '--%s'", flag) |
| 131 |
} else { |
| 132 |
format = fmt.Sprintf("invalid value '%s' for flag '--%s'", value, flag) |
| 133 |
} |
| 134 |
|
| 135 |
if len(options) == 0 { |
| 136 |
return errors.New(format) |
| 137 |
} |
| 138 |
|
| 139 |
return errors.New(format + "; options are " + options) |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
func InvalidFlagValueMsg(ctx *cli.Context, flag string, value string, msg string) error { |
| 146 |
var format string |
| 147 |
if len(value) == 0 { |
| 148 |
format = fmt.Sprintf("missing value for flag '--%s'", flag) |
| 149 |
} else { |
| 150 |
format = fmt.Sprintf("invalid value '%s' for flag '--%s'", value, flag) |
| 151 |
} |
| 152 |
|
| 153 |
if len(msg) == 0 { |
| 154 |
return errors.New(format) |
| 155 |
} |
| 156 |
|
| 157 |
return errors.New(format + "; " + msg) |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
func IncompatibleFlag(ctx *cli.Context, flag string, value string) error { |
| 163 |
return errors.Errorf("flag '--%s' is incompatible with '%s'", flag, value) |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
func IncompatibleFlagWithFlag(ctx *cli.Context, flag string, withFlag string) error { |
| 169 |
return errors.Errorf("flag '--%s' is incompatible with '--%s'", flag, withFlag) |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
func IncompatibleFlagValue(ctx *cli.Context, flag, incompatibleWith, |
| 175 |
incompatibleWithValue string) error { |
| 176 |
return errors.Errorf("flag '--%s' is incompatible with flag '--%s %s'", |
| 177 |
flag, incompatibleWith, incompatibleWithValue) |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
func IncompatibleFlagValues(ctx *cli.Context, flag, value, incompatibleWith, |
| 183 |
incompatibleWithValue string) error { |
| 184 |
return errors.Errorf("flag '--%s %s' is incompatible with flag '--%s %s'", |
| 185 |
flag, value, incompatibleWith, incompatibleWithValue) |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
func IncompatibleFlagValueWithFlagValue(ctx *cli.Context, flag string, value string, |
| 192 |
withFlag string, withValue, options string) error { |
| 193 |
format := fmt.Sprintf("flag '--%s %s' is incompatible with flag '--%s %s'", |
| 194 |
flag, value, withFlag, withValue) |
| 195 |
|
| 196 |
if len(options) == 0 { |
| 197 |
return errors.New(format) |
| 198 |
} |
| 199 |
|
| 200 |
return errors.Errorf("%s\n\n Option(s): --%s %s", format, withFlag, options) |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
func RequiredFlag(ctx *cli.Context, flag string) error { |
| 205 |
return errors.Errorf("'%s %s' requires the '--%s' flag", ctx.App.HelpName, |
| 206 |
ctx.Command.Name, flag) |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
func RequiredWithFlag(ctx *cli.Context, flag, required string) error { |
| 211 |
return errors.Errorf("flag '--%s' requires the '--%s' flag", flag, required) |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
func RequiredWithFlagValue(ctx *cli.Context, flag, value, required string) error { |
| 216 |
return errors.Errorf("'--%s %s' requires the '--%s' flag", flag, value, required) |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
func RequiredWithProvisionerTypeFlag(ctx *cli.Context, provisionerType, required string) error { |
| 221 |
return errors.Errorf("provisioner type '%s' requires the '--%s' flag", provisionerType, required) |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
func RequiredInsecureFlag(ctx *cli.Context, flag string) error { |
| 227 |
return errors.Errorf("flag '--%s' requires the '--insecure' flag", flag) |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
func RequiredSubtleFlag(ctx *cli.Context, flag string) error { |
| 233 |
return errors.Errorf("flag '--%s' requires the '--subtle' flag", flag) |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
func RequiredUnlessInsecureFlag(ctx *cli.Context, flag string) error { |
| 239 |
return errors.Errorf("flag '--%s' is required unless the '--insecure' flag is provided", flag) |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
func RequiredUnlessFlag(ctx *cli.Context, flag, unlessFlag string) error { |
| 245 |
return errors.Errorf("flag '--%s' is required unless the '--%s' flag is provided", flag, unlessFlag) |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
func RequiredUnlessSubtleFlag(ctx *cli.Context, flag string) error { |
| 251 |
return errors.Errorf("flag '--%s' is required unless the '--subtle' flag is provided", flag) |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
func RequiredOrFlag(ctx *cli.Context, flags ...string) error { |
| 256 |
params := make([]string, len(flags)) |
| 257 |
for i, flag := range flags { |
| 258 |
params[i] = "--" + flag |
| 259 |
} |
| 260 |
return errors.Errorf("one of flag %s is required", strings.Join(params, " or ")) |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
func RequiredWithOrFlag(ctx *cli.Context, withFlag string, flags ...string) error { |
| 266 |
params := make([]string, len(flags)) |
| 267 |
for i := 0; i < len(flags); i++ { |
| 268 |
params[i] = "--" + flags[i] |
| 269 |
} |
| 270 |
return errors.Errorf("one of flag %s is required with flag --%s", strings.Join(params, " or "), withFlag) |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
func MinSizeFlag(ctx *cli.Context, flag string, size string) error { |
| 276 |
return errors.Errorf("flag '--%s' must be greater or equal than %s", flag, size) |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
func MinSizeInsecureFlag(ctx *cli.Context, flag, size string) error { |
| 282 |
return errors.Errorf("flag '--%s' requires at least %s unless '--insecure' flag is provided", flag, size) |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
func MutuallyExclusiveFlags(ctx *cli.Context, flag1, flag2 string) error { |
| 288 |
return errors.Errorf("flag '--%s' and flag '--%s' are mutually exclusive", flag1, flag2) |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
func UnsupportedFlag(ctx *cli.Context, flag string) error { |
| 294 |
return errors.Errorf("flag '--%s' is not yet supported", flag) |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
func usage(ctx *cli.Context) string { |
| 299 |
if len(ctx.Command.UsageText) == 0 { |
| 300 |
return fmt.Sprintf("%s %s [command options]", ctx.App.HelpName, ctx.Command.Name) |
| 301 |
} |
| 302 |
|
| 303 |
lines := strings.Split(ctx.Command.UsageText, "\n") |
| 304 |
return strings.Replace(lines[0], "**", "", -1) |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
func FileError(err error, filename string) error { |
| 309 |
if err == nil { |
| 310 |
return nil |
| 311 |
} |
| 312 |
switch e := err.(type) { |
| 313 |
case *os.PathError: |
| 314 |
return errors.Errorf("%s %s failed: %v", e.Op, e.Path, e.Err) |
| 315 |
case *os.LinkError: |
| 316 |
return errors.Errorf("%s %s %s failed: %v", e.Op, e.Old, e.New, e.Err) |
| 317 |
case *os.SyscallError: |
| 318 |
return errors.Errorf("%s failed: %v", e.Syscall, e.Err) |
| 319 |
default: |
| 320 |
return Wrap(err, "unexpected error on %s", filename) |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
type FriendlyError interface { |
| 326 |
Message() string |
| 327 |
} |