oizys refactoring

This commit is contained in:
Daylin Morgan 2024-07-12 10:19:07 -05:00
parent c3e90946f0
commit 13876be966
Signed by: daylin
GPG key ID: 950D13E9719334AD
5 changed files with 83 additions and 38 deletions

View file

@ -11,12 +11,12 @@ import (
"github.com/charmbracelet/log"
)
func logCmd(cmd *exec.Cmd) {
func LogCmd(cmd *exec.Cmd) {
log.Debugf("CMD: %s", strings.Join(cmd.Args, " "))
}
func cmdOutputWithSpinner(cmd *exec.Cmd, msg string, stderr bool) (output []byte, err error) {
logCmd(cmd)
func CmdOutputWithSpinner(cmd *exec.Cmd, msg string, stderr bool) (output []byte, err error) {
LogCmd(cmd)
s := startSpinner(msg)
if stderr {
output, err = cmd.CombinedOutput()
@ -38,8 +38,8 @@ func startSpinner(msg string) *spinner.Spinner {
return s
}
func exitWithCommand(cmd *exec.Cmd) {
logCmd(cmd)
func ExitWithCommand(cmd *exec.Cmd) {
LogCmd(cmd)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
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"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"
e "oizys/internal/exec"
"oizys/internal/ui"
)
var o *Oizys
@ -129,7 +131,7 @@ func git(rest ...string) *exec.Cmd {
args := []string{"-C", o.flake}
args = append(args, rest...)
cmd := exec.Command("git", args...)
logCmd(cmd)
e.LogCmd(cmd)
return cmd
}
@ -141,18 +143,18 @@ func GitPull() {
if len(cmdOutput) > 0 {
fmt.Println("unstaged commits, cowardly exiting...")
showFailedOutput(cmdOutput)
ui.ShowFailedOutput(cmdOutput)
os.Exit(1)
}
cmdOutput, err = git("pull").CombinedOutput()
if err != nil {
showFailedOutput(cmdOutput)
ui.ShowFailedOutput(cmdOutput)
log.Fatal(err)
}
}
func parseDryRun(buf string) (*packages, *packages) {
func parseDryRun(buf string) (*ui.Packages, *ui.Packages) {
lines := strings.Split(strings.TrimSpace(buf), "\n")
var parts [2][]string
i := 0
@ -173,8 +175,8 @@ func parseDryRun(buf string) (*packages, *packages) {
os.Exit(0)
}
return parsePackages(parts[0], "packages to build"),
parsePackages(parts[1], "packages to fetch")
return ui.ParsePackages(parts[0], "packages to build"),
ui.ParsePackages(parts[1], "packages to fetch")
}
// TODO: Refactor this and above
@ -203,8 +205,8 @@ func parseDryRun2(buf string) ([]string, []string) {
// TODO: refactor to account for --debug and not --verbose?
func showDryRunResult(nixOutput string, verbose bool) {
toBuild, toFetch := parseDryRun(nixOutput)
toFetch.show(o.debug)
toBuild.show(true)
toFetch.Show(o.debug)
toBuild.Show(true)
}
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),
)
}
result, err := cmdOutputWithSpinner(cmd, spinnerMsg, true)
result, err := e.CmdOutputWithSpinner(cmd, spinnerMsg, true)
if err != nil {
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, rest...)
exitWithCommand(cmd)
e.ExitWithCommand(cmd)
}
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, rest...)
exitWithCommand(cmd)
e.ExitWithCommand(cmd)
}
func (o *Oizys) writeToGithubStepSummary(txt string) {
@ -332,12 +334,12 @@ func CacheBuild(rest ...string) {
args = append(args, NixosConfigAttrs()...)
args = append(args, rest...)
cmd := exec.Command("cachix", args...)
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...)
exitWithCommand(cmd)
e.ExitWithCommand(cmd)
}

View file

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

View file

@ -1,4 +1,4 @@
package oizys
package ui
import (
"fmt"
@ -11,6 +11,17 @@ import (
"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
func terminalSize() (int, int) {
fd := os.Stdout.Fd()
@ -25,13 +36,13 @@ func terminalSize() (int, int) {
return w, h
}
type packages struct {
type Packages struct {
desc string
names []string
pad int
}
func parsePackages(lines []string, desc string) *packages {
func ParsePackages(lines []string, desc string) *Packages {
w, _ := terminalSize()
maxAcceptable := (w / 4) - 1
maxLen := 0
@ -48,7 +59,7 @@ func parsePackages(lines []string, desc string) *packages {
names[i] = name
}
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 {
@ -62,7 +73,7 @@ func ellipsis(s string, maxLen int) string {
return string(runes[0:maxLen-3]) + "..."
}
func (p *packages) show(verbose bool) {
func (p *Packages) Show(verbose bool) {
p.summary()
if !verbose || (len(p.names) == 0) {
return
@ -81,7 +92,7 @@ func (p *packages) show(verbose bool) {
fmt.Println()
}
func (p *packages) summary() {
func (p *Packages) summary() {
fmt.Printf("%s: %s\n",
p.desc,
lipgloss.NewStyle().
@ -90,14 +101,3 @@ func (p *packages) summary() {
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)
}
}