aboutsummaryrefslogtreecommitdiff
path: root/rui.go
blob: edf4c1856053419612d3829b513164e566399a88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main

import (
	"fmt"
	"os"
	"os/exec"
	"time"

	"github.com/urfave/cli/v2"
)

func main() {
	(&cli.App{
		Name:                 "rui",
		Usage:                "Personal NixOS Flake Manager",
		Description:          "Personal NixOS Flake Manager",
		EnableBashCompletion: true,
		Authors: []*cli.Author{
			{
				Name:  "Fuwn",
				Email: "[email protected]",
			},
		},
		Copyright: fmt.Sprintf("Copyright (c) 2024-%s Fuwn", fmt.Sprint(time.Now().Year())),
		ExitErrHandler: func(c *cli.Context, err error) {
			if err != nil {
				fmt.Println(err)
			}
		},
		Suggest: true,
		Commands: []*cli.Command{
			{
				Name: "hs",
				Action: func(c *cli.Context) error {
					return c.App.Command("home").Command("switch").Run(c)
				},
				Hidden:      true,
				Description: "Alias for `home switch`",
			},
			{
				Name: "osw",
				Action: func(c *cli.Context) error {
					return c.App.Command("os").Command("switch").Run(c)
				},
				Hidden: true,
				Usage:  "Alias for `os switch`",
			},
			{
				Name: "home",
				Subcommands: []*cli.Command{
					{
						Name:    "switch",
						Aliases: []string{"sw"},
						Flags: []cli.Flag{
							&cli.BoolFlag{
								Name:  "impure",
								Value: true,
							},
							&cli.BoolFlag{
								Name: "force-home-manager",
							},
							&cli.StringFlag{
								Name: "user",
							},
						},
						Action: func(c *cli.Context) error {
							_, err := exec.LookPath("nh")
							extraArgs := []string{}

							if c.Bool("impure") {
								extraArgs = []string{"--impure"}
							}

							if err == nil && !c.Bool("force-home-manager") {
								return Command("nh", append([]string{"home", "switch", "--"},
									extraArgs...)...)
							}

							user := c.String("user")

							if user == "" {
								user = os.Getenv("USER")
							}

							return Command("home-manager", append([]string{"switch",
								"--flake", fmt.Sprintf("%s#%s", os.Getenv("FLAKE"), user)},
								extraArgs...)...)
						},
					},
					{
						Name: "news",
						Action: func(c *cli.Context) error {
							return Command("home-manager", "news", "--flake",
								os.Getenv("FLAKE"), "--impure")
						},
					},
				},
			},
			{
				Name: "os",
				Subcommands: []*cli.Command{
					{
						Name:    "switch",
						Aliases: []string{"sw"},
						Flags: []cli.Flag{
							&cli.BoolFlag{
								Name: "force-nixos-rebuild",
							},
							&cli.StringFlag{
								Name: "hostname",
							},
						},
						Action: func(c *cli.Context) error {
							_, err := exec.LookPath("nh")

							if err == nil && !c.Bool("force-nixos-rebuild") {
								return Command("nh", "os", "switch")
							}

							_, err = exec.LookPath("doas")
							escalator := "sudo"

							if err == nil {
								escalator = "doas"
							}

							hostname := c.String("hostname")

							if hostname == "" {
								hostname, err = os.Hostname()

								if err != nil {
									return err
								}
							}

							return Command(escalator, "nixos-rebuild", "switch", "--flake",
								fmt.Sprintf("%s#%s", os.Getenv("FLAKE"), hostname))
						},
					},
				},
			},
			{
				Name: "edit",
				Action: func(c *cli.Context) error {
					editor, err := os.LookupEnv("FLAKE_EDITOR")

					if err {
						return Command(editor, os.Getenv("FLAKE"))
					}

					return Command(os.Getenv("EDITOR"), os.Getenv("FLAKE"))
				},
			},
		},
	}).Run(os.Args)
}

func Command(name string, args ...string) error {
	cmd := exec.Command(name, args...)
	cmd.Stdin = os.Stdin
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr

	return cmd.Run()
}