-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathviper.go
More file actions
93 lines (79 loc) · 2.81 KB
/
viper.go
File metadata and controls
93 lines (79 loc) · 2.81 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package config
import (
"bytes"
"path/filepath"
"strings"
"github.com/microsoft/go-sqlcmd/internal/localizer"
"github.com/microsoft/go-sqlcmd/internal/pal"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
)
// Load loads the configuration from the file specified by the SetFileName() function.
// Any errors encountered while marshalling or saving the configuration are checked
// and handled by the injected errorHandler (via the checkErr function).
func Load() {
if filename == "" {
panic("Must call config.SetFileName()")
}
var err error
err = viper.ReadInConfig()
checkErr(err)
err = viper.BindEnv("ACCEPT_EULA")
checkErr(err)
viper.AutomaticEnv() // read in environment variables that match
err = viper.Unmarshal(&config)
checkErr(err)
trace("Config loaded from file: %v"+pal.LineBreak(), viper.ConfigFileUsed())
}
// Save marshals the current configuration object and saves it to the configuration
// file previously specified by the SetFileName variable.
// Any errors encountered while marshalling or saving the configuration are checked
// and handled by the injected errorHandler (via the checkErr function).
func Save() {
if filename == "" {
panic("Must call config.SetFileName()")
}
if config.Version == "" {
config.Version = "v1"
}
b, err := yaml.Marshal(&config)
checkErr(err)
err = viper.ReadConfig(bytes.NewReader(b))
checkErr(err)
err = viper.WriteConfig()
checkErr(err)
}
// GetConfigFileUsed returns the path to the configuration file used by the Viper library.
func GetConfigFileUsed() string {
return viper.ConfigFileUsed()
}
// validateConfigFileExtension checks if the config file has a supported extension.
// Allows .yaml, .yml, and no extension (for default sqlconfig file).
func validateConfigFileExtension(configFile string) error {
ext := strings.ToLower(filepath.Ext(configFile))
if ext == "" || ext == ".yaml" || ext == ".yml" {
return nil
}
return localizer.Errorf(
"Configuration files must use YAML format (.yaml or .yml extension). "+
"File '%s' has unsupported extension '%s'.",
configFile, ext)
}
// configureViper initializes the Viper library with the given configuration file.
// This function sets the configuration file type to "yaml" and sets the environment variable prefix to "SQLCMD".
// It also sets the configuration file to use to the one provided as an argument to the function.
// This function is intended to be called at the start of the application to configure Viper before any other code uses it.
func configureViper(configFile string) error {
if configFile == "" {
panic("Must provide configFile")
}
if err := validateConfigFileExtension(configFile); err != nil {
return err
}
viper.SetConfigType("yaml")
viper.SetEnvPrefix("SQLCMD")
viper.SetConfigFile(configFile)
return nil
}