summaryrefslogtreecommitdiff
path: root/vendor/github.com/pborman/getopt/error.go
diff options
context:
space:
mode:
authorDan Duvall <dduvall@wikimedia.org>2018-03-06 20:31:58 -0800
committerDan Duvall <dduvall@wikimedia.org>2018-03-19 15:55:16 -0700
commiteb9b69dd3d710cb7afa1dfb6e23a5987842b21cc (patch)
tree049b11cc885e4e9f54aac8981c91a1bf3620e7af /vendor/github.com/pborman/getopt/error.go
parent6896e655eb5cc88b90e66979bc2d862eb92cbb9f (diff)
downloadblubber-eb9b69dd3d710cb7afa1dfb6e23a5987842b21cc.tar.gz
Allow for configuration policies
Summary: Implements a rough interface for validating configuration against arbitrary policy rules. Policies are provided as YAML and passed via the command line as file paths or remote URIs. The format of policies is: enforcements: - path: <path> rule: <rule> Where `<path>` is a YAML-ish path to a config field and `<rule>` is any expression our config validator understands (expressions built in by the validator library and custom tags defined in `config.validation.go`). Example policy: enforcements: - path: variants.production.base rule: oneof=debian:jessie debian:stretch - path: variants.production.runs.as rule: ne=foo - path: variants.production.node.dependencies rule: isfalse Command flag parsing was implemented in `main.go` to support the new `--policy=uri` flag and improve existing handling of `--version` and the usage statement. Test Plan: Run `go test ./...`. Reviewers: thcipriani, demon, hashar, mmodell, #release-engineering-team Reviewed By: thcipriani, #release-engineering-team Tags: #release-engineering-team Differential Revision: https://phabricator.wikimedia.org/D999
Diffstat (limited to 'vendor/github.com/pborman/getopt/error.go')
-rw-r--r--vendor/github.com/pborman/getopt/error.go93
1 files changed, 93 insertions, 0 deletions
diff --git a/vendor/github.com/pborman/getopt/error.go b/vendor/github.com/pborman/getopt/error.go
new file mode 100644
index 0000000..3de8e86
--- /dev/null
+++ b/vendor/github.com/pborman/getopt/error.go
@@ -0,0 +1,93 @@
+// Copyright 2013 Google Inc. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package getopt
+
+import "fmt"
+
+// An Error is returned by Getopt when it encounters an error.
+type Error struct {
+ ErrorCode // General reason of failure.
+ Err error // The actual error.
+ Parameter string // Parameter passed to option, if any
+ Name string // Option that cause error, if any
+}
+
+// Error returns the error message, implementing the error interface.
+func (i *Error) Error() string { return i.Err.Error() }
+
+// An ErrorCode indicates what sort of error was encountered.
+type ErrorCode int
+
+const (
+ NoError = ErrorCode(iota)
+ UnknownOption // an invalid option was encountered
+ MissingParameter // the options parameter is missing
+ ExtraParameter // a value was set to a long flag
+ Invalid // attempt to set an invalid value
+)
+
+func (e ErrorCode) String() string {
+ switch e {
+ case UnknownOption:
+ return "unknow option"
+ case MissingParameter:
+ return "missing argument"
+ case ExtraParameter:
+ return "unxpected value"
+ case Invalid:
+ return "error setting value"
+ }
+ return "unknown error"
+}
+
+// unknownOption returns an Error indicating an unknown option was
+// encountered.
+func unknownOption(name interface{}) *Error {
+ i := &Error{ErrorCode: UnknownOption}
+ switch n := name.(type) {
+ case rune:
+ if n == '-' {
+ i.Name = "-"
+ } else {
+ i.Name = "-" + string(n)
+ }
+ case string:
+ i.Name = "--" + n
+ }
+ i.Err = fmt.Errorf("unknown option: %s", i.Name)
+ return i
+}
+
+// missingArg returns an Error inidicating option o was not passed
+// a required paramter.
+func missingArg(o Option) *Error {
+ return &Error{
+ ErrorCode: MissingParameter,
+ Name: o.Name(),
+ Err: fmt.Errorf("missing parameter for %s", o.Name()),
+ }
+}
+
+// extraArg returns an Error inidicating option o was passed the
+// unexpected paramter value.
+func extraArg(o Option, value string) *Error {
+ return &Error{
+ ErrorCode: ExtraParameter,
+ Name: o.Name(),
+ Parameter: value,
+ Err: fmt.Errorf("unexpected parameter passed to %s: %q", o.Name(), value),
+ }
+}
+
+// setError returns an Error inidicating option o and the specified
+// error while setting it to value.
+func setError(o Option, value string, err error) *Error {
+ return &Error{
+ ErrorCode: Invalid,
+ Name: o.Name(),
+ Parameter: value,
+ Err: err,
+ }
+}