Esempio n. 1
0
func TestFile(t *testing.T) {
	data := []byte(`{"foo": "bar"}`)
	path := filepath.Join(os.TempDir(), fmt.Sprintf("file.%d", time.Now().UnixNano()))
	fh, err := os.Create(path)
	if err != nil {
		t.Error(err)
	}
	defer func() {
		fh.Close()
		os.Remove(path)
	}()

	_, err = fh.Write(data)
	if err != nil {
		t.Error(err)
	}

	f := NewSource(config.SourceName(path))
	c, err := f.Read()
	if err != nil {
		t.Error(err)
	}
	t.Logf("%+v", c)
	if string(c.Data) != string(data) {
		t.Error("data from file does not match")
	}
}
Esempio n. 2
0
func main() {
	flag.Parse()

	// Write our first entry
	if err := writeFile(0); err != nil {
		fmt.Println(err)
		return
	}
	defer os.Remove(configFile)

	// Create a config instance
	config := config.NewConfig(
		// aggressive config polling
		config.PollInterval(time.Millisecond*500),
		// use file as a config source
		config.WithSource(file.NewSource(config.SourceName(configFile))),
	)

	fmt.Println("Starting config runner")

	// Start the config runner which polls config
	config.Start()

	// lets read the value while editing it a number of times
	for i := 0; i < 10; i++ {
		val := config.Get("key").String("default")
		fmt.Println("Got ", val)
		writeFile(i + 1)
		time.Sleep(time.Second)
	}

	fmt.Println("Stopping config runner")

	// Stop the runner. The cache is still populated
	config.Stop()
}
Esempio n. 3
0
func main() {
	flag.Parse()

	// Write our first entry
	if err := writeFile(0); err != nil {
		fmt.Println(err)
		return
	}
	defer os.Remove(configFile)

	// Create a config instance
	config := config.NewConfig(
		// aggressive config polling
		config.PollInterval(time.Millisecond*500),
		// use file as a config source
		config.WithSource(file.NewSource(config.SourceName(configFile))),
	)

	defer config.Close()

	// lets read the value while editing it a number of times
	for i := 0; i < 10; i++ {
		val := config.Get("key").String("default")
		fmt.Println("Got ", val)
		writeFile(i + 1)
		time.Sleep(time.Second)
	}

	// watch key that exists
	watch(config, "key")

	// watch key that does not exist
	watch(config, "foo")

	fmt.Println("Stopping config runner")
}