eclipse/lib/leader/config.go

62 lines
1.4 KiB
Go
Raw Permalink Normal View History

2024-01-18 05:49:09 +00:00
// Copyright (C) 2024 Umorpha Systems
// SPDX-License-Identifier: AGPL-3.0-or-later
package leader
2024-01-18 05:49:09 +00:00
import (
"fmt"
"os"
"path/filepath"
"strings"
"git.mothstuff.lol/lukeshu/eclipse/lib/common"
2024-01-18 05:49:09 +00:00
)
// Main config /////////////////////////////////////////////////////////////////
type Config struct {
2024-02-25 01:32:05 +00:00
PubKeyFile string `json:"pubkey_file"`
2024-01-18 05:49:09 +00:00
StateDir string `json:"state_dir"`
CacheDir string `json:"cache_dir"`
ActionsDir string `json:"actions_dir"`
GitCacheFor common.Duration `json:"cache_git_for"`
ParallelDownloads int `json:"parallel_downloads"`
2024-01-18 05:49:09 +00:00
}
func LoadConfig(filename string) (Config, error) {
return common.LoadYAML[Config](filename)
2024-01-18 05:49:09 +00:00
}
// Actions /////////////////////////////////////////////////////////////////////
type Action struct {
Triggers []GitTrigger `json:"triggers"`
Run string `json:"run"`
}
type GitTrigger struct {
RepoURL string `json:"repo_url"`
Rev string `json:"rev"`
}
func (cfg Config) Actions() ([]Action, error) {
entries, err := os.ReadDir(cfg.ActionsDir)
if err != nil {
return nil, err
}
var ret []Action
for _, entry := range entries {
if !strings.HasSuffix(entry.Name(), ".yml") && !strings.HasSuffix(entry.Name(), ".yaml") {
continue
}
actions, err := common.LoadYAML[[]Action](filepath.Join(cfg.ActionsDir, entry.Name()))
2024-01-18 05:49:09 +00:00
if err != nil {
return nil, fmt.Errorf("%q: %w", entry.Name(), err)
}
ret = append(ret, actions...)
}
return ret, nil
}