1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-18 08:26:38 +02:00
go-micro/config/source/file
Shubhendra Singh Chauhan 26b859c4f9
improve code quality (#2128)
* Fix inefficient string comparison

* Fix unnecessary calls to Printf

* Canonicalize header key

* Replace `t.Sub(time.Now())` with `time.Until`

* Remove unnecessary blank (_) identifier

* Remove unnecessary use of slice

* Remove unnecessary comparison with bool
2021-02-25 08:30:35 +00:00
..
file_test.go v3 (#2104) 2021-01-20 13:54:31 +00:00
file.go v3 (#2104) 2021-01-20 13:54:31 +00:00
format_test.go v3 (#2104) 2021-01-20 13:54:31 +00:00
format.go v3 (#2104) 2021-01-20 13:54:31 +00:00
options.go v3 (#2104) 2021-01-20 13:54:31 +00:00
README.md add config 2019-05-30 23:11:13 +01:00
watcher_linux.go improve code quality (#2128) 2021-02-25 08:30:35 +00:00
watcher.go v3 (#2104) 2021-01-20 13:54:31 +00:00

File Source

The file source reads config from a file.

It uses the File extension to determine the Format e.g config.yaml has the yaml format. It does not make use of encoders or interpet the file data. If a file extension is not present the source Format will default to the Encoder in options.

Example

A config file format in json

{
    "hosts": {
        "database": {
            "address": "10.0.0.1",
            "port": 3306
        },
        "cache": {
            "address": "10.0.0.2",
            "port": 6379
        }
    }
}

New Source

Specify file source with path to file. Path is optional and will default to config.json

fileSource := file.NewSource(
	file.WithPath("/tmp/config.json"),
)

File Format

To load different file formats e.g yaml, toml, xml simply specify them with their extension

fileSource := file.NewSource(
        file.WithPath("/tmp/config.yaml"),
)

If you want to specify a file without extension, ensure you set the encoder to the same format

e := toml.NewEncoder()

fileSource := file.NewSource(
        file.WithPath("/tmp/config"),
	source.WithEncoder(e),
)

Load Source

Load the source into config

// Create new config
conf := config.NewConfig()

// Load file source
conf.Load(fileSource)