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

	// Go 1.2 and 1.3 don't have os.Unsetenv
	os.Setenv("NAME", "")
	os.Setenv("LOG_PATH", "")

	err := envconfig.Init(&conf)
	equals(t, "envconfig: key NAME not found", err.Error())

	os.Setenv("NAME", "foobar")
	err = envconfig.Init(&conf)
	equals(t, "envconfig: key LOG_PATH not found", err.Error())

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

	equals(t, "foobar", conf.Name)
	equals(t, "/var/log/foobar", conf.Log.Path)
}
Beispiel #2
0
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)
	ok(t, err)

	equals(t, 2, len(conf.Names))
	equals(t, "foobar", conf.Names[0])
	equals(t, "barbaz", conf.Names[1])
	equals(t, 2, len(conf.Ports))
	equals(t, 900, conf.Ports[0])
	equals(t, 100, conf.Ports[1])
	equals(t, 2, len(conf.Shards))
	equals(t, "foobar", conf.Shards[0].Name)
	equals(t, "localhost:2929", conf.Shards[0].Addr)
	equals(t, "barbaz", conf.Shards[1].Name)
	equals(t, "localhost:2828", conf.Shards[1].Addr)
}
Beispiel #3
0
func TestUnexportedField(t *testing.T) {
	var conf struct {
		name string
	}

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

	err := envconfig.Init(&conf)
	equals(t, envconfig.ErrUnexportedField, err)
}
Beispiel #4
0
func TestParseBoolConfig(t *testing.T) {
	var conf struct {
		DoIt bool
	}

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

	err := envconfig.Init(&conf)
	ok(t, err)
	equals(t, true, conf.DoIt)
}
Beispiel #5
0
func TestParseCustomNameConfig(t *testing.T) {
	var conf struct {
		Name string `envconfig:"customName"`
	}

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

	err := envconfig.Init(&conf)
	ok(t, err)
	equals(t, "foobar", conf.Name)
}
Beispiel #6
0
func TestParseOptionalConfig(t *testing.T) {
	var conf struct {
		Name string `envconfig:"optional"`
	}

	os.Setenv("NAME", "")

	err := envconfig.Init(&conf)
	ok(t, err)
	equals(t, "", conf.Name)
}
Beispiel #7
0
func TestUnmarshaler(t *testing.T) {
	var conf struct {
		LogMode logMode
	}

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

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

	equals(t, logFile, conf.LogMode)
}
Beispiel #8
0
func TestDurationConfig(t *testing.T) {
	var conf struct {
		Timeout time.Duration
	}

	os.Setenv("TIMEOUT", "1m")

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

	equals(t, time.Minute*1, conf.Timeout)
}
Beispiel #9
0
func TestParseOptionalStruct(t *testing.T) {
	var conf struct {
		Master struct {
			Name string
		} `envconfig:"optional"`
	}

	os.Setenv("MASTER_NAME", "")

	err := envconfig.Init(&conf)
	ok(t, err)
	equals(t, "", conf.Master.Name)
}
Beispiel #10
0
func TestParseFloatConfig(t *testing.T) {
	var conf struct {
		Delta  float32
		DeltaV float64
	}

	os.Setenv("DELTA", "0.02")
	os.Setenv("DELTAV", "400.20000000001")

	err := envconfig.Init(&conf)
	ok(t, err)
	equals(t, float32(0.02), conf.Delta)
	equals(t, float64(400.20000000001), conf.DeltaV)
}
Beispiel #11
0
func TestAllPointerConfig(t *testing.T) {
	var conf struct {
		Name   *string
		Port   *int
		Delta  *float32
		DeltaV *float64
		Hosts  *[]string
		Shards *[]*struct {
			Name *string
			Addr *string
		}
		Master *struct {
			Name *string
			Addr *string
		}
		Timeout *time.Duration
	}

	os.Setenv("NAME", "foobar")
	os.Setenv("PORT", "9000")
	os.Setenv("DELTA", "40.01")
	os.Setenv("DELTAV", "200.00001")
	os.Setenv("HOSTS", "localhost,free.fr")
	os.Setenv("SHARDS", "{foobar,localhost:2828},{barbaz,localhost:2929}")
	os.Setenv("MASTER_NAME", "master")
	os.Setenv("MASTER_ADDR", "localhost:2727")
	os.Setenv("TIMEOUT", "1m")

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

	equals(t, "foobar", *conf.Name)
	equals(t, 9000, *conf.Port)
	equals(t, float32(40.01), *conf.Delta)
	equals(t, 200.00001, *conf.DeltaV)
	equals(t, 2, len(*conf.Hosts))
	equals(t, "localhost", (*conf.Hosts)[0])
	equals(t, "free.fr", (*conf.Hosts)[1])
	equals(t, 2, len(*conf.Shards))
	equals(t, "foobar", *(*conf.Shards)[0].Name)
	equals(t, "localhost:2828", *(*conf.Shards)[0].Addr)
	equals(t, "barbaz", *(*conf.Shards)[1].Name)
	equals(t, "localhost:2929", *(*conf.Shards)[1].Addr)
	equals(t, "master", *conf.Master.Name)
	equals(t, "localhost:2727", *conf.Master.Addr)
	equals(t, time.Minute*1, *conf.Timeout)
}
Beispiel #12
0
func TestParseIntegerConfig(t *testing.T) {
	var conf struct {
		Port    int
		Long    uint64
		Version uint8
	}

	timestamp := time.Now().UnixNano()

	os.Setenv("PORT", "80")
	os.Setenv("LONG", fmt.Sprintf("%d", timestamp))
	os.Setenv("VERSION", "2")

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

	equals(t, 80, conf.Port)
	equals(t, uint64(timestamp), conf.Long)
	equals(t, uint8(2), conf.Version)
}
Beispiel #13
0
func ExampleInit() {
	var conf struct {
		MySQL struct {
			Host     string
			Port     int
			Database struct {
				User     string
				Password string
				Name     string
			}
		}
		Log struct {
			Path   string
			Rotate bool
		}
		NbWorkers int
		Timeout   time.Duration
	}

	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("LOG_PATH", "/var/log/foobar.log")
	os.Setenv("LOG_ROTATE", "true")
	os.Setenv("NBWORKERS", "10")
	os.Setenv("TIMEOUT", "120s")

	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)
	// Output:
	// root
	// true
	// 2m0s
}
Beispiel #14
0
func main() {
	if err := envconfig.Init(&conf); err != nil {
		log.Fatal(err)
	}

	api = slack.New(conf.Token)
	rtm = api.NewRTM()
	go rtm.ManageConnection()

	for {
		select {
		case msg := <-rtm.IncomingEvents:
			switch ev := msg.Data.(type) {
			case *slack.StarAddedEvent:
				notify(newStar(ev.User, true, ev.Item))
			case *slack.StarRemovedEvent:
				notify(newStar(ev.User, false, ev.Item))
			case *slack.DisconnectedEvent:
				log.Println("Disconnection.")
				return
			case *slack.ConnectingEvent:
				log.Println("Connecting....")
			case *slack.ConnectedEvent:
				log.Println("Connected.")
				slackInfo = rtm.GetInfo()
			case *slack.MessageEvent:
				if strings.HasPrefix(ev.Channel, "D") {
					resp := gotDM(ev.Channel, ev.User, ev.Text)
					if resp != "" {
						api.PostMessage(ev.Channel, resp, slackParams)
					}
				}
			default:
				// skip
			}
		}
	}
}