Move checkRoot to main

Signed-off-by: Dimitris Karakasilis <dimitris@karakasilis.me>
This commit is contained in:
Dimitris Karakasilis
2023-09-14 15:53:27 +03:00
parent fddbf3f657
commit bf40c48812
6 changed files with 29 additions and 36 deletions

36
main.go
View File

@@ -3,6 +3,7 @@ package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
@@ -119,7 +120,8 @@ See https://kairos.io/docs/upgrade/manual/ for documentation.
return fmt.Errorf("source %s does not match any of oci:, dir: or file: ", source)
}
}
return nil
return checkRoot()
},
Action: func(c *cli.Context) error {
var v string
@@ -230,6 +232,9 @@ E.g. kairos-agent install-bundle container:quay.io/kairos/kairos...
},
},
UsageText: "Install a bundle manually in the node",
Before: func(c *cli.Context) error {
return checkRoot()
},
Action: func(c *cli.Context) error {
if c.Args().Len() != 1 {
return fmt.Errorf("bundle name required")
@@ -382,6 +387,9 @@ This command is meant to be used from the boot GRUB menu, but can be also starte
},
},
Usage: "Starts interactive installation",
Before: func(c *cli.Context) error {
return checkRoot()
},
Action: func(c *cli.Context) error {
return agent.InteractiveInstall(c.Bool("debug"), c.Bool("shell"))
},
@@ -403,6 +411,9 @@ This command is meant to be used from the boot GRUB menu, but can be also starte
Name: "reboot",
},
},
Before: func(c *cli.Context) error {
return checkRoot()
},
Action: func(c *cli.Context) error {
if c.NArg() == 0 {
return fmt.Errorf("expect one argument. the config file - if you don't have it, use the interactive-install")
@@ -424,6 +435,9 @@ See also https://kairos.io/docs/installation/qrcode/ for documentation.
This command is meant to be used from the boot GRUB menu, but can be started manually`,
Aliases: []string{"i"},
Before: func(c *cli.Context) error {
return checkRoot()
},
Action: func(c *cli.Context) error {
return agent.Install(configScanDir...)
},
@@ -457,6 +471,9 @@ This command is meant to be used from the boot GRUB menu, but can likely be used
Usage: "Do not wait for user input and provide ttys after reset. Also sets the fast mode (do not wait 60 seconds before reset)",
},
},
Before: func(c *cli.Context) error {
return checkRoot()
},
Action: func(c *cli.Context) error {
reboot := c.Bool("reboot")
unattended := c.Bool("unattended")
@@ -535,7 +552,8 @@ The validate command expects a configuration file as its only argument. Local fi
_ = cli.ShowSubcommandHelp(c)
return fmt.Errorf("")
}
return nil
return checkRoot()
},
Action: func(c *cli.Context) error {
stage := c.Args().First()
@@ -574,11 +592,7 @@ The validate command expects a configuration file as its only argument. Local fi
return fmt.Errorf("")
}
if os.Geteuid() != 0 {
return fmt.Errorf("this command requires root privileges")
}
return nil
return checkRoot()
},
Action: func(c *cli.Context) error {
image := c.Args().Get(0)
@@ -664,3 +678,11 @@ The kairos agent is a component to abstract away node ops, providing a common fe
os.Exit(1)
}
}
func checkRoot() error {
if os.Geteuid() != 0 {
return errors.New("this command requires root privileges")
}
return nil
}