use better logging

This commit is contained in:
Daylin Morgan 2024-06-18 14:00:53 -05:00
parent 20810ca4ba
commit 52d79fd28c
Signed by: daylin
GPG key ID: 950D13E9719334AD
2 changed files with 24 additions and 32 deletions

View file

@ -42,6 +42,10 @@ var rootCmd = &cobra.Command{
Use: "oizys", Use: "oizys",
Short: "nix begat oizys", Short: "nix begat oizys",
PersistentPreRun: func(cmd *cobra.Command, args []string) { 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.Set(flake, host, cacheName, verbose, systemPath)
oizys.CheckFlake() oizys.CheckFlake()
}, },
@ -53,6 +57,7 @@ func setupLogger() {
for k, v := range styles.Levels { for k, v := range styles.Levels {
styles.Levels[k] = v.MaxWidth(10) styles.Levels[k] = v.MaxWidth(10)
} }
log.SetStyles(styles)
} }
func init() { func init() {

View file

@ -193,10 +193,9 @@ func (p *packages) summary() {
func (o *Oizys) git(rest ...string) *exec.Cmd { func (o *Oizys) git(rest ...string) *exec.Cmd {
args := []string{"-C", o.flake} args := []string{"-C", o.flake}
args = append(args, rest...) args = append(args, rest...)
if o.verbose { cmd := exec.Command("git", args...)
fmt.Println("CMD:", "git", strings.Join(args, " ")) logCmd(cmd)
} return cmd
return exec.Command("git", args...)
} }
func showFailedOutput(buf []byte) { func showFailedOutput(buf []byte) {
@ -257,6 +256,10 @@ func showDryRunResult(nixOutput string, verbose bool) {
toFetch.show(verbose) 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) { func (o *Oizys) NixDryRun(verbose bool, rest ...string) {
args := []string{ args := []string{
"build", o.nixosConfigAttr(), "--dry-run", "build", o.nixosConfigAttr(), "--dry-run",
@ -273,19 +276,19 @@ func (o *Oizys) NixDryRun(verbose bool, rest ...string) {
showDryRunResult(string(result), verbose) showDryRunResult(string(result), verbose)
} }
// / Setup command completely differently here
func (o *Oizys) NixosRebuild(subcmd string, rest ...string) { func (o *Oizys) NixosRebuild(subcmd string, rest ...string) {
args := []string{ cmd := exec.Command("sudo",
"nixos-rebuild", "nixos-rebuild",
subcmd, subcmd,
"--flake", "--flake",
o.flake, o.flake,
} )
args = append(args, rest...) cmd.Args = append(cmd.Args, rest...)
if o.verbose { if o.verbose {
args = append(args, "--print-build-logs") cmd.Args = append(cmd.Args, "--print-build-logs")
fmt.Println("CMD:", "sudo", strings.Join(args, " "))
} }
cmd := exec.Command("sudo", args...) logCmd(cmd)
runCommand(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) { func (o *Oizys) NixBuild(nom bool, rest ...string) {
var cmdName string var cmdName string
if nom { if nom {