mirror of
https://github.com/ivuorinen/gibidify.git
synced 2026-02-24 02:53:11 +00:00
feat: update go to 1.25, add permissions and envs (#49)
* chore(ci): update go to 1.25, add permissions and envs * fix(ci): update pr-lint.yml * chore: update go, fix linting * fix: tests and linting * fix(lint): lint fixes, renovate should now pass * fix: updates, security upgrades * chore: workflow updates, lint * fix: more lint, checkmake, and other fixes * fix: more lint, convert scripts to POSIX compliant * fix: simplify codeql workflow * tests: increase test coverage, fix found issues * fix(lint): editorconfig checking, add to linters * fix(lint): shellcheck, add to linters * fix(lint): apply cr comment suggestions * fix(ci): remove step-security/harden-runner * fix(lint): remove duplication, apply cr fixes * fix(ci): tests in CI/CD pipeline * chore(lint): deduplication of strings * fix(lint): apply cr comment suggestions * fix(ci): actionlint * fix(lint): apply cr comment suggestions * chore: lint, add deps management
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"github.com/ivuorinen/gibidify/utils"
|
||||
"github.com/ivuorinen/gibidify/gibidiutils"
|
||||
)
|
||||
|
||||
// LoadConfig reads configuration from a YAML file.
|
||||
@@ -15,13 +17,18 @@ import (
|
||||
// 1. $XDG_CONFIG_HOME/gibidify/config.yaml
|
||||
// 2. $HOME/.config/gibidify/config.yaml
|
||||
// 3. The current directory as fallback.
|
||||
//
|
||||
// Note: LoadConfig relies on isRunningTest() which requires the testing package
|
||||
// to have registered its flags (e.g., via flag.Parse() or during test initialization).
|
||||
// If called too early (e.g., from init() or before TestMain), test detection may not work reliably.
|
||||
// For explicit control, use SetRunningInTest() before calling LoadConfig.
|
||||
func LoadConfig() {
|
||||
viper.SetConfigName("config")
|
||||
viper.SetConfigType("yaml")
|
||||
|
||||
if xdgConfig := os.Getenv("XDG_CONFIG_HOME"); xdgConfig != "" {
|
||||
// Validate XDG_CONFIG_HOME for path traversal attempts
|
||||
if err := utils.ValidateConfigPath(xdgConfig); err != nil {
|
||||
if err := gibidiutils.ValidateConfigPath(xdgConfig); err != nil {
|
||||
logrus.Warnf("Invalid XDG_CONFIG_HOME path, using default config: %v", err)
|
||||
} else {
|
||||
configPath := filepath.Join(xdgConfig, "gibidify")
|
||||
@@ -37,7 +44,14 @@ func LoadConfig() {
|
||||
}
|
||||
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
logrus.Infof("Config file not found, using default values: %v", err)
|
||||
// Suppress this info-level log when running tests.
|
||||
// Prefer an explicit test flag (SetRunningInTest) but fall back to runtime detection.
|
||||
if runningInTest.Load() || isRunningTest() {
|
||||
// Keep a debug-level record so tests that enable debug can still see it.
|
||||
logrus.Debugf("Config file not found (tests): %v", err)
|
||||
} else {
|
||||
logrus.Infof("Config file not found, using default values: %v", err)
|
||||
}
|
||||
setDefaultConfig()
|
||||
} else {
|
||||
logrus.Infof("Using config file: %s", viper.ConfigFileUsed())
|
||||
@@ -87,4 +101,31 @@ func setDefaultConfig() {
|
||||
viper.SetDefault("resourceLimits.hardMemoryLimitMB", DefaultHardMemoryLimitMB)
|
||||
viper.SetDefault("resourceLimits.enableGracefulDegradation", true)
|
||||
viper.SetDefault("resourceLimits.enableResourceMonitoring", true)
|
||||
}
|
||||
}
|
||||
|
||||
var runningInTest atomic.Bool
|
||||
|
||||
// SetRunningInTest allows tests to explicitly indicate they are running under `go test`.
|
||||
// Call this from TestMain in tests to suppress noisy info logs while still allowing
|
||||
// debug-level output for tests that enable it.
|
||||
func SetRunningInTest(b bool) {
|
||||
runningInTest.Store(b)
|
||||
}
|
||||
|
||||
// isRunningTest attempts to detect if the binary is running under `go test`.
|
||||
// Prefer checking for standard test flags registered by the testing package.
|
||||
// This is reliable when `go test` initializes the flag set.
|
||||
//
|
||||
// IMPORTANT: This function relies on flag.Lookup which returns nil if the testing
|
||||
// package hasn't registered test flags yet. Callers must invoke this after flag
|
||||
// parsing (or test flag registration) has occurred. If invoked too early (e.g.,
|
||||
// from init() or early in TestMain before flags are parsed), detection will fail.
|
||||
// For explicit control, use SetRunningInTest() instead.
|
||||
func isRunningTest() bool {
|
||||
// Look for the well-known test flags created by the testing package.
|
||||
// If any are present in the flag registry, we're running under `go test`.
|
||||
if flag.Lookup("test.v") != nil || flag.Lookup("test.run") != nil || flag.Lookup("test.bench") != nil {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user