Introduced basic structure

This commit is contained in:
Simon Martens
2024-11-10 00:58:23 +01:00
parent dafa217003
commit 65f8f0d8a1
8 changed files with 193 additions and 110 deletions

100
providers/config.go Normal file
View File

@@ -0,0 +1,100 @@
package providers
import (
"encoding/json"
"fmt"
"os"
"strings"
"githib.com/Theodor-Springmann-Stiftung/kgpz_web/helpers"
"github.com/kelseyhightower/envconfig"
)
// WARNING: this is not intended to be used in a multi-threaded environment
// Instatiate this once on startup before any goroutines are started
const (
DEFAULT_GIT_DIR = "data_git"
DEFAULT_GND_DIR = "cache_gnd"
DEFAULT_GEO_DIR = "cache_geo"
)
type ConfigProvider struct {
Files []string
*Config
}
type Config struct {
// At least one of these should be set
GitURL string `json:"git_url" envconfig:"GIT_URL"`
GitBranch string `json:"git_branch" envconfig:"GIT_BRANCH"`
FolderPath string `json:"folder_path" envconfig:"FOLDER_PATH"`
GNDPath string `json:"gnd_path" envconfig:"GND_PATH"`
GeoPath string `json:"geo_path" envconfig:"GEO_PATH"`
WebHookEndpoint string `json:"webhook_endpoint" envconfig:"WEBHOOK_ENDPOINT"`
WebHookSecret string `json:"webhook_secret" envconfig:"WEBHOOK_SECRET"`
Debug bool `json:"debug" envconfig:"DEBUG"`
}
func NewConfigProvider(files []string) *ConfigProvider {
return &ConfigProvider{Files: files}
}
func (c *ConfigProvider) Read() error {
c.Config = &Config{}
for _, file := range c.Files {
c.Config = readSettingsFile(c.Config, file)
}
c.Config = readSettingsEnv(c.Config)
c.Config = readDefaults(c.Config)
return nil
}
func (c *ConfigProvider) Validate() error {
if strings.TrimSpace(c.Config.FolderPath) == "" {
return fmt.Errorf("Folder path not set")
}
return nil
}
func readSettingsFile(cfg *Config, path string) *Config {
f, err := os.Open(path)
if err != nil {
fmt.Println("Error: ", err)
fmt.Println("Coudln't open ", path)
return cfg
}
defer f.Close()
dec := json.NewDecoder(f)
err = dec.Decode(cfg)
helpers.MaybePanic(err, "Error decoding config.json")
return cfg
}
func readSettingsEnv(cfg *Config) *Config {
_ = envconfig.Process("KGPZ", cfg)
return cfg
}
func readDefaults(cfg *Config) *Config {
if strings.TrimSpace(cfg.FolderPath) == "" {
cfg.FolderPath = DEFAULT_GIT_DIR
}
if strings.TrimSpace(cfg.GNDPath) == "" {
cfg.GNDPath = DEFAULT_GND_DIR
}
if strings.TrimSpace(cfg.GeoPath) == "" {
cfg.GeoPath = DEFAULT_GEO_DIR
}
return cfg
}
// Implement stringer
func (c *Config) String() string {
return fmt.Sprintf("GitURL: %s\nGitBranch: %s\nFolderPath: %s\nGNDPath: %s\nGeoPath: %s\nWebHookEndpoint: %s\nWebHookSecret: %s\n",
c.GitURL, c.GitBranch, c.FolderPath, c.GNDPath, c.GeoPath, c.WebHookEndpoint, c.WebHookSecret)
}

View File

@@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"os"
"sync"
"time"
"github.com/go-git/go-git/v5"
@@ -19,6 +20,8 @@ var InvalidStateError = errors.New("The GitProvider is not in a valid state. Fix
// In case of success in either case it updates the commit hash and date and closes the repo again.
// The Files are opened and serialized by the FSProvider, which operates on the same file path.
type GitProvider struct {
mu sync.Mutex
URL string
Path string
Branch string
@@ -34,6 +37,9 @@ func NewGitProvider(url string, path string, branch string) *GitProvider {
}
func (g *GitProvider) Pull() error {
g.mu.Lock()
defer g.mu.Unlock()
branch := plumbing.NewBranchReferenceName(g.Branch)
repo, err := git.PlainOpen(g.Path)
if err != nil {
@@ -65,6 +71,9 @@ func (g *GitProvider) Pull() error {
}
func (g *GitProvider) Clone() error {
g.mu.Lock()
defer g.mu.Unlock()
branch := plumbing.NewBranchReferenceName(g.Branch)
repo, err := git.PlainClone(g.Path, false, &git.CloneOptions{
@@ -118,7 +127,11 @@ func (g *GitProvider) setValues(repo *git.Repository) error {
return err
}
// WARNING: this expects the repo to be in a certain state and is intended to be used in tests.
func (g *GitProvider) Read() error {
g.mu.Lock()
defer g.mu.Unlock()
repo, err := git.PlainOpen(g.Path)
if err != nil {
return err
@@ -138,6 +151,9 @@ func (g *GitProvider) Read() error {
}
func (g *GitProvider) Validate() error {
g.mu.Lock()
defer g.mu.Unlock()
if g.Commit == "" || g.Date.IsZero() {
return InvalidStateError
}