60 lines
1.4 KiB
Go
60 lines
1.4 KiB
Go
// Copyright (C) 2024 Umorpha Systems
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package leader
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"git.mothstuff.lol/lukeshu/eclipse/lib/common"
|
|
)
|
|
|
|
// Main config /////////////////////////////////////////////////////////////////
|
|
|
|
type Config struct {
|
|
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"`
|
|
}
|
|
|
|
func LoadConfig(filename string) (Config, error) {
|
|
return common.LoadYAML[Config](filename)
|
|
}
|
|
|
|
// 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()))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%q: %w", entry.Name(), err)
|
|
}
|
|
ret = append(ret, actions...)
|
|
}
|
|
return ret, nil
|
|
}
|