-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
55 lines (51 loc) · 1.41 KB
/
main.go
File metadata and controls
55 lines (51 loc) · 1.41 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"flag"
"fmt"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
func main() {
/*Setear valores default
VALORES INICIALES
name_example = lore
color_example = green
number_example = 1
*/
viper.SetDefault("name_example", "lore")
viper.SetDefault("color_example", "green")
viper.SetDefault("number_example", 5)
/*Configuracion para leer el archivo yaml
VALORES LUEGO DE LEER EL YAML
name_example = owo
color_example = red
number_example = 1
*/
viper.SetConfigName("def")
viper.AddConfigPath("./files")
viper.SetConfigType("yml")
if err := viper.ReadInConfig(); err != nil {
fmt.Printf("Error reading config file, %s", err)
}
/*Leer y/o sobreescribir variables de entorno
VALORES LUEGO DE LEER VARIABLE DE ENTORNO (export COLOR_EXAMPLE=blue)
name_example = owo
color_example = blue
number_example = 1
*/
viper.AutomaticEnv()
/*Leer y/o sobre escribir flags
VALORES LUEGO DE LEER FLAGS (go run main.go --number_example=4)
name_example = owo
color_example = blue
number_example = 4
*/
flag.Int("number_example", 3, "number")
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()
viper.BindPFlag("number_example", pflag.CommandLine.Lookup("number_example"))
viper.BindPFlags(pflag.CommandLine)
fmt.Println("el nombre es", viper.Get("name_example"))
fmt.Println("el color es", viper.Get("color_example"))
fmt.Println("el número es", viper.Get("number_example"))
}