add ci command to oizys-cli

This commit is contained in:
Daylin Morgan 2024-05-24 11:50:18 -05:00
parent 7ff50c1310
commit c58213dfb5
Signed by: daylin
GPG key ID: 950D13E9719334AD
4 changed files with 53 additions and 16 deletions

View file

@ -0,0 +1,20 @@
package cmd
import (
"github.com/spf13/cobra"
)
// gh workflow run build.yml -F lockFile=@flake.lock
var ciCmd= &cobra.Command{
Use: "ci",
Short: "offload build to GHA",
Run: func(cmd *cobra.Command, args []string) {
oizys.CI(args...)
},
}
func init() {
rootCmd.AddCommand(ciCmd)
}

View file

@ -3,9 +3,10 @@ package cmd
import ( import (
"os" "os"
o "oizys/pkg"
cc "github.com/ivanpirog/coloredcobra" cc "github.com/ivanpirog/coloredcobra"
"github.com/spf13/cobra" "github.com/spf13/cobra"
o "oizys/pkg"
) )
func Execute() { func Execute() {

View file

@ -1,7 +1,9 @@
{ {
lib,
installShellFiles, installShellFiles,
buildGoModule, buildGoModule,
lib, makeWrapper,
gh,
... ...
}: }:
buildGoModule { buildGoModule {
@ -11,10 +13,18 @@ buildGoModule {
src = lib.cleanSource ./.; src = lib.cleanSource ./.;
vendorHash = "sha256-NCHU491j6fRfSk6LA9tS9yiuT/gZhPic46mNTVf1Jeg="; vendorHash = "sha256-NCHU491j6fRfSk6LA9tS9yiuT/gZhPic46mNTVf1Jeg=";
nativeBuildInputs = [ installShellFiles ]; nativeBuildInputs = [
installShellFiles
makeWrapper
];
postInstall = '' postInstall = ''
installShellCompletion --cmd oizys \ installShellCompletion --cmd oizys \
--zsh <($out/bin/oizys completion zsh) --zsh <(OIZYS_SKIP_CHECK=true $out/bin/oizys completion zsh)
'';
postFixup = ''
wrapProgram $out/bin/oizys \
--prefix PATH ${lib.makeBinPath [ gh ]}
''; '';
} }

View file

@ -8,12 +8,11 @@ import (
"os" "os"
"os/exec" "os/exec"
"strings" "strings"
"time"
"github.com/muesli/termenv" "github.com/muesli/termenv"
"golang.org/x/term" "golang.org/x/term"
"time"
"github.com/briandowns/spinner" "github.com/briandowns/spinner"
) )
@ -52,7 +51,7 @@ func (o *Oizys) Output() string {
) )
} }
func (o *Oizys) Set ( func (o *Oizys) Set(
flake, host, cache string, flake, host, cache string,
verbose bool, verbose bool,
) { ) {
@ -118,7 +117,7 @@ func ellipsis(s string, maxLen int) string {
func (p *packages) show(verbose bool) { func (p *packages) show(verbose bool) {
p.summary() p.summary()
if !verbose || (len(p.names) == 0) { if !verbose || (len(p.names) == 0) {
return return
} }
@ -159,7 +158,6 @@ func showFailedOutput(buf []byte) {
} }
func (o *Oizys) GitPull() { func (o *Oizys) GitPull() {
cmdOutput, err := o.git("status", "--porcelain").Output() cmdOutput, err := o.git("status", "--porcelain").Output()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -171,14 +169,13 @@ func (o *Oizys) GitPull() {
os.Exit(1) os.Exit(1)
} }
cmdOutput, err = o.git("pull").CombinedOutput() cmdOutput, err = o.git("pull").CombinedOutput()
if err != nil { if err != nil {
showFailedOutput(cmdOutput) showFailedOutput(cmdOutput)
log.Fatal(err) log.Fatal(err)
} }
} }
func parseDryRun(buf string) (*packages, *packages) { func parseDryRun(buf string) (*packages, *packages) {
lines := strings.Split(strings.TrimSpace(buf), "\n") lines := strings.Split(strings.TrimSpace(buf), "\n")
var parts [2][]string var parts [2][]string
@ -232,10 +229,10 @@ func (o *Oizys) NixosRebuild(subcmd string, rest ...string) {
o.flake, o.flake,
} }
args = append(args, rest...) args = append(args, rest...)
if o.verbose { if o.verbose {
args = append(args, "--print-build-logs") args = append(args, "--print-build-logs")
fmt.Println("CMD:", "sudo", strings.Join(args, " ")) fmt.Println("CMD:", "sudo", strings.Join(args, " "))
} }
cmd := exec.Command("sudo", args...) cmd := exec.Command("sudo", args...)
runCommand(cmd) runCommand(cmd)
} }
@ -267,11 +264,20 @@ func (o *Oizys) CacheBuild(rest ...string) {
} }
func (o *Oizys) CheckFlake() { func (o *Oizys) CheckFlake() {
if _, err := os.Stat(o.flake); errors.Is(err, fs.ErrNotExist) { if _, ok := os.LookupEnv("OIZYS_SKIP_CHECK"); !ok {
log.Fatalln("path to flake:", o.flake, "does not exist") if _, err := os.Stat(o.flake); errors.Is(err, fs.ErrNotExist) {
log.Fatalln("path to flake:", o.flake, "does not exist")
}
} }
} }
func (o *Oizys) CI(rest ...string) {
args := []string{"workflow", "run", "build.yml", "-F", fmt.Sprintf("host=%s", o.host)}
args = append(args, rest...)
cmd := exec.Command("gh", args...)
runCommand(cmd)
}
func Output(flake string, host string) string { func Output(flake string, host string) string {
return fmt.Sprintf( return fmt.Sprintf(
"%s#nixosConfigurations.%s.config.system.build.toplevel", "%s#nixosConfigurations.%s.config.system.build.toplevel",