aboutsummaryrefslogtreecommitdiff
path: root/rui.go
diff options
context:
space:
mode:
authorFuwn <[email protected]>2024-09-15 18:53:25 -0700
committerFuwn <[email protected]>2024-09-15 18:53:25 -0700
commit56f7875cd7db7da5b0a4c01ffe2cb7f92a97de4a (patch)
tree2653dde3d87c7254d9f046120519eb0bd3f168f2 /rui.go
downloadrui-56f7875cd7db7da5b0a4c01ffe2cb7f92a97de4a.tar.xz
rui-56f7875cd7db7da5b0a4c01ffe2cb7f92a97de4a.zip
feat: initial release
Diffstat (limited to 'rui.go')
-rw-r--r--rui.go147
1 files changed, 147 insertions, 0 deletions
diff --git a/rui.go b/rui.go
new file mode 100644
index 0000000..c16a7a5
--- /dev/null
+++ b/rui.go
@@ -0,0 +1,147 @@
+package main
+
+import (
+ "fmt"
+ "os"
+ "os/exec"
+ "time"
+
+ "github.com/urfave/cli/v2"
+)
+
+func main() {
+ (&cli.App{
+ Name: "rui",
+ 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: "home",
+ Subcommands: []*cli.Command{
+ {
+ Name: "switch",
+ 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",
+ 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()
+}