refactor: change configruation to yml

This commit is contained in:
2024-04-29 21:50:16 +02:00
parent 727b083e52
commit 5860f81aa2
13 changed files with 63 additions and 65 deletions

View File

@@ -1,45 +0,0 @@
package cfg
import (
"log"
"os/exec"
"strings"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
)
type Config struct {
ScriptBinaryPath string `required:"true" split_words:"true"`
WebhookScriptPath string `required:"true" split_words:"true"`
FileToWatchPath string `required:"true" split_words:"true"`
TestFileToWatchPath string `required:"false" split_words:"true"`
}
func RootDir() string {
cmdOut, err := exec.Command("git", "rev-parse", "--show-toplevel").Output()
if err != nil {
log.Fatalf("exec.Command: %s", err)
return ""
}
rootDir := strings.TrimSpace(string(cmdOut))
return rootDir
}
func NewConfig(envFilePath string) *Config {
if envFilePath != "" {
err := godotenv.Load(envFilePath)
if err != nil {
log.Fatalf("environment variable ENV is empty and an error occurred while loading the .env file\n")
}
}
cfg := &Config{}
err := envconfig.Process("", cfg)
if err != nil {
log.Fatalf("envconfig.Process: %s\n", err)
}
return cfg
}

27
kit/config/config.go Normal file
View File

@@ -0,0 +1,27 @@
package config
import (
"os"
"gopkg.in/yaml.v2"
)
type Config struct {
ScriptBinaryPath string `yaml:"script_binary_path"`
WebhookScriptPath string `yaml:"webhook_script_path"`
FileToWatchPath string `yaml:"file_to_watch_path"`
}
func LoadConfig(path string) (*Config, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var config Config
if err := yaml.Unmarshal(data, &config); err != nil {
return nil, err
}
return &config, nil
}