improve oizys-go dry parsing

This commit is contained in:
Daylin Morgan 2024-05-19 10:06:55 -05:00
parent fa200bb369
commit e6e09537b5
Signed by: daylin
GPG Key ID: 950D13E9719334AD
1 changed files with 39 additions and 30 deletions

View File

@ -51,7 +51,10 @@ func (o *Oizys) Output() string {
) )
} }
func (o *Oizys) Update(flake string, host string, cache string, verbose bool) { func (o *Oizys) Update(
flake, host, cache string,
verbose bool,
) {
if host != "" { if host != "" {
o.host = host o.host = host
} }
@ -82,11 +85,10 @@ type packages struct {
desc string desc string
} }
func parsePackages(buf 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
lines := strings.Split(strings.TrimSpace(buf), "\n")[1:]
names := make([]string, len(lines)) names := make([]string, len(lines))
for i, pkg := range lines { for i, pkg := range lines {
s := strings.SplitN(pkg, "-", 2) s := strings.SplitN(pkg, "-", 2)
@ -174,34 +176,32 @@ func (o *Oizys) GitPull() {
} }
} }
func ParseDryRunOutput(nixOutput string, verbose bool) { func parseDryRun(buf string) (*packages, *packages) {
parts := strings.Split("\n"+nixOutput, "\nthese") lines := strings.Split(strings.TrimSpace(buf), "\n")
var parts [2][]string
i := 0
for _, line := range lines {
if strings.Contains(line, "fetch") {
i++
}
if strings.HasPrefix(line, " ") {
parts[i] = append(parts[i], line)
}
}
if len(parts) != 3 { if len(parts[0]) + len(parts[1]) == 0 {
log.Println("no changes...") log.Println("no changes...")
log.Println("or I failed to parse it into the expected number of parts") log.Println("or I failed to parse it into the expected number of parts")
fmt.Println(parts) log.Fatalln("failed to parse nix build --dry-run output")
return
} }
toBuild := parsePackages(parts[1], "packages to build")
toFetch := parsePackages(parts[2], "packages to fetch")
toBuild.show(verbose) return parsePackages(parts[0], "packages to build"), parsePackages(parts[1], "packages to fetch")
toFetch.show(verbose)
} }
func nixSpinner(host string) *spinner.Spinner { func showDryRunResult(nixOutput string, verbose bool) {
msg := fmt.Sprintf("%s %s", " evaluating derivation for:", toBuild, toFetch := parseDryRun(nixOutput)
output.String(host).Bold().Foreground(output.Color("6")), toBuild.show(verbose)
) toFetch.show(verbose)
s := spinner.New(
spinner.CharSets[14],
100*time.Millisecond,
spinner.WithSuffix(msg),
spinner.WithColor("fgHiMagenta"),
)
s.Start()
return s
} }
func (o *Oizys) NixDryRun(verbose bool, rest ...string) { func (o *Oizys) NixDryRun(verbose bool, rest ...string) {
@ -217,7 +217,7 @@ func (o *Oizys) NixDryRun(verbose bool, rest ...string) {
fmt.Println(string(result)) fmt.Println(string(result))
log.Fatal(err) log.Fatal(err)
} }
ParseDryRunOutput(string(result), verbose) showDryRunResult(string(result), verbose)
} }
func (o *Oizys) NixosRebuild(subcmd string, rest ...string) { func (o *Oizys) NixosRebuild(subcmd string, rest ...string) {
@ -258,11 +258,6 @@ func (o *Oizys) CacheBuild(rest ...string) {
runCommand(cmd) runCommand(cmd)
} }
func CheckFlake(flake string) {
if _, err := os.Stat(flake); errors.Is(err, fs.ErrNotExist) {
log.Fatalln("path to flake:", flake, "does not exist")
}
}
func (o *Oizys) CheckFlake() { func (o *Oizys) CheckFlake() {
if _, err := os.Stat(o.flake); errors.Is(err, fs.ErrNotExist) { if _, err := os.Stat(o.flake); errors.Is(err, fs.ErrNotExist) {
@ -277,3 +272,17 @@ func Output(flake string, host string) string {
host, host,
) )
} }
func nixSpinner(host string) *spinner.Spinner {
msg := fmt.Sprintf("%s %s", " evaluating derivation for:",
output.String(host).Bold().Foreground(output.Color("6")),
)
s := spinner.New(
spinner.CharSets[14],
100*time.Millisecond,
spinner.WithSuffix(msg),
spinner.WithColor("fgHiMagenta"),
)
s.Start()
return s
}