courgette/internal/actions/act.go

54 lines
1.3 KiB
Go

// Implementation for basic Action-related operations.
package actions
import (
logger "courgette/internal/logging"
"os"
"os/exec"
"strings"
)
type CliClient interface {
Clone(url string, destination string) error
Exec(args ...string) error
}
type GitClient struct{}
func (g GitClient) Exec(args ...string) error {
cmd := exec.Command("git", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func (g GitClient) Clone(url string, destination string) error {
return g.Exec("clone", url, destination, "--depth", "1")
}
// Returns a key that can be used as a directory / cache-key string
// based on the provided Action repository url.
func GetActionKey(url string) string {
url = strings.ToLower(url)
if strings.HasPrefix(url, "http://") {
url = strings.TrimPrefix(url, "http://")
} else if strings.HasPrefix(url, "https://") {
url = strings.TrimPrefix(url, "https://")
}
parts := strings.Split(url, "/")
return strings.Join(parts, "__")
}
// 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 PrefetchAction(client CliClient, actionName string, destination string) error {
logger.Info("Prefetching action: %s to %s", actionName, destination)
return client.Clone(actionName, destination)
}