Debian Codegraph @

pkg/crc/config/viper_config.go

raw symbols SCIP
1 package configscip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/
2
3 import (
4 "encoding/jsonscip-go gomod github.com/golang/go/src go1.20 `encoding/json`/"
5 "fmtscip-go gomod github.com/golang/go/src go1.20 fmt/"
6 "osscip-go gomod github.com/golang/go/src go1.20 os/"
7 "path/filepathscip-go gomod github.com/golang/go/src go1.20 `path/filepath`/"
8 "stringsscip-go gomod github.com/golang/go/src go1.20 strings/"
9 "syncscip-go gomod github.com/golang/go/src go1.20 sync/"
10
11 "github.com/spf13/pflagscip-go gomod github.com/spf13/pflag v1.0.5 `github.com/spf13/pflag`/"
12 "github.com/spf13/viperscip-go gomod github.com/spf13/viper v1.18.2 `github.com/spf13/viper`/"
13 )
14
15 type ViperStoragescip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage# struct {
16 storeLockscip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#storeLock. *syncscip-go gomod github.com/golang/go/src go1.20 sync/.Mutexscip-go gomod github.com/golang/go/src go1.20 sync/Mutex#
17
18 // store flag values
19 flagSetscip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#flagSet. *pflagscip-go gomod github.com/spf13/pflag v1.0.5 `github.com/spf13/pflag`/.FlagSetscip-go gomod github.com/spf13/pflag v1.0.5 `github.com/spf13/pflag`/FlagSet#
20
21 configFilescip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#configFile. string
22 envPrefixscip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#envPrefix. string
23 }
24
25 func NewViperStoragescip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/NewViperStorage().(configFile, envPrefix string) (*ViperStoragescip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#, error) {
26 return &ViperStoragescip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#{
27 storeLockscip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#storeLock.: &syncscip-go gomod github.com/golang/go/src go1.20 sync/.Mutexscip-go gomod github.com/golang/go/src go1.20 sync/Mutex#{},
28 configFilescip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#configFile.: configFile,
29 envPrefixscip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#envPrefix.: envPrefix,
30 }, nil
31 }
32
33 func (c *ViperStoragescip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#) viperInstancescip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#viperInstance().() (*viperscip-go gomod github.com/spf13/viper v1.18.2 `github.com/spf13/viper`/.Viperscip-go gomod github.com/spf13/viper v1.18.2 `github.com/spf13/viper`/Viper#, error) {
34 if err := ensureConfigFileExistsscip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ensureConfigFileExists().(c.configFilescip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#configFile.); err != nil {
35 return nil, err
36 }
37 v := viperscip-go gomod github.com/spf13/viper v1.18.2 `github.com/spf13/viper`/.Newscip-go gomod github.com/spf13/viper v1.18.2 `github.com/spf13/viper`/New().()
38 v.SetConfigFilescip-go gomod github.com/spf13/viper v1.18.2 `github.com/spf13/viper`/Viper#SetConfigFile().(c.configFilescip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#configFile.)
39 v.SetConfigTypescip-go gomod github.com/spf13/viper v1.18.2 `github.com/spf13/viper`/Viper#SetConfigType().("json")
40 v.SetEnvPrefixscip-go gomod github.com/spf13/viper v1.18.2 `github.com/spf13/viper`/Viper#SetEnvPrefix().(c.envPrefixscip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#envPrefix.)
41 // Replaces '-' in flags with '_' in env variables
42 v.SetEnvKeyReplacerscip-go gomod github.com/spf13/viper v1.18.2 `github.com/spf13/viper`/Viper#SetEnvKeyReplacer().(stringsscip-go gomod github.com/golang/go/src go1.20 strings/.NewReplacerscip-go gomod github.com/golang/go/src go1.20 strings/NewReplacer().("-", "_"))
43 v.AutomaticEnvscip-go gomod github.com/spf13/viper v1.18.2 `github.com/spf13/viper`/Viper#AutomaticEnv().()
44 v.SetTypeByDefaultValuescip-go gomod github.com/spf13/viper v1.18.2 `github.com/spf13/viper`/Viper#SetTypeByDefaultValue().(true)
45 if err := v.ReadInConfigscip-go gomod github.com/spf13/viper v1.18.2 `github.com/spf13/viper`/Viper#ReadInConfig().(); err != nil {
46 return nil, fmtscip-go gomod github.com/golang/go/src go1.20 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.20 fmt/Errorf().("error reading configuration file '%s': %v", c.configFilescip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#configFile., err)
47 }
48 if c.flagSetscip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#flagSet. == nil {
49 return v, nil
50 }
51 return v, v.BindPFlagsscip-go gomod github.com/spf13/viper v1.18.2 `github.com/spf13/viper`/Viper#BindPFlags().(c.flagSetscip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#flagSet.)
52 }
53
54 func (c *ViperStoragescip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#) Getscip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#Get().(key string) interface{} {
55 c.storeLockscip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#storeLock..Lockscip-go gomod github.com/golang/go/src go1.20 sync/Mutex#Lock().()
56 defer c.storeLockscip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#storeLock..Unlockscip-go gomod github.com/golang/go/src go1.20 sync/Mutex#Unlock().()
57 viperInstance, err := c.viperInstancescip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#viperInstance().()
58 if err != nil {
59 return nil
60 }
61 return viperInstance.Getscip-go gomod github.com/spf13/viper v1.18.2 `github.com/spf13/viper`/Viper#Get().(key)
62 }
63
64 func (c *ViperStoragescip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#) Setscip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#Set().(key string, value interface{}) error {
65 c.storeLockscip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#storeLock..Lockscip-go gomod github.com/golang/go/src go1.20 sync/Mutex#Lock().()
66 defer c.storeLockscip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#storeLock..Unlockscip-go gomod github.com/golang/go/src go1.20 sync/Mutex#Unlock().()
67 if err := ensureConfigFileExistsscip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ensureConfigFileExists().(c.configFilescip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#configFile.); err != nil {
68 return err
69 }
70 in, err := osscip-go gomod github.com/golang/go/src go1.20 os/.ReadFilescip-go gomod github.com/golang/go/src go1.20 os/ReadFile().(c.configFilescip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#configFile.)
71 if err != nil {
72 return err
73 }
74 var cfg map[string]interface{}
75 if err := jsonscip-go gomod github.com/golang/go/src go1.20 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.20 `encoding/json`/Unmarshal().(in, &cfg); err != nil {
76 return err
77 }
78 cfg[key] = value
79 bin, err := jsonscip-go gomod github.com/golang/go/src go1.20 `encoding/json`/.MarshalIndentscip-go gomod github.com/golang/go/src go1.20 `encoding/json`/MarshalIndent().(cfg, "", " ")
80 if err != nil {
81 return err
82 }
83 return atomicWritescip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/atomicWrite().(bin, c.configFilescip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#configFile.)
84 }
85
86 func (c *ViperStoragescip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#) Unsetscip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#Unset().(key string) error {
87 c.storeLockscip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#storeLock..Lockscip-go gomod github.com/golang/go/src go1.20 sync/Mutex#Lock().()
88 defer c.storeLockscip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#storeLock..Unlockscip-go gomod github.com/golang/go/src go1.20 sync/Mutex#Unlock().()
89 if err := ensureConfigFileExistsscip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ensureConfigFileExists().(c.configFilescip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#configFile.); err != nil {
90 return err
91 }
92 in, err := osscip-go gomod github.com/golang/go/src go1.20 os/.ReadFilescip-go gomod github.com/golang/go/src go1.20 os/ReadFile().(c.configFilescip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#configFile.)
93 if err != nil {
94 return err
95 }
96 var cfg map[string]interface{}
97 if err := jsonscip-go gomod github.com/golang/go/src go1.20 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.20 `encoding/json`/Unmarshal().(in, &cfg); err != nil {
98 return err
99 }
100 delete(cfg, key)
101 bin, err := jsonscip-go gomod github.com/golang/go/src go1.20 `encoding/json`/.MarshalIndentscip-go gomod github.com/golang/go/src go1.20 `encoding/json`/MarshalIndent().(cfg, "", " ")
102 if err != nil {
103 return err
104 }
105 return atomicWritescip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/atomicWrite().(bin, c.configFilescip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#configFile.)
106 }
107
108 // BindFlagset binds a flagset to their respective config properties
109 func (c *ViperStoragescip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#) BindFlagSetscip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#BindFlagSet().(flagSet *pflagscip-go gomod github.com/spf13/pflag v1.0.5 `github.com/spf13/pflag`/.FlagSetscip-go gomod github.com/spf13/pflag v1.0.5 `github.com/spf13/pflag`/FlagSet#) error {
110 c.storeLockscip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#storeLock..Lockscip-go gomod github.com/golang/go/src go1.20 sync/Mutex#Lock().()
111 defer c.storeLockscip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#storeLock..Unlockscip-go gomod github.com/golang/go/src go1.20 sync/Mutex#Unlock().()
112 c.flagSetscip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ViperStorage#flagSet. = flagSet
113 return nil
114 }
115
116 // ensureConfigFileExists creates the viper config file if it does not exists
117 func ensureConfigFileExistsscip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/ensureConfigFileExists().(file string) error {
118 _, err := osscip-go gomod github.com/golang/go/src go1.20 os/.Statscip-go gomod github.com/golang/go/src go1.20 os/Stat().(file)
119 if osscip-go gomod github.com/golang/go/src go1.20 os/.IsNotExistscip-go gomod github.com/golang/go/src go1.20 os/IsNotExist().(err) {
120 return osscip-go gomod github.com/golang/go/src go1.20 os/.WriteFilescip-go gomod github.com/golang/go/src go1.20 os/WriteFile().(file, []byte("{}\n"), 0600)
121 }
122 return err
123 }
124
125 func atomicWritescip-go gomod github.com/crc-org/crc/v2 . `github.com/crc-org/crc/v2/pkg/crc/config`/atomicWrite().(bin []byte, configFile string) error {
126 ext := filepathscip-go gomod github.com/golang/go/src go1.20 `path/filepath`/.Extscip-go gomod github.com/golang/go/src go1.20 `path/filepath`/Ext().(configFile)
127 pattern := fmtscip-go gomod github.com/golang/go/src go1.20 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.20 fmt/Sprintf().("%s*%s", stringsscip-go gomod github.com/golang/go/src go1.20 strings/.TrimSuffixscip-go gomod github.com/golang/go/src go1.20 strings/TrimSuffix().(filepathscip-go gomod github.com/golang/go/src go1.20 `path/filepath`/.Basescip-go gomod github.com/golang/go/src go1.20 `path/filepath`/Base().(configFile), ext), ext)
128 tmpFile, err := osscip-go gomod github.com/golang/go/src go1.20 os/.CreateTempscip-go gomod github.com/golang/go/src go1.20 os/CreateTemp().(filepathscip-go gomod github.com/golang/go/src go1.20 `path/filepath`/.Dirscip-go gomod github.com/golang/go/src go1.20 `path/filepath`/Dir().(configFile), pattern)
129 if err != nil {
130 return err
131 }
132 defer func() {
133 _ = osscip-go gomod github.com/golang/go/src go1.20 os/.Removescip-go gomod github.com/golang/go/src go1.20 os/Remove().(tmpFile.Namescip-go gomod github.com/golang/go/src go1.20 os/File#Name().())
134 }()
135 if err := tmpFile.Closescip-go gomod github.com/golang/go/src go1.20 os/File#Close().(); err != nil {
136 return err
137 }
138 if err := osscip-go gomod github.com/golang/go/src go1.20 os/.WriteFilescip-go gomod github.com/golang/go/src go1.20 os/WriteFile().(tmpFile.Namescip-go gomod github.com/golang/go/src go1.20 os/File#Name().(), bin, 0600); err != nil {
139 return err
140 }
141 return osscip-go gomod github.com/golang/go/src go1.20 os/.Renamescip-go gomod github.com/golang/go/src go1.20 os/Rename().(tmpFile.Namescip-go gomod github.com/golang/go/src go1.20 os/File#Name().(), configFile)
142 }