Skip to content
quarry Artifact [abb9c2573b]
Login

Artifact [abb9c2573b]

Artifact abb9c2573bfab73095394d1d7535ae7c57cb32a1fe676d3996b344e2495afb5a:


package runner

import (
	"context"
	"net/http"
	"os"
	"path/filepath"
	"runtime"
	"time"

	"craftdesign.group/quarry/internal/runner/install"
)

// setupCommand says whether args open with install, doctor or
// uninstall. A URL or a flag never does, so a unit written before these
// existed runs the runner as it always did.
func setupCommand(args []string) (string, bool) {
	if len(args) == 0 {
		return "", false
	}
	switch args[0] {
	case "install", "doctor", "uninstall":
		return args[0], true
	}
	return "", false
}

// runSetup runs one of the three on this machine.
func runSetup(name string, args []string, version string) error {
	e := install.Env{
		Sys:     install.HostSystem{},
		HTTP:    &http.Client{Timeout: 30 * time.Second},
		Out:     os.Stdout,
		Getenv:  os.Getenv,
		Prompt:  install.PromptSecret,
		Version: version,
		GOOS:    runtime.GOOS,
		Trunk:   trunkHasCI,
	}
	ctx := context.Background()
	switch name {
	case "install":
		return install.Install(ctx, e, args)
	case "doctor":
		return install.Doctor(ctx, e, args)
	}
	return install.Uninstall(ctx, e, args)
}

// trunkHasCI is whether the repository's trunk has a workflow in ci/ or
// a ci/runner.json: something for a runner to run.
func trunkHasCI(ctx context.Context, repoURL string) (bool, error) {
	dir, err := os.MkdirTemp("", "quarry-doctor-")
	if err != nil {
		return false, err
	}
	defer os.RemoveAll(dir)
	if err := (ArchiveCheckout{URL: repoURL}).Checkout(ctx, dir, "trunk"); err != nil {
		return false, err
	}
	if _, err := os.Stat(filepath.Join(dir, ManifestPath)); err == nil {
		return true, nil
	}
	es, err := os.ReadDir(filepath.Join(dir, "ci"))
	return err == nil && len(es) > 0, nil
}