(source string)
| 130 | } |
| 131 | |
| 132 | func 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) |
no test coverage detected