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 package cmd
import ( import (
"oizys/internal/oizys" "oizys/internal/github"
"os"
"github.com/charmbracelet/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -12,11 +14,28 @@ var ciCmd = &cobra.Command{
Use: "ci", Use: "ci",
Short: "offload build to GHA", Short: "offload build to GHA",
Run: func(cmd *cobra.Command, args []string) { 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() { func init() {
rootCmd.AddCommand(ciCmd) 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 the markdown content as string
return string(content), nil 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" "fmt"
"io/fs" "io/fs"
"oizys/internal/git" "oizys/internal/git"
// "oizys/internal/github"
"oizys/internal/ui" "oizys/internal/ui"
"os" "os"
"os/exec" "os/exec"
@ -316,16 +317,6 @@ func CacheBuild(rest ...string) {
e.ExitWithCommand(cmd) 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() { func UpdateRepo() {
log.Info("rebasing HEAD on origin/flake-lock") log.Info("rebasing HEAD on origin/flake-lock")
o.repo.Fetch() o.repo.Fetch()