fallback.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package flags
  14. import (
  15. "errors"
  16. )
  17. const (
  18. FallbackExit = "exit"
  19. FallbackProxy = "proxy"
  20. )
  21. var (
  22. FallbackValues = []string{
  23. FallbackExit,
  24. FallbackProxy,
  25. }
  26. )
  27. // FallbackFlag implements the flag.Value interface.
  28. type Fallback string
  29. // Set verifies the argument to be a valid member of FallbackFlagValues
  30. // before setting the underlying flag value.
  31. func (fb *Fallback) Set(s string) error {
  32. for _, v := range FallbackValues {
  33. if s == v {
  34. *fb = Fallback(s)
  35. return nil
  36. }
  37. }
  38. return errors.New("invalid value")
  39. }
  40. func (fb *Fallback) String() string {
  41. return string(*fb)
  42. }