Compare commits

...

2 commits

Author SHA1 Message Date
13876be966
oizys refactoring 2024-07-12 10:23:25 -05:00
c3e90946f0
don't use action for ref checkout 2024-07-12 09:55:23 -05:00
6 changed files with 88 additions and 43 deletions

View file

@ -17,8 +17,7 @@ jobs:
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v4 uses: actions/checkout@v4
with: - run: git checkout -B flake-lock
ref: flake-lock
- name: Setup Git Bot - name: Setup Git Bot
run: | run: |
@ -40,7 +39,9 @@ jobs:
- name: Update nix flake - name: Update nix flake
run: | run: |
printf '# Flake Lock\n\n```txt\n' >> $GITHUB_STEP_SUMMARY
nix flake update --commit-lock-file 2>> $GITHUB_STEP_SUMMARY nix flake update --commit-lock-file 2>> $GITHUB_STEP_SUMMARY
printf '```\n' >> $GITHUB_STEP_SUMMARY
- name: Build - name: Build
run: | run: |
@ -63,9 +64,8 @@ jobs:
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v4 uses: actions/checkout@v4
with: - run: git checkout -B flake-lock
ref: flake-lock
# - name: Setup Git Bot # - name: Setup Git Bot
# run: | # run: |
# git --version # git --version

View file

@ -11,12 +11,12 @@ import (
"github.com/charmbracelet/log" "github.com/charmbracelet/log"
) )
func logCmd(cmd *exec.Cmd) { func LogCmd(cmd *exec.Cmd) {
log.Debugf("CMD: %s", strings.Join(cmd.Args, " ")) log.Debugf("CMD: %s", strings.Join(cmd.Args, " "))
} }
func cmdOutputWithSpinner(cmd *exec.Cmd, msg string, stderr bool) (output []byte, err error) { func CmdOutputWithSpinner(cmd *exec.Cmd, msg string, stderr bool) (output []byte, err error) {
logCmd(cmd) LogCmd(cmd)
s := startSpinner(msg) s := startSpinner(msg)
if stderr { if stderr {
output, err = cmd.CombinedOutput() output, err = cmd.CombinedOutput()
@ -38,8 +38,8 @@ func startSpinner(msg string) *spinner.Spinner {
return s return s
} }
func exitWithCommand(cmd *exec.Cmd) { func ExitWithCommand(cmd *exec.Cmd) {
logCmd(cmd) LogCmd(cmd)
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {

View file

@ -0,0 +1,42 @@
package oizys
import (
"fmt"
"os"
"os/exec"
"github.com/charmbracelet/log"
"oizys/internal/ui"
)
type GitRepo struct {
path string
}
func (g *GitRepo) git(rest ...string) *exec.Cmd {
args := []string{"-C", g.path}
args = append(args, rest...)
cmd := exec.Command("git", args...)
// logCmd(cmd)
return cmd
}
func GitPull(workDir string) {
g := GitRepo{workDir}
cmdOutput, err := g.git("status", "--porcelain").Output()
if err != nil {
log.Fatal(err)
}
if len(cmdOutput) > 0 {
fmt.Println("unstaged commits, cowardly exiting...")
ui.ShowFailedOutput(cmdOutput)
os.Exit(1)
}
cmdOutput, err = g.git("pull").CombinedOutput()
if err != nil {
ui.ShowFailedOutput(cmdOutput)
log.Fatal(err)
}
}

View file

@ -10,8 +10,10 @@ import (
"strings" "strings"
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log" "github.com/charmbracelet/log"
e "oizys/internal/exec"
"oizys/internal/ui"
) )
var o *Oizys var o *Oizys
@ -129,7 +131,7 @@ func git(rest ...string) *exec.Cmd {
args := []string{"-C", o.flake} args := []string{"-C", o.flake}
args = append(args, rest...) args = append(args, rest...)
cmd := exec.Command("git", args...) cmd := exec.Command("git", args...)
logCmd(cmd) e.LogCmd(cmd)
return cmd return cmd
} }
@ -141,18 +143,18 @@ func GitPull() {
if len(cmdOutput) > 0 { if len(cmdOutput) > 0 {
fmt.Println("unstaged commits, cowardly exiting...") fmt.Println("unstaged commits, cowardly exiting...")
showFailedOutput(cmdOutput) ui.ShowFailedOutput(cmdOutput)
os.Exit(1) os.Exit(1)
} }
cmdOutput, err = git("pull").CombinedOutput() cmdOutput, err = git("pull").CombinedOutput()
if err != nil { if err != nil {
showFailedOutput(cmdOutput) ui.ShowFailedOutput(cmdOutput)
log.Fatal(err) log.Fatal(err)
} }
} }
func parseDryRun(buf string) (*packages, *packages) { func parseDryRun(buf string) (*ui.Packages, *ui.Packages) {
lines := strings.Split(strings.TrimSpace(buf), "\n") lines := strings.Split(strings.TrimSpace(buf), "\n")
var parts [2][]string var parts [2][]string
i := 0 i := 0
@ -173,8 +175,8 @@ func parseDryRun(buf string) (*packages, *packages) {
os.Exit(0) os.Exit(0)
} }
return parsePackages(parts[0], "packages to build"), return ui.ParsePackages(parts[0], "packages to build"),
parsePackages(parts[1], "packages to fetch") ui.ParsePackages(parts[1], "packages to fetch")
} }
// TODO: Refactor this and above // TODO: Refactor this and above
@ -203,8 +205,8 @@ func parseDryRun2(buf string) ([]string, []string) {
// TODO: refactor to account for --debug and not --verbose? // TODO: refactor to account for --debug and not --verbose?
func showDryRunResult(nixOutput string, verbose bool) { func showDryRunResult(nixOutput string, verbose bool) {
toBuild, toFetch := parseDryRun(nixOutput) toBuild, toFetch := parseDryRun(nixOutput)
toFetch.show(o.debug) toFetch.Show(o.debug)
toBuild.show(true) toBuild.Show(true)
} }
func Dry(verbose bool, minimal bool, rest ...string) { func Dry(verbose bool, minimal bool, rest ...string) {
@ -229,7 +231,7 @@ func Dry(verbose bool, minimal bool, rest ...string) {
lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("6")).Render(o.host), lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("6")).Render(o.host),
) )
} }
result, err := cmdOutputWithSpinner(cmd, spinnerMsg, true) result, err := e.CmdOutputWithSpinner(cmd, spinnerMsg, true)
if err != nil { if err != nil {
log.Fatal("failed to dry-run nix build", "err", err, "output", string(result)) log.Fatal("failed to dry-run nix build", "err", err, "output", string(result))
} }
@ -255,7 +257,7 @@ func NixosRebuild(subcmd string, rest ...string) {
cmd.Args = append(cmd.Args, "--print-build-logs") cmd.Args = append(cmd.Args, "--print-build-logs")
} }
cmd.Args = append(cmd.Args, rest...) cmd.Args = append(cmd.Args, rest...)
exitWithCommand(cmd) e.ExitWithCommand(cmd)
} }
func NixBuild(nom bool, minimal bool, rest ...string) { func NixBuild(nom bool, minimal bool, rest ...string) {
@ -282,7 +284,7 @@ func NixBuild(nom bool, minimal bool, rest ...string) {
cmd.Args = append(cmd.Args, "--log-format", "multiline") cmd.Args = append(cmd.Args, "--log-format", "multiline")
} }
cmd.Args = append(cmd.Args, rest...) cmd.Args = append(cmd.Args, rest...)
exitWithCommand(cmd) e.ExitWithCommand(cmd)
} }
func (o *Oizys) writeToGithubStepSummary(txt string) { func (o *Oizys) writeToGithubStepSummary(txt string) {
@ -332,12 +334,12 @@ func CacheBuild(rest ...string) {
args = append(args, NixosConfigAttrs()...) args = append(args, NixosConfigAttrs()...)
args = append(args, rest...) args = append(args, rest...)
cmd := exec.Command("cachix", args...) cmd := exec.Command("cachix", args...)
exitWithCommand(cmd) e.ExitWithCommand(cmd)
} }
func CI(rest ...string) { func CI(rest ...string) {
args := []string{"workflow", "run", "build.yml", "-F", fmt.Sprintf("hosts=%s", o.host)} args := []string{"workflow", "run", "build.yml", "-F", fmt.Sprintf("hosts=%s", o.host)}
args = append(args, rest...) args = append(args, rest...)
cmd := exec.Command("gh", args...) cmd := exec.Command("gh", args...)
exitWithCommand(cmd) e.ExitWithCommand(cmd)
} }

View file

@ -8,6 +8,7 @@ import (
"strings" "strings"
"github.com/charmbracelet/log" "github.com/charmbracelet/log"
e "oizys/internal/exec"
) )
var ignoredMap = stringSliceToMap( var ignoredMap = stringSliceToMap(
@ -128,7 +129,7 @@ func toBuildNixosConfiguration() []string {
if o.resetCache { if o.resetCache {
systemCmd.Args = append(systemCmd.Args, "--narinfo-cache-negative-ttl", "0") systemCmd.Args = append(systemCmd.Args, "--narinfo-cache-negative-ttl", "0")
} }
result, err := cmdOutputWithSpinner( result, err := e.CmdOutputWithSpinner(
systemCmd, systemCmd,
fmt.Sprintf("running dry build for: %s", strings.Join(NixosConfigAttrs(), " ")), fmt.Sprintf("running dry build for: %s", strings.Join(NixosConfigAttrs(), " ")),
true, true,
@ -143,7 +144,7 @@ func toBuildNixosConfiguration() []string {
func evaluateDerivations(drvs ...string) map[string]Derivation { func evaluateDerivations(drvs ...string) map[string]Derivation {
cmd := exec.Command("nix", "derivation", "show", "-r") cmd := exec.Command("nix", "derivation", "show", "-r")
cmd.Args = append(cmd.Args, drvs...) cmd.Args = append(cmd.Args, drvs...)
out, err := cmdOutputWithSpinner(cmd, out, err := e.CmdOutputWithSpinner(cmd,
fmt.Sprintf("evaluating derivations %s", strings.Join(drvs, " ")), fmt.Sprintf("evaluating derivations %s", strings.Join(drvs, " ")),
false) false)
if err != nil { if err != nil {

View file

@ -1,4 +1,4 @@
package oizys package ui
import ( import (
"fmt" "fmt"
@ -11,6 +11,17 @@ import (
"golang.org/x/term" "golang.org/x/term"
) )
func ShowFailedOutput(buf []byte) {
arrow := lipgloss.
NewStyle().
Bold(true).
Foreground(lipgloss.Color("9")).
Render("->")
for _, line := range strings.Split(strings.TrimSpace(string(buf)), "\n") {
fmt.Println(arrow, line)
}
}
// TODO: seperate parsing and displaying of packages // TODO: seperate parsing and displaying of packages
func terminalSize() (int, int) { func terminalSize() (int, int) {
fd := os.Stdout.Fd() fd := os.Stdout.Fd()
@ -25,13 +36,13 @@ func terminalSize() (int, int) {
return w, h return w, h
} }
type packages struct { type Packages struct {
desc string desc string
names []string names []string
pad int pad int
} }
func parsePackages(lines []string, desc string) *packages { func ParsePackages(lines []string, desc string) *Packages {
w, _ := terminalSize() w, _ := terminalSize()
maxAcceptable := (w / 4) - 1 maxAcceptable := (w / 4) - 1
maxLen := 0 maxLen := 0
@ -48,7 +59,7 @@ func parsePackages(lines []string, desc string) *packages {
names[i] = name names[i] = name
} }
sort.Strings(names) sort.Strings(names)
return &packages{names: names, pad: maxLen + 1, desc: desc} return &Packages{names: names, pad: maxLen + 1, desc: desc}
} }
func ellipsis(s string, maxLen int) string { func ellipsis(s string, maxLen int) string {
@ -62,7 +73,7 @@ func ellipsis(s string, maxLen int) string {
return string(runes[0:maxLen-3]) + "..." return string(runes[0:maxLen-3]) + "..."
} }
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
@ -81,7 +92,7 @@ func (p *packages) show(verbose bool) {
fmt.Println() fmt.Println()
} }
func (p *packages) summary() { func (p *Packages) summary() {
fmt.Printf("%s: %s\n", fmt.Printf("%s: %s\n",
p.desc, p.desc,
lipgloss.NewStyle(). lipgloss.NewStyle().
@ -90,14 +101,3 @@ func (p *packages) summary() {
Render(fmt.Sprint(len(p.names))), Render(fmt.Sprint(len(p.names))),
) )
} }
func showFailedOutput(buf []byte) {
arrow := lipgloss.
NewStyle().
Bold(true).
Foreground(lipgloss.Color("9")).
Render("->")
for _, line := range strings.Split(strings.TrimSpace(string(buf)), "\n") {
fmt.Println(arrow, line)
}
}