-
-
Notifications
You must be signed in to change notification settings - Fork 955
/
user_config.go
55 lines (45 loc) · 1.32 KB
/
user_config.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package cliconfig
import (
"os"
"path/filepath"
"github.com/gruntwork-io/go-commons/errors"
"github.com/gruntwork-io/terragrunt/util"
"github.com/hashicorp/terraform/command/cliconfig"
"github.com/hashicorp/terraform/tfdiags"
)
// The user configuration is read as raw data and stored at the top of the saved configuration file.
// The location of the default config is different for each OS https://developer.hashicorp.com/terraform/cli/config/config-file#locations
func LoadUserConfig() (*Config, error) {
return loadUserConfig(cliconfig.ConfigFile, cliconfig.LoadConfig)
}
func loadUserConfig(
configFileFn func() (string, error),
loadConfigFn func() (*cliconfig.Config, tfdiags.Diagnostics),
) (*Config, error) {
var rawHCL []byte
configFile, err := configFileFn()
if err != nil {
return nil, errors.WithStackTrace(err)
}
if util.FileExists(configFile) {
rawHCL, err = os.ReadFile(configFile)
if err != nil {
return nil, errors.WithStackTrace(err)
}
}
cfg, diag := loadConfigFn()
if diag.HasErrors() {
return nil, diag.Err()
}
return &Config{
rawHCL: rawHCL,
PluginCacheDir: cfg.PluginCacheDir,
}, nil
}
func UserProviderDir() (string, error) {
configDir, err := cliconfig.ConfigDir()
if err != nil {
return "", errors.WithStackTrace(err)
}
return filepath.Join(configDir, "plugins"), nil
}