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 (
|
2024-03-04 03:29:10 +00:00
|
|
|
"encoding/json"
|
2024-01-18 05:49:09 +00:00
|
|
|
"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 {
|
2024-03-04 03:29:10 +00:00
|
|
|
Triggers []Trigger `json:"triggers"`
|
|
|
|
Run string `json:"run"`
|
2024-01-18 05:49:09 +00:00
|
|
|
}
|
|
|
|
|
2024-03-04 03:29:10 +00:00
|
|
|
type Trigger struct {
|
|
|
|
// GitRev triggers once for each matching rev.
|
|
|
|
//
|
|
|
|
// For example, given the config
|
|
|
|
//
|
|
|
|
// rev="refs/tags/v*"
|
|
|
|
//
|
|
|
|
// and the refs
|
|
|
|
//
|
|
|
|
// "refs/tags/v1" => 0001
|
|
|
|
// "refs/tags/v2" => 0002
|
|
|
|
// "refs/tags/v3" => 0003
|
|
|
|
//
|
|
|
|
// it would trigger 3 times:
|
|
|
|
//
|
|
|
|
// {"git_rev":{"url":"…","rev":"refs/tags/v1"}} => 0001
|
|
|
|
// {"git_rev":{"url":"…","rev":"refs/tags/v2"}} => 0002
|
|
|
|
// {"git_rev":{"url":"…","rev":"refs/tags/v3"}} => 0003
|
|
|
|
GitRev *struct {
|
|
|
|
URL string `json:"url"`
|
|
|
|
Rev string `json:"rev"`
|
|
|
|
} `json:"git_rev,omitempty"`
|
|
|
|
|
|
|
|
// GitRevs triggers once with the list of all matching revs.
|
|
|
|
//
|
|
|
|
// For example, given the config
|
|
|
|
//
|
|
|
|
// revs="refs/tags/v*"
|
|
|
|
//
|
|
|
|
// and the refs
|
|
|
|
//
|
|
|
|
// "refs/tags/v1" => 0001
|
|
|
|
// "refs/tags/v2" => 0002
|
|
|
|
// "refs/tags/v3" => 0003
|
|
|
|
//
|
|
|
|
// it would trigger just once:
|
|
|
|
//
|
|
|
|
// {"git_revs:"{"url":"…","revs":"refs/tags/v*"}} => "refs/tags/v1\t0001\nrefs/tags/v2\t0002\nrefs/tags/v3\t0003"
|
|
|
|
GitRevs *struct {
|
|
|
|
URL string `json:"url"`
|
|
|
|
Revs string `json:"revs"`
|
|
|
|
} `json:"git_revs,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (trigger *Trigger) UnmarshalJSON(dat []byte) error {
|
|
|
|
type untypedTrigger Trigger
|
|
|
|
var obj untypedTrigger
|
|
|
|
if err := json.Unmarshal(dat, &obj); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cnt := 0
|
|
|
|
if obj.GitRev != nil {
|
|
|
|
cnt++
|
|
|
|
}
|
|
|
|
if obj.GitRevs != nil {
|
|
|
|
cnt++
|
|
|
|
}
|
|
|
|
if cnt != 1 {
|
|
|
|
return fmt.Errorf("trigger must have exactly 1 field set")
|
|
|
|
}
|
|
|
|
*trigger = Trigger(obj)
|
|
|
|
return nil
|
2024-01-18 05:49:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|