From 52d79fd28cf81080df78f18799200c70de35bc84 Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Tue, 18 Jun 2024 14:00:53 -0500 Subject: [PATCH] use better logging --- pkgs/oizys/oizys-go/cmd/root.go | 17 ++++++---- pkgs/oizys/oizys-go/internal/oizys/main.go | 39 ++++++++-------------- 2 files changed, 24 insertions(+), 32 deletions(-) diff --git a/pkgs/oizys/oizys-go/cmd/root.go b/pkgs/oizys/oizys-go/cmd/root.go index a573b4b..15ab125 100644 --- a/pkgs/oizys/oizys-go/cmd/root.go +++ b/pkgs/oizys/oizys-go/cmd/root.go @@ -42,21 +42,26 @@ var rootCmd = &cobra.Command{ Use: "oizys", Short: "nix begat oizys", PersistentPreRun: func(cmd *cobra.Command, args []string) { + if verbose { + log.Info("running with verbose mode") + log.SetLevel(log.DebugLevel) + } oizys.Set(flake, host, cacheName, verbose, systemPath) oizys.CheckFlake() }, } func setupLogger() { - log.SetReportTimestamp(false) - styles := log.DefaultStyles() - for k, v := range styles.Levels { - styles.Levels[k] = v.MaxWidth(10) - } + log.SetReportTimestamp(false) + styles := log.DefaultStyles() + for k, v := range styles.Levels { + styles.Levels[k] = v.MaxWidth(10) + } + log.SetStyles(styles) } func init() { - setupLogger() + setupLogger() rootCmd.CompletionOptions.HiddenDefaultCmd = true rootCmd.PersistentFlags().StringVar(&flake, "flake", "", "path to flake ($OIZYS_DIR or $HOME/oizys)") diff --git a/pkgs/oizys/oizys-go/internal/oizys/main.go b/pkgs/oizys/oizys-go/internal/oizys/main.go index c5abd37..8d9a76e 100644 --- a/pkgs/oizys/oizys-go/internal/oizys/main.go +++ b/pkgs/oizys/oizys-go/internal/oizys/main.go @@ -193,10 +193,9 @@ func (p *packages) summary() { func (o *Oizys) git(rest ...string) *exec.Cmd { args := []string{"-C", o.flake} args = append(args, rest...) - if o.verbose { - fmt.Println("CMD:", "git", strings.Join(args, " ")) - } - return exec.Command("git", args...) + cmd := exec.Command("git", args...) + logCmd(cmd) + return cmd } func showFailedOutput(buf []byte) { @@ -257,6 +256,10 @@ func showDryRunResult(nixOutput string, verbose bool) { toFetch.show(verbose) } +func logCmd(cmd *exec.Cmd) { + log.Debugf("CMD: %s %s", cmd.Path, strings.Join(cmd.Args, " ")) +} + func (o *Oizys) NixDryRun(verbose bool, rest ...string) { args := []string{ "build", o.nixosConfigAttr(), "--dry-run", @@ -273,19 +276,19 @@ func (o *Oizys) NixDryRun(verbose bool, rest ...string) { showDryRunResult(string(result), verbose) } +// / Setup command completely differently here func (o *Oizys) NixosRebuild(subcmd string, rest ...string) { - args := []string{ + cmd := exec.Command("sudo", "nixos-rebuild", subcmd, "--flake", o.flake, - } - args = append(args, rest...) + ) + cmd.Args = append(cmd.Args, rest...) if o.verbose { - args = append(args, "--print-build-logs") - fmt.Println("CMD:", "sudo", strings.Join(args, " ")) + cmd.Args = append(cmd.Args, "--print-build-logs") } - cmd := exec.Command("sudo", args...) + logCmd(cmd) runCommand(cmd) } @@ -297,22 +300,6 @@ func runCommand(cmd *exec.Cmd) { } } -// func runBuildWithNom(buildCmd *exec.Cmd) { -// log.Println("starting build?") -// nomCmd := exec.Command("nom","--json") -// var err error -// buildCmd.Args = append(buildCmd.Args, "--log-format", "internal-json", "-v") -// nomCmd.Stdin, err = buildCmd.StderrPipe() -// if err != nil { -// log.Fatal(err) -// } -// nomCmd.Stdout = os.Stdout -// nomCmd.Start() -// log.Println("starting buildcmd?") -// buildCmd.Run() -// nomCmd.Wait() -// } - func (o *Oizys) NixBuild(nom bool, rest ...string) { var cmdName string if nom {