MCPcopy
hub / github.com/gitleaks/gitleaks / initConfig

Function initConfig

cmd/root.go:132–195  ·  view source on GitHub ↗
(source string)

Source from the content-addressed store, hash-verified

130}
131
132func initConfig(source string) {
133 hideBanner, err := rootCmd.Flags().GetBool("no-banner")
134 viper.SetConfigType("toml")
135
136 if err != nil {
137 logging.Fatal().Msg(err.Error())
138 }
139 if !hideBanner {
140 _, _ = fmt.Fprint(os.Stderr, banner)
141 }
142
143 logging.Debug().Msgf("using %s regex engine", regexp.Version)
144
145 cfgPath, err := rootCmd.Flags().GetString("config")
146 if err != nil {
147 logging.Fatal().Msg(err.Error())
148 }
149 if cfgPath != "" {
150 viper.SetConfigFile(cfgPath)
151 logging.Debug().Msgf("using gitleaks config %s from `--config`", cfgPath)
152 } else if os.Getenv("GITLEAKS_CONFIG") != "" {
153 envPath := os.Getenv("GITLEAKS_CONFIG")
154 viper.SetConfigFile(envPath)
155 logging.Debug().Msgf("using gitleaks config from GITLEAKS_CONFIG env var: %s", envPath)
156 } else if os.Getenv("GITLEAKS_CONFIG_TOML") != "" {
157 configContent := []byte(os.Getenv("GITLEAKS_CONFIG_TOML"))
158 if err := viper.ReadConfig(bytes.NewBuffer(configContent)); err != nil {
159 logging.Fatal().Err(err).Str("content", os.Getenv("GITLEAKS_CONFIG_TOML")).Msg("unable to load gitleaks config from GITLEAKS_CONFIG_TOML env var")
160 }
161 logging.Debug().Str("content", os.Getenv("GITLEAKS_CONFIG_TOML")).Msg("using gitleaks config from GITLEAKS_CONFIG_TOML env var content")
162 return
163 } else {
164 fileInfo, err := os.Stat(source)
165 if err != nil {
166 logging.Fatal().Msg(err.Error())
167 }
168
169 if !fileInfo.IsDir() {
170 logging.Debug().Msgf("unable to load gitleaks config from %s since --source=%s is a file, using default config",
171 filepath.Join(source, ".gitleaks.toml"), source)
172 if err = viper.ReadConfig(strings.NewReader(config.DefaultConfig)); err != nil {
173 logging.Fatal().Msgf("err reading toml %s", err.Error())
174 }
175 return
176 }
177
178 if _, err := os.Stat(filepath.Join(source, ".gitleaks.toml")); os.IsNotExist(err) {
179 logging.Debug().Msgf("no gitleaks config found in path %s, using default gitleaks config", filepath.Join(source, ".gitleaks.toml"))
180
181 if err = viper.ReadConfig(strings.NewReader(config.DefaultConfig)); err != nil {
182 logging.Fatal().Msgf("err reading default config toml %s", err.Error())
183 }
184 return
185 } else {
186 logging.Debug().Msgf("using existing gitleaks config %s from `(--source)/.gitleaks.toml`", filepath.Join(source, ".gitleaks.toml"))
187 }
188
189 viper.AddConfigPath(source)

Callers 5

runStdInFunction · 0.85
runDetectFunction · 0.85
runGitFunction · 0.85
runProtectFunction · 0.85
runDirectoryFunction · 0.85

Calls 2

FatalFunction · 0.92
DebugFunction · 0.92

Tested by

no test coverage detected