github.com/lyft/flytestdlib@v0.3.12-0.20210213045714-8cdd111ecda1/config/tests/types_test.go (about)

     1  package tests
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/lyft/flytestdlib/config"
     7  	"github.com/lyft/flytestdlib/config/viper"
     8  	"github.com/spf13/pflag"
     9  )
    10  
    11  const testNameFormatter = "[%v] %v"
    12  
    13  var providers = []config.AccessorProvider{viper.NewAccessor}
    14  
    15  type MyComponentConfig struct {
    16  	StringValue  string `json:"str"`
    17  	StringValue2 string `json:"str2"`
    18  	StringValue3 string `json:"str3"`
    19  }
    20  
    21  type OtherComponentConfig struct {
    22  	DurationValue           config.Duration `json:"duration-value"`
    23  	URLValue                config.URL      `json:"url-value"`
    24  	StringValue             string          `json:"string-value"`
    25  	IntValue                int             `json:"int-val"`
    26  	StringArray             []string        `json:"strings"`
    27  	StringArrayWithDefaults []string        `json:"strings-def"`
    28  }
    29  
    30  func (MyComponentConfig) GetPFlagSet(prefix string) *pflag.FlagSet {
    31  	cmdFlags := pflag.NewFlagSet("MyComponentConfig", pflag.ExitOnError)
    32  	cmdFlags.String(fmt.Sprintf("%v%v", prefix, "str"), "hello world", "life is short")
    33  	cmdFlags.String(fmt.Sprintf("%v%v", prefix, "str2"), "hello world", "life is short")
    34  	cmdFlags.String(fmt.Sprintf("%v%v", prefix, "str3"), "hello world", "life is short")
    35  	return cmdFlags
    36  }
    37  
    38  func (OtherComponentConfig) GetPFlagSet(prefix string) *pflag.FlagSet {
    39  	cmdFlags := pflag.NewFlagSet("MyComponentConfig", pflag.ExitOnError)
    40  	cmdFlags.String(fmt.Sprintf("%v%v", prefix, "string-value"), "hello world", "life is short")
    41  	cmdFlags.String(fmt.Sprintf("%v%v", prefix, "duration-value"), "20s", "")
    42  	cmdFlags.Int(fmt.Sprintf("%v%v", prefix, "int-val"), 4, "this is an important flag")
    43  	cmdFlags.String(fmt.Sprintf("%v%v", prefix, "url-value"), "http://blah.com", "Sets the type of storage to configure [s3/minio/local/mem].")
    44  	cmdFlags.StringSlice(fmt.Sprintf("%v%v", prefix, "strings-def"), []string{"default value"}, "Sets the type of storage to configure [s3/minio/local/mem].")
    45  	return cmdFlags
    46  }
    47  
    48  type TestConfig struct {
    49  	MyComponentConfig    MyComponentConfig    `json:"my-component"`
    50  	OtherComponentConfig OtherComponentConfig `json:"other-component"`
    51  }
    52  
    53  const (
    54  	MyComponentSectionKey    = "my-component"
    55  	OtherComponentSectionKey = "other-component"
    56  )
    57  
    58  func init() {
    59  	if _, err := config.RegisterSection(MyComponentSectionKey, &MyComponentConfig{}); err != nil {
    60  		panic(err)
    61  	}
    62  
    63  	if _, err := config.RegisterSection(OtherComponentSectionKey, &OtherComponentConfig{}); err != nil {
    64  		panic(err)
    65  	}
    66  }