Advertisement
Guest User

Untitled

a guest
Mar 11th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.74 KB | None | 0 0
  1. config.go
  2.  
  3. // Copyright 2018 Shestakov Denis. All rights reserved.
  4. // Use of this source code is governed by a Lil
  5. // license that can be found in the LICENSE file.
  6.  
  7. // Package config provides app configuration reading and validating.
  8. package config
  9.  
  10. import (
  11.   "fmt"
  12.   "errors"
  13.   "io/ioutil"
  14.   "os"
  15.  
  16.   "gopkg.in/yaml.v2"
  17. )
  18.  
  19. // Config described configuration file with reading.
  20. type Config interface {
  21.   FromFile(path string) (Config, error)
  22.   Version() Version
  23. }
  24.  
  25. // FromFileUnm reads and validates config to `s` struct from `path` file
  26. // by unm function (like yaml.Unmarshal, json.Unmarshal and so on).
  27. func FromFileUnm(path string) (Config, error) {
  28.   fileBytes, err := readFile(path)
  29.   if err != nil {
  30.     return nil, err
  31.   }
  32.  
  33.   var confVer version
  34.   yaml.Unmarshal(fileBytes, confVer)
  35.  
  36.   return nil, errors.New("Not implemented")
  37. }
  38.  
  39. func readFile(path string) ([]byte, error) {
  40.   file, err := os.Open(path)
  41.   if err != nil {
  42.     return nil, err
  43.   }
  44.   defer file.Close()
  45.  
  46.   fileBytes, err := ioutil.ReadAll(file)
  47.   if err != nil {
  48.     return nil, err
  49.   }
  50.  
  51.   return fileBytes, nil
  52. }
  53.  
  54. // V1_0 is the first implementation of config file.
  55. type V1_0 struct {
  56.   Config
  57. }
  58.  
  59. // FromFile reads and validates config to struct instance from `path` file.
  60. func (config *V1_0) FromFile(path string) {
  61.   cfg, err := FromFileUnm(path)
  62.   if err != nil {
  63.     fmt.Println("Error")
  64.   }
  65.   fmt.Println("Config:", cfg)
  66. }
  67.  
  68. main.go
  69.  
  70. // Copyright 2018 Shestakov Denis. All rights reserved.
  71. // Use of this source code is governed by a Lil
  72. // license that can be found in the LICENSE file.
  73.  
  74. package main
  75.  
  76. import (
  77.     "warlog/config"
  78. )
  79.  
  80.  
  81. func main() {
  82.     var cfg config.V1_0;
  83.     cfg.FromFile("config/test_data/test_1_0.yml")
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement