Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tmatias committed Sep 22, 2017
0 parents commit 627a462
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor/*
93 changes: 93 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"


[[constraint]]
name = "github.com/spf13/viper"
version = "1.0.0"
8 changes: 8 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[database]
hostname = "localhost"
port = 5432
username = "postgres"
password = ""

[output]
file = "out.txt"
41 changes: 41 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"fmt"
"os"

"github.com/spf13/viper"
)

type DatabaseConfig struct {
Host string `mapstructure:"hostname"`
Port string
User string `mapstructure:"username"`
Pass string `mapstructure:"password"`
}

type OutputConfig struct {
File string
}

type Config struct {
Db DatabaseConfig `mapstructure:"database"`
Out OutputConfig `mapstructure:"output"`
}

func main() {
v := viper.New()
v.SetConfigName("config")
v.AddConfigPath(".")
if err := v.ReadInConfig(); err != nil {
fmt.Printf("couldn't load config: %s", err)
os.Exit(1)
}
var c Config
if err := v.Unmarshal(&c); err != nil {
fmt.Printf("couldn't read config: %s", err)
}
fmt.Printf("host=%s port=%s user=%s pass=%s\n", c.Db.Host, c.Db.Port, c.Db.User, c.Db.Pass)
fmt.Printf("output=%s\n", c.Out.File)
fmt.Printf("%#v", c)
}

0 comments on commit 627a462

Please sign in to comment.