Skip to content

Commit

Permalink
chore: move toml function to config package and improve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sacha-c committed Dec 18, 2024
1 parent a53f9d5 commit de9ea78
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
9 changes: 4 additions & 5 deletions internal/config/patrol.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"net/url"
"sheriff/internal/toml"

zerolog "github.com/rs/zerolog/log"
)
Expand Down Expand Up @@ -49,14 +48,14 @@ type PatrolCommonOpts struct {
Report PatrolReportOpts `toml:"report"`
}

// Options only available from CLI configuration
// PatrolCLIOpts are the options only available from CLI configuration
type PatrolCLIOpts struct {
Config string
Verbose bool
PatrolCommonOpts
}

// Options only available from File configuration
// PatrolFileOpts are the options only available from File configuration
type PatrolFileOpts struct {
PatrolCommonOpts
}
Expand All @@ -65,7 +64,7 @@ func GetPatrolConfiguration(cliOpts PatrolCLIOpts) (config PatrolConfig, err err
zerolog.Debug().Interface("cli options", cliOpts).Msg("Running with cli options")
var tomlOpts PatrolFileOpts
if cliOpts.Config != "" {
found, err := toml.GetFile(cliOpts.Config, &tomlOpts)
found, err := getTOMLFile(cliOpts.Config, &tomlOpts)
if !found {
return config, fmt.Errorf("failed to find configuration file %v", cliOpts.Config)
} else if err != nil {
Expand Down Expand Up @@ -105,7 +104,7 @@ func mergeConfigs(cliOpts PatrolCLIOpts, fileOpts PatrolFileOpts) (config Patrol
return
}

// Returns valueA if != nil, otherwise valueB if != nil, otherwise the provided default value
// getCliOrFileOption returns valueA if != nil, otherwise valueB if != nil, otherwise the provided default value
func getCliOrFileOption[T interface{}](valueA *T, valueB *T, def T) (r T) {
if valueA != nil {
return *valueA
Expand Down
4 changes: 1 addition & 3 deletions internal/config/project.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package config

import (
"sheriff/internal/toml"

"path"

"github.com/rs/zerolog/log"
Expand All @@ -22,7 +20,7 @@ type ProjectConfig struct {
}

func GetProjectConfiguration(projectName string, dir string) (config ProjectConfig) {
found, err := toml.GetFile(path.Join(dir, projectConfigFileName), &config)
found, err := getTOMLFile(path.Join(dir, projectConfigFileName), &config)
if err != nil {
log.Error().Err(err).Str("project", projectName).Msg("Failed to read project configuration. Running with empty configuration.")
} else if found {
Expand Down
6 changes: 3 additions & 3 deletions internal/toml/parse.go → internal/config/toml.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package toml
package config

import (
"errors"
Expand All @@ -9,8 +9,8 @@ import (
"github.com/rs/zerolog/log"
)

// Parses and sets passed config pointer by value
func GetFile[T interface{}](filename string, config *T) (found bool, err error) {
// getTOMLFile parses and sets passed config pointer by value
func getTOMLFile[T interface{}](filename string, config *T) (found bool, err error) {
if _, err := os.Stat(filename); os.IsNotExist(err) {
return false, nil
} else if err != nil {
Expand Down

0 comments on commit de9ea78

Please sign in to comment.