diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-13 10:01:59 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-13 10:01:59 +0200 |
| commit | 44a426c883a2c448d40a19903c822d03e5cf70af (patch) | |
| tree | dbb1fe11db1993c2cac7daf1c9930e37f44b38d1 /internal/config | |
| parent | cfddc5696f4956081630e3d394ef3d8c652af02e (diff) | |
chore: complete code quality audit fixesv1.2.6
- Fixed failing test in config_test.go (hardcoded date)
- Addressed unchecked error returns from Close() operations
- Refactored large functions to follow SRP (run.go and main.go)
- Added documentation to exported identifiers
- Fixed linting errors (error message capitalization, errcheck)
- Bumped version to v1.2.6
Diffstat (limited to 'internal/config')
| -rw-r--r-- | internal/config/config.go | 17 | ||||
| -rw-r--r-- | internal/config/config_test.go | 10 |
2 files changed, 20 insertions, 7 deletions
diff --git a/internal/config/config.go b/internal/config/config.go index 8b101be..73dae9f 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -11,7 +11,8 @@ import ( "codeberg.org/snonux/gos/internal/colour" ) -// The config file containing all the secrets and credentials plus maybe more. +// Config holds the application configuration loaded from the JSON config file. +// It contains secrets, credentials, and various settings for the application. type Config struct { LastRunEpoch int64 `json:"LastRunEpoch,omitempty"` MastodonURL string @@ -48,7 +49,12 @@ func New(configPath string, composeEntry bool) (Config, error) { if err != nil { return conf, fmt.Errorf("failed to open file: %w", err) } - defer file.Close() + defer func() { + if err := file.Close(); err != nil { + // Log the error but don't fail the operation since we've already written the data + colour.Errorln("Error closing file:", err) + } + }() bytes, err := io.ReadAll(file) if err != nil { @@ -77,7 +83,12 @@ func (s Config) WriteToDisk(configPath string) error { if err != nil { return fmt.Errorf("failed to create file: %w", err) } - defer file.Close() + defer func() { + if err := file.Close(); err != nil { + // Log the error but don't fail the operation since we've already written the data + colour.Errorln("Error closing file:", err) + } + }() if _, err := file.Write(bytes); err != nil { return fmt.Errorf("failed to write to file: %w", err) diff --git a/internal/config/config_test.go b/internal/config/config_test.go index adecc18..65e7767 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -1,6 +1,7 @@ package config import ( + "fmt" "testing" "time" ) @@ -159,9 +160,10 @@ func isPausedAtTime(c Config, testTime time.Time) (bool, error) { func TestIsPausedCurrentTime(t *testing.T) { // Test with actual current time using the real IsPaused method + currentYear := time.Now().Year() config := Config{ - PauseStart: "2025-01-01", - PauseEnd: "2025-12-31", + PauseStart: fmt.Sprintf("%d-01-01", currentYear), + PauseEnd: fmt.Sprintf("%d-12-31", currentYear), } paused, err := config.IsPaused() @@ -169,9 +171,9 @@ func TestIsPausedCurrentTime(t *testing.T) { t.Errorf("Unexpected error: %v", err) } - // Since we're in 2025, this should be paused + // Since we're in the current year, this should be paused if !paused { - t.Errorf("Expected to be paused in 2025, but got false") + t.Errorf("Expected to be paused in %d, but got false", currentYear) } // Test with dates in the past |
