feat: oizys update

This commit is contained in:
Daylin Morgan 2024-05-14 16:11:38 -05:00
parent c5a38b7506
commit 3e38f187dd
Signed by: daylin
GPG key ID: 950D13E9719334AD
5 changed files with 69 additions and 18 deletions

View file

@ -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",
)
}

View file

@ -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")
}

View file

@ -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")
}

View file

@ -21,6 +21,7 @@ type Oizys struct {
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,8 +139,43 @@ 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...")

View file

@ -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
<!-- generated with <3 by daylinmorgan/todo -->