Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- config.go
- // Copyright 2018 Shestakov Denis. All rights reserved.
- // Use of this source code is governed by a Lil
- // license that can be found in the LICENSE file.
- // Package config provides app configuration reading and validating.
- package config
- import (
- "fmt"
- "errors"
- "io/ioutil"
- "os"
- "gopkg.in/yaml.v2"
- )
- // Config described configuration file with reading.
- type Config interface {
- FromFile(path string) (Config, error)
- Version() Version
- }
- // FromFileUnm reads and validates config to `s` struct from `path` file
- // by unm function (like yaml.Unmarshal, json.Unmarshal and so on).
- func FromFileUnm(path string) (Config, error) {
- fileBytes, err := readFile(path)
- if err != nil {
- return nil, err
- }
- var confVer version
- yaml.Unmarshal(fileBytes, confVer)
- return nil, errors.New("Not implemented")
- }
- func readFile(path string) ([]byte, error) {
- file, err := os.Open(path)
- if err != nil {
- return nil, err
- }
- defer file.Close()
- fileBytes, err := ioutil.ReadAll(file)
- if err != nil {
- return nil, err
- }
- return fileBytes, nil
- }
- // V1_0 is the first implementation of config file.
- type V1_0 struct {
- Config
- }
- // FromFile reads and validates config to struct instance from `path` file.
- func (config *V1_0) FromFile(path string) {
- cfg, err := FromFileUnm(path)
- if err != nil {
- fmt.Println("Error")
- }
- fmt.Println("Config:", cfg)
- }
- main.go
- // Copyright 2018 Shestakov Denis. All rights reserved.
- // Use of this source code is governed by a Lil
- // license that can be found in the LICENSE file.
- package main
- import (
- "warlog/config"
- )
- func main() {
- var cfg config.V1_0;
- cfg.FromFile("config/test_data/test_1_0.yml")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement