2024-01-18 05:49:09 +00:00
|
|
|
// Copyright (C) 2024 Umorpha Systems
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
2024-01-20 17:16:25 +00:00
|
|
|
package leader
|
2024-01-18 05:49:09 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2024-01-20 17:16:25 +00:00
|
|
|
"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"`
|
|
|
|
|
2024-01-20 17:16:25 +00:00
|
|
|
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) {
|
2024-01-20 17:16:25 +00:00
|
|
|
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
|
|
|
|
}
|
2024-01-20 17:16:25 +00:00
|
|
|
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
|
|
|
|
}
|