options.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  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 initupload
  14. import (
  15. "encoding/json"
  16. "flag"
  17. "k8s.io/test-infra/prow/gcsupload"
  18. )
  19. const (
  20. // JSONConfigEnvVar is the environment variable that
  21. // utilities expect to find a full JSON configuration
  22. // in when run.
  23. JSONConfigEnvVar = "INITUPLOAD_OPTIONS"
  24. )
  25. // NewOptions returns an empty Options with no nil fields
  26. func NewOptions() *Options {
  27. return &Options{
  28. Options: gcsupload.NewOptions(),
  29. }
  30. }
  31. type Options struct {
  32. *gcsupload.Options
  33. // Log is the log file to which clone records are written.
  34. // If unspecified, no clone records are uploaded.
  35. Log string `json:"log,omitempty"`
  36. }
  37. // ConfigVar exposes the environment variable used
  38. // to store serialized configuration
  39. func (o *Options) ConfigVar() string {
  40. return JSONConfigEnvVar
  41. }
  42. // LoadConfig loads options from serialized config
  43. func (o *Options) LoadConfig(config string) error {
  44. return json.Unmarshal([]byte(config), o)
  45. }
  46. // AddFlags binds flags to options
  47. func (o *Options) AddFlags(flags *flag.FlagSet) {
  48. flags.StringVar(&o.Log, "clone-log", "", "Path to the clone records log")
  49. o.Options.AddFlags(flags)
  50. }
  51. // Complete internalizes command line arguments
  52. func (o *Options) Complete(args []string) {
  53. o.Options.Complete(args)
  54. }
  55. // Encode will encode the set of options in the format
  56. // that is expected for the configuration environment variable
  57. func Encode(options Options) (string, error) {
  58. encoded, err := json.Marshal(options)
  59. return string(encoded), err
  60. }