remove gh call in favor of client api

This commit is contained in:
Daylin Morgan 2024-07-17 11:46:58 -05:00
parent ff6074ef4b
commit 34773c08f3
Signed by: daylin
GPG key ID: 950D13E9719334AD
3 changed files with 47 additions and 13 deletions

View file

@ -1,8 +1,10 @@
package cmd
import (
"oizys/internal/oizys"
"oizys/internal/github"
"os"
"github.com/charmbracelet/log"
"github.com/spf13/cobra"
)
@ -12,11 +14,28 @@ var ciCmd = &cobra.Command{
Use: "ci",
Short: "offload build to GHA",
Run: func(cmd *cobra.Command, args []string) {
oizys.CI(args...)
inputs := make(map[string]interface{})
if includeLock {
log.Debug("including lock file in inputs")
inputs["lockFile"] = readLockFile()
}
github.CreateDispatch("build.yml", ref, inputs)
},
}
var includeLock bool
var ref string
func init() {
rootCmd.AddCommand(ciCmd)
ciCmd.Flags().BoolVar(&includeLock, "lockfile", false, "include lock file in inputs")
ciCmd.Flags().StringVar(&ref, "ref", "main", "git ref to trigger workflow on")
}
func readLockFile() string {
dat, err := os.ReadFile("flake.lock")
if err != nil {
log.Fatal("failed to read flake.lock", "err", err)
}
return string(dat)
}

View file

@ -152,3 +152,27 @@ func ReadMarkdownFromZip(zipData []byte, fileName string) (string, error) {
// Return the markdown content as string
return string(content), nil
}
// func CI(rest ...string) {
// args := []string{
// "workflow", "run", "build.yml",
// "-F", fmt.Sprintf("hosts=%s", o.host),
// }
// args = append(args, rest...)
// cmd := exec.Command("gh", args...)
// e.ExitWithCommand(cmd)
// }
func CreateDispatch(workflowFileName string, ref string, inputs map[string]interface{}) {
event := github.CreateWorkflowDispatchEventRequest{Ref: ref, Inputs: inputs}
_, err := client.Actions.CreateWorkflowDispatchEventByFileName(
context.Background(),
"daylinmorgan",
"oizys",
workflowFileName,
event,
)
if err != nil {
log.Fatal("failed to dispatch event", "filename", workflowFileName, "err", err)
}
}

View file

@ -6,6 +6,7 @@ import (
"fmt"
"io/fs"
"oizys/internal/git"
// "oizys/internal/github"
"oizys/internal/ui"
"os"
"os/exec"
@ -316,16 +317,6 @@ func CacheBuild(rest ...string) {
e.ExitWithCommand(cmd)
}
func CI(rest ...string) {
args := []string{
"workflow", "run", "build.yml",
"-F", fmt.Sprintf("hosts=%s", o.host),
}
args = append(args, rest...)
cmd := exec.Command("gh", args...)
e.ExitWithCommand(cmd)
}
func UpdateRepo() {
log.Info("rebasing HEAD on origin/flake-lock")
o.repo.Fetch()