Exemple #1
0
// TestNotExists validates the ability to load configuration values
// using the OS-level environment variables and panic when something
// is missing.
func TestNotExists(t *testing.T) {
	t.Log("Given the need to panic when environment variables are missing.")
	{
		os.Setenv("MYAPP_PROC_ID", "322")
		os.Setenv("MYAPP_SOCKET", "./tmp/sockets.po")
		os.Setenv("MYAPP_PORT", "4034")

		cfg.Init("myapp")

		t.Log("\tWhen given a namspace key to search for that does NOT exist.")
		{
			shouldPanic(t, "stamp", func() {
				cfg.Time("stamp")
			})

			shouldPanic(t, "pid", func() {
				cfg.Int("pid")
			})

			shouldPanic(t, "dest", func() {
				cfg.String("dest")
			})

		}
	}
}
// ExampleDev shows how to use the config package.
func ExampleDev() {
	// Set up some basic environment variables.
	os.Setenv("DOCK_IP", "40.23.233.10")
	os.Setenv("DOCK_PORT", "4044")
	os.Setenv("DOCK_InitStamp", time.Date(2009, time.November,
		10, 15, 0, 0, 0, time.UTC).UTC().Format(time.UnixDate))

	// Init() must be called only once with the given namespace to load.
	cfg.Init("dock")

	// NOTE: All keys must be in lowercase.

	// To get the ip.
	fmt.Println(cfg.String("ip"))

	// To get the port number.
	fmt.Println(cfg.Int("port"))

	// To get the timestamp.
	fmt.Println(cfg.Time("initstamp"))

	// Output:
	// 40.23.233.10
	// 4044
	// 2009-11-10 15:00:00 +0000 UTC
}
Exemple #3
0
// TestExists validates the ability to load configuration values
// using the OS-level environment variables and read them back.
func TestExists(t *testing.T) {
	t.Log("Given the need to read environment variables.")
	{
		os.Setenv("MYAPP_PROC_ID", "322")
		os.Setenv("MYAPP_SOCKET", "./tmp/sockets.po")
		os.Setenv("MYAPP_PORT", "4034")

		cfg.Init("myapp")

		t.Log("\tWhen given a namspace key to search for that exists.")
		{
			if cfg.Int("proc_id") != 322 {
				t.Errorf("\t\t%s Should have key %q with value %d", failed, "proc_id", 322)
			} else {
				t.Logf("\t\t%s Should have key %q with value %d", succeed, "proc_id", 322)
			}

			if cfg.String("socket") != "./tmp/sockets.po" {
				t.Errorf("\t\t%s Should have key %q with value %q", failed, "socket", "./tmp/sockets.po")
			} else {
				t.Logf("\t\t%s Should have key %q with value %q", succeed, "socket", "./tmp/sockets.po")
			}

			if cfg.Int("port") != 4034 {
				t.Errorf("\t\t%s Should have key %q with value %d", failed, "port", 4034)
			} else {
				t.Logf("\t\t%s Should have key %q with value %d", succeed, "port", 4034)
			}
		}
	}
}