courgette/internal/actions/manager.go

57 lines
1.5 KiB
Go
Raw Permalink Normal View History

package actions
import (
driver "courgette/internal/driver"
logger "courgette/internal/logging"
"errors"
"fmt"
"path/filepath"
)
type ActionsManager struct {
git CliClient
// Action cache location.
cacheRoot string
}
2024-09-03 02:55:29 +00:00
func NewActionsManager(cacheRoot string) *ActionsManager {
return &ActionsManager{
git: GitClient{},
cacheRoot: cacheRoot,
}
}
// Prefetches and caches the action defined by <actionName> at <destination>.
//
// <actionName> is expected to be the full url of the repository where
// the action lives.
func (a ActionsManager) PrefetchAction(actionName string) error {
destination := filepath.Join(a.cacheRoot, GetActionKey(actionName))
logger.Info("Prefetching action: %s to %s", actionName, destination)
return a.git.Clone(actionName, destination)
}
func (a ActionsManager) UseAction(actionName string, containerId string, options driver.CommandOptions) error {
actionKey := GetActionKey(actionName)
hostActionRoot := filepath.Join(a.cacheRoot, actionKey)
actionDefPath := filepath.Join(hostActionRoot, "action.yml")
actionDef, _ := GetDefinitionFromFile(actionDefPath)
containerActionRoot := filepath.Join("/cache", actionKey)
actionMain := filepath.Join(containerActionRoot, actionDef.Runs.Main)
command := fmt.Sprintf("node %s", actionMain)
result := driver.PodmanDriver{}.Exec(containerId, command, options)
if result.Error != nil {
return result.Error
}
if result.ExitCode != 0 {
return errors.New("Action used returned non-zero exit code.")
}
return nil
}