-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo-poc-bs-main.go
37 lines (37 loc) · 1.36 KB
/
go-poc-bs-main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package main
import (
c "./config"
"fmt"
"github.com/spf13/viper"
)
func main() {
// Set the file name of the configurations file
viper.SetConfigName("config")
// Set the path to look for the configurations file
viper.AddConfigPath(".")
// Enable VIPER to read Environment Variables
viper.AutomaticEnv()
viper.SetConfigType("yml")
var configuration c.Configurations
if err := viper.ReadInConfig(); err != nil {
fmt.Printf("Error reading config file, %s", err)
}
// Set undefined variables
viper.SetDefault("database.dbname", "test_db")
err := viper.Unmarshal(&configuration)
if err != nil {
fmt.Printf("Unable to decode into struct, %v", err)
}
// Reading variables using the model
fmt.Println("Reading variables using the model..")
fmt.Println("Database is\t", configuration.Database.DBName)
fmt.Println("Port is\t\t", configuration.Server.Port)
fmt.Println("DME2_GRM_SERVER_PORT is\t", configuration.DME2_GRM_SERVER_PORT)
fmt.Println("EXAMPLE_VAR is\t", configuration.EXAMPLE_VAR)
// Reading variables without using the model
fmt.Println("\nReading variables without using the model..")
fmt.Println("Database is\t", viper.GetString("database.dbname"))
fmt.Println("Port is\t\t", viper.GetInt("server.port"))
fmt.Println("DME2_GRM_SERVER_PORT is\t", viper.GetString("DME2_GRM_SERVER_PORT"))
fmt.Println("EXAMPLE_VAR is\t", viper.GetString("EXAMPLE_VAR"))
}