当前位置: 首页>>代码示例>>Golang>>正文


Golang envconfig.Init函数代码示例

本文整理汇总了Golang中github.com/vrischmann/envconfig.Init函数的典型用法代码示例。如果您正苦于以下问题:Golang Init函数的具体用法?Golang Init怎么用?Golang Init使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Init函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: TestParseSimpleConfig

func TestParseSimpleConfig(t *testing.T) {
	var conf struct {
		Name string
		Log  struct {
			Path string
		}
	}

	err := envconfig.Init(&conf)
	require.Equal(t, "envconfig: keys NAME, name not found", err.Error())

	os.Setenv("NAME", "foobar")
	err = envconfig.Init(&conf)
	require.Equal(t, "envconfig: keys LOG_PATH, log_path not found", err.Error())

	os.Setenv("LOG_PATH", "/var/log/foobar")
	err = envconfig.Init(&conf)
	require.Nil(t, err)

	require.Equal(t, "foobar", conf.Name)
	require.Equal(t, "/var/log/foobar", conf.Log.Path)

	// Clean up at the end of the test - some tests share the same key and we don't values to be seen by those tests
	os.Setenv("NAME", "")
	os.Setenv("LOG_PATH", "")
}
开发者ID:mozilla-services,项目名称:go-syncstorage,代码行数:26,代码来源:envconfig_test.go

示例2: TestParseWrongValues

func TestParseWrongValues(t *testing.T) {
	var conf struct{ OK bool }
	os.Setenv("OK", "foobar")
	err := envconfig.Init(&conf)
	require.Equal(t, `strconv.ParseBool: parsing "foobar": invalid syntax`, err.Error())

	var conf2 struct{ Port int }
	os.Setenv("PORT", "foobar")
	err = envconfig.Init(&conf2)
	require.Equal(t, `strconv.ParseInt: parsing "foobar": invalid syntax`, err.Error())

	var conf3 struct{ Port uint }
	os.Setenv("PORT", "foobar")
	err = envconfig.Init(&conf3)
	require.Equal(t, `strconv.ParseUint: parsing "foobar": invalid syntax`, err.Error())

	var conf4 struct{ Port float32 }
	os.Setenv("PORT", "foobar")
	err = envconfig.Init(&conf4)
	require.Equal(t, `strconv.ParseFloat: parsing "foobar": invalid syntax`, err.Error())

	var conf5 struct{ Data []byte }
	os.Setenv("DATA", "foobar")
	err = envconfig.Init(&conf5)
	require.Equal(t, "illegal base64 data at input byte 4", err.Error())
}
开发者ID:mozilla-services,项目名称:go-syncstorage,代码行数:26,代码来源:envconfig_test.go

示例3: TestParseDefaultVal

func TestParseDefaultVal(t *testing.T) {
	var conf struct {
		MySQL struct {
			Master struct {
				Address string `envconfig:"default=localhost"`
				Port    int    `envconfig:"default=3306"`
			}
			Timeout      time.Duration `envconfig:"default=1m,myTimeout"`
			LocalTimeout time.Duration `envconfig:"myTimeout2,default=1m"`
		}
	}

	err := envconfig.Init(&conf)
	require.Nil(t, err)
	require.Equal(t, "localhost", conf.MySQL.Master.Address)
	require.Equal(t, 3306, conf.MySQL.Master.Port)
	require.Equal(t, time.Minute*1, conf.MySQL.Timeout)

	os.Setenv("myTimeout", "2m")
	os.Setenv("myTimeout2", "20m")

	err = envconfig.Init(&conf)
	require.Nil(t, err)
	require.Equal(t, "localhost", conf.MySQL.Master.Address)
	require.Equal(t, 3306, conf.MySQL.Master.Port)
	require.Equal(t, time.Minute*2, conf.MySQL.Timeout)
	require.Equal(t, time.Minute*20, conf.MySQL.LocalTimeout)
}
开发者ID:mozilla-services,项目名称:go-syncstorage,代码行数:28,代码来源:envconfig_test.go

示例4: TestParseSliceConfig

func TestParseSliceConfig(t *testing.T) {
	var conf struct {
		Names  []string
		Ports  []int
		Shards []struct {
			Name string
			Addr string
		}
	}

	os.Setenv("NAMES", "foobar,barbaz")
	os.Setenv("PORTS", "900,100")
	os.Setenv("SHARDS", "{foobar,localhost:2929},{barbaz,localhost:2828}")

	err := envconfig.Init(&conf)
	require.Nil(t, err)

	require.Equal(t, 2, len(conf.Names))
	require.Equal(t, "foobar", conf.Names[0])
	require.Equal(t, "barbaz", conf.Names[1])
	require.Equal(t, 2, len(conf.Ports))
	require.Equal(t, 900, conf.Ports[0])
	require.Equal(t, 100, conf.Ports[1])
	require.Equal(t, 2, len(conf.Shards))
	require.Equal(t, "foobar", conf.Shards[0].Name)
	require.Equal(t, "localhost:2929", conf.Shards[0].Addr)
	require.Equal(t, "barbaz", conf.Shards[1].Name)
	require.Equal(t, "localhost:2828", conf.Shards[1].Addr)
}
开发者ID:mozilla-services,项目名称:go-syncstorage,代码行数:29,代码来源:envconfig_test.go

示例5: TestParseOptionalConfig

func TestParseOptionalConfig(t *testing.T) {
	var conf struct {
		Name    string        `envconfig:"optional"`
		Flag    bool          `envconfig:"optional"`
		Timeout time.Duration `envconfig:"optional"`
		Port    int           `envconfig:"optional"`
		Port2   uint          `envconfig:"optional"`
		Delta   float32       `envconfig:"optional"`
		DeltaV  float64       `envconfig:"optional"`
		Slice   []string      `envconfig:"optional"`
		Struct  struct {
			A string
			B int
		} `envconfig:"optional"`
	}

	os.Setenv("NAME", "")
	os.Setenv("FLAG", "")
	os.Setenv("TIMEOUT", "")
	os.Setenv("PORT", "")
	os.Setenv("PORT2", "")
	os.Setenv("DELTA", "")
	os.Setenv("DELTAV", "")
	os.Setenv("SLICE", "")
	os.Setenv("STRUCT", "")

	err := envconfig.Init(&conf)
	require.Nil(t, err)
	require.Equal(t, "", conf.Name)
}
开发者ID:remeh,项目名称:envconfig,代码行数:30,代码来源:envconfig_test.go

示例6: TestInvalidDurationConfig

func TestInvalidDurationConfig(t *testing.T) {
	var conf struct {
		Timeout time.Duration
	}

	os.Setenv("TIMEOUT", "foo")

	err := envconfig.Init(&conf)
	require.NotNil(t, err)
}
开发者ID:mozilla-services,项目名称:go-syncstorage,代码行数:10,代码来源:envconfig_test.go

示例7: TestInvalidSliceElementValueKind

func TestInvalidSliceElementValueKind(t *testing.T) {
	var conf struct {
		Foo []interface{}
	}

	os.Setenv("FOO", "lalala")

	err := envconfig.Init(&conf)
	require.Equal(t, "envconfig: kind interface not supported", err.Error())
}
开发者ID:mozilla-services,项目名称:go-syncstorage,代码行数:10,代码来源:envconfig_test.go

示例8: initializeConfig

// initialize config and turns it to map
func initializeConfig() map[string]string {
	err := envconfig.Init(&conf)
	if err != nil {
		log.Panic("Error on env config initialize! ", err.Error())
	}

	return map[string]string{
		"dbConnection": conf.DB.Connection,
	}
}
开发者ID:NikitaSmall,项目名称:radio-go,代码行数:11,代码来源:config.go

示例9: TestUnmarshaler

func TestUnmarshaler(t *testing.T) {
	var conf struct {
		LogMode logMode
	}

	os.Setenv("LOGMODE", "file")

	err := envconfig.Init(&conf)
	require.Nil(t, err)
	require.Equal(t, logFile, conf.LogMode)

	var conf2 struct {
		LogMode *logMode
	}

	err = envconfig.Init(&conf2)
	require.Nil(t, err)
	require.Equal(t, logFile, *conf2.LogMode)
}
开发者ID:mozilla-services,项目名称:go-syncstorage,代码行数:19,代码来源:envconfig_test.go

示例10: ExampleInit

func ExampleInit() {
	var conf struct {
		MySQL struct {
			Host     string
			Port     int
			Database struct {
				User     string
				Password string
				Name     string
			}
			Params struct {
				Charset string `envconfig:"-"`
			}
		}
		Log struct {
			Path   string `envconfig:"default=/var/log/mylog.log"`
			Rotate bool   `envconfig:"logRotate"`
		}
		NbWorkers int
		Timeout   time.Duration
		Cassandra struct {
			SSLCert string
			SSLKey  string
		}
	}

	os.Setenv("MYSQL_HOST", "localhost")
	os.Setenv("MYSQL_PORT", "3306")
	os.Setenv("MYSQL_DATABASE_USER", "root")
	os.Setenv("MYSQL_DATABASE_PASSWORD", "foobar")
	os.Setenv("MYSQL_DATABASE_NAME", "default")
	os.Setenv("logRotate", "true")
	os.Setenv("NBWORKERS", "10")
	os.Setenv("TIMEOUT", "120s")
	os.Setenv("CASSANDRA_SSL_CERT", "/etc/cassandra/ssl.crt")
	os.Setenv("CASSANDRA_SSL_KEY", "/etc/cassandra/ssl.key")

	if err := envconfig.Init(&conf); err != nil {
		fmt.Printf("err=%s\n", err)
	}

	fmt.Println(conf.MySQL.Database.User)
	fmt.Println(conf.Log.Rotate)
	fmt.Println(conf.Timeout)
	fmt.Println(conf.Log.Path)
	fmt.Println(conf.Cassandra.SSLCert)
	fmt.Println(conf.Cassandra.SSLKey)
	// Output:
	// root
	// true
	// 2m0s
	// /var/log/mylog.log
	// /etc/cassandra/ssl.crt
	// /etc/cassandra/ssl.key
}
开发者ID:remeh,项目名称:envconfig,代码行数:55,代码来源:example_test.go

示例11: TestParseEmptyTag

func TestParseEmptyTag(t *testing.T) {
	var conf struct {
		Name string `envconfig:""`
	}

	os.Setenv("NAME", "foobar")

	err := envconfig.Init(&conf)
	require.Nil(t, err)
	require.Equal(t, "foobar", conf.Name)
}
开发者ID:mozilla-services,项目名称:go-syncstorage,代码行数:11,代码来源:envconfig_test.go

示例12: TestParseMapType

func TestParseMapType(t *testing.T) {
	var conf struct {
		Map myMapType
	}

	os.Setenv("MAP", "a")

	err := envconfig.Init(&conf)
	require.Nil(t, err)
	require.Equal(t, 1, conf.Map["a"])
}
开发者ID:mozilla-services,项目名称:go-syncstorage,代码行数:11,代码来源:envconfig_test.go

示例13: TestParseCustomNameConfig

func TestParseCustomNameConfig(t *testing.T) {
	var conf struct {
		Name string `envconfig:"customName"`
	}

	os.Setenv("customName", "foobar")

	err := envconfig.Init(&conf)
	require.Nil(t, err)
	require.Equal(t, "foobar", conf.Name)
}
开发者ID:mozilla-services,项目名称:go-syncstorage,代码行数:11,代码来源:envconfig_test.go

示例14: TestParseSkippableConfig

func TestParseSkippableConfig(t *testing.T) {
	var conf struct {
		Flag bool `envconfig:"-"`
	}

	os.Setenv("FLAG", "true")

	err := envconfig.Init(&conf)
	require.Nil(t, err)
	require.Equal(t, false, conf.Flag)
}
开发者ID:mozilla-services,项目名称:go-syncstorage,代码行数:11,代码来源:envconfig_test.go

示例15: TestParseBoolConfig

func TestParseBoolConfig(t *testing.T) {
	var conf struct {
		DoIt bool
	}

	os.Setenv("DOIT", "true")

	err := envconfig.Init(&conf)
	require.Nil(t, err)
	require.Equal(t, true, conf.DoIt)
}
开发者ID:mozilla-services,项目名称:go-syncstorage,代码行数:11,代码来源:envconfig_test.go


注:本文中的github.com/vrischmann/envconfig.Init函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。