-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created a more generic way to read configuration files
- Loading branch information
Showing
2 changed files
with
62 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
"github.com/spf13/viper" | ||
"log" | ||
) | ||
|
||
type FileReader[T any] struct { | ||
} | ||
|
||
func (reader *FileReader[T]) GetContent(filePath string) *T { | ||
fileReader, err := reader.LoadFileContent(filePath, "yml") | ||
if err != nil { | ||
log.Fatalf("Error while loading file content: %v", err) | ||
} | ||
|
||
fileContent, err := reader.ParseFileContent(fileReader) | ||
if err != nil { | ||
log.Fatalf("Error while parsing file content: %v", err) | ||
} | ||
|
||
return fileContent | ||
} | ||
|
||
func (reader *FileReader[T]) ParseFileContent(fileReader *viper.Viper) (*T, error) { | ||
var fileContent T | ||
err := fileReader.Unmarshal(&fileContent) | ||
if err != nil { | ||
log.Printf("Unable to parse file: %v", err) | ||
return nil, err | ||
} | ||
return &fileContent, nil | ||
} | ||
|
||
func (reader *FileReader[T]) LoadFileContent(fileName string, fileType string) (*viper.Viper, error) { | ||
fileReader := viper.New() | ||
fileReader.SetConfigType(fileType) | ||
fileReader.SetConfigName(fileName) | ||
fileReader.AddConfigPath(".") | ||
fileReader.AutomaticEnv() | ||
|
||
err := fileReader.ReadInConfig() | ||
if err != nil { | ||
log.Printf("Unable to read file content: %v", err) | ||
var fileNotFoundError viper.ConfigFileNotFoundError | ||
if errors.As(err, &fileNotFoundError) { | ||
return nil, errors.New("could not find file") | ||
} | ||
return nil, err | ||
} | ||
return fileReader, nil | ||
} |