From 3e38f187dddcbad7d59af2c7d49c01aaa39ea613 Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Tue, 14 May 2024 16:11:38 -0500 Subject: [PATCH] feat: oizys update --- pkgs/oizys/oizys-go/cmd/dry.go | 9 +----- pkgs/oizys/oizys-go/cmd/root.go | 8 +++-- pkgs/oizys/oizys-go/cmd/update.go | 20 +++++++++++++ pkgs/oizys/oizys-go/pkg/oizys.go | 49 +++++++++++++++++++++++++++---- todo.md | 1 - 5 files changed, 69 insertions(+), 18 deletions(-) create mode 100644 pkgs/oizys/oizys-go/cmd/update.go diff --git a/pkgs/oizys/oizys-go/cmd/dry.go b/pkgs/oizys/oizys-go/cmd/dry.go index cdd43d8..a5f5a6e 100644 --- a/pkgs/oizys/oizys-go/cmd/dry.go +++ b/pkgs/oizys/oizys-go/cmd/dry.go @@ -12,15 +12,8 @@ var dryCmd = &cobra.Command{ }, } -var verbose bool func init() { rootCmd.AddCommand(dryCmd) - dryCmd.Flags().BoolVarP( - &verbose, - "verbose", - "v", - false, - "show verbose output", - ) + } diff --git a/pkgs/oizys/oizys-go/cmd/root.go b/pkgs/oizys/oizys-go/cmd/root.go index 800a183..30952f6 100644 --- a/pkgs/oizys/oizys-go/cmd/root.go +++ b/pkgs/oizys/oizys-go/cmd/root.go @@ -12,9 +12,9 @@ func Execute() { cc.Init(&cc.Config{ RootCmd: rootCmd, Headings: cc.HiMagenta + cc.Bold, - Commands: cc.HiYellow + cc.Bold, + Commands: cc.Bold, Example: cc.Italic, - ExecName: cc.HiYellow + cc.Bold, + ExecName: cc.Bold, Flags: cc.Bold, NoExtraNewlines: true, NoBottomNewline: true, @@ -29,6 +29,7 @@ var ( flake string host string cacheName string + verbose bool ) var oizys = o.NewOizys() @@ -37,7 +38,7 @@ var rootCmd = &cobra.Command{ Use: "oizys", Short: "nix begat oizys", PersistentPreRun: func(cmd *cobra.Command, args []string) { - oizys.Update(flake, host, cacheName) + oizys.Update(flake, host, cacheName, verbose) oizys.CheckFlake() }, } @@ -46,4 +47,5 @@ func init() { rootCmd.CompletionOptions.HiddenDefaultCmd = true rootCmd.PersistentFlags().StringVar(&flake, "flake", "", "path to flake ($OIZYS_DIR or $HOME/oizys)") rootCmd.PersistentFlags().StringVar(&host, "host", "", "host to build (current host)") + rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "show verbose output") } diff --git a/pkgs/oizys/oizys-go/cmd/update.go b/pkgs/oizys/oizys-go/cmd/update.go new file mode 100644 index 0000000..431fdec --- /dev/null +++ b/pkgs/oizys/oizys-go/cmd/update.go @@ -0,0 +1,20 @@ +package cmd + +import ( + "github.com/spf13/cobra" +) + +var updateCmd = &cobra.Command{ + Use: "update", + Short: "update and run nixos rebuild", + Run: func(cmd *cobra.Command, args []string) { + oizys.GitPull() + }, +} + +var boot bool + +func init() { + rootCmd.AddCommand(updateCmd) + updateCmd.Flags().BoolVarP(&boot, "boot", "b", false, "run nixos-rebuild boot") +} diff --git a/pkgs/oizys/oizys-go/pkg/oizys.go b/pkgs/oizys/oizys-go/pkg/oizys.go index 9f4aff6..f247bf4 100644 --- a/pkgs/oizys/oizys-go/pkg/oizys.go +++ b/pkgs/oizys/oizys-go/pkg/oizys.go @@ -18,9 +18,10 @@ import ( ) type Oizys struct { - flake string - host string - cache string + flake string + host string + cache string + verbose bool } var output = termenv.NewOutput(os.Stdout) @@ -50,7 +51,7 @@ func (o *Oizys) Output() string { ) } -func (o *Oizys) Update(flake string, host string, cache string) { +func (o *Oizys) Update(flake string, host string, cache string, verbose bool) { if host != "" { o.host = host } @@ -60,6 +61,7 @@ func (o *Oizys) Update(flake string, host string, cache string) { if cache != "" { o.cache = cache } + o.verbose = verbose } func terminalSize() (int, int) { @@ -137,13 +139,48 @@ 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...) +} + +func showFailedOutput(buf []byte) { + arrow := output.String("->").Bold().Foreground(output.Color("9")) + for _, line := range strings.Split(strings.TrimSpace(string(buf)), "\n") { + fmt.Println(arrow, line) + } +} + +func (o *Oizys) GitPull() { + + cmdOutput, err := o.git("status", "--porcelain").Output() + if err != nil { + log.Fatal(err) + } + + if len(cmdOutput) > 0 { + fmt.Println("unstaged commits, cowardly exiting...") + showFailedOutput(cmdOutput) + os.Exit(1) + } + + if cmdOutput, err := o.git("pull").CombinedOutput(); err != nil { + showFailedOutput(cmdOutput) + log.Fatal(err) + } +} + func ParseDryRunOutput(nixOutput string, verbose bool) { - parts := strings.Split("\n" + nixOutput, "\nthese") + parts := strings.Split("\n"+nixOutput, "\nthese") if len(parts) != 3 { log.Println("no changes...") log.Println("or I failed to parse it into the expected number of parts") - fmt.Println(parts) + fmt.Println(parts) return } toBuild := parsePackages(parts[1], "packages to build") diff --git a/todo.md b/todo.md index 415309d..ed676d0 100644 --- a/todo.md +++ b/todo.md @@ -1,6 +1,5 @@ # oizys todo's -- [ ] add `oizys update` to pull changes and run `oizys switch` - [ ] go back to latest kernel when VirtualBox Upgrade is merged