main: implement sudo and machinectl launcher methods

This does almost exactly what github:intgr/ego does, with some minor optimisations and corrections.

Signed-off-by: Ophestra Umiker <cat@ophivana.moe>
This commit is contained in:
2024-07-15 23:29:21 +09:00
parent 1cd0846dc9
commit da7e404bcf
2 changed files with 342 additions and 19 deletions

22
cli.go
View File

@@ -9,26 +9,17 @@ import (
)
var (
ego *user.User
command []string
verbose bool
method = machinectl
userName string
methodFlags [2]bool
printVersion bool
)
const (
machinectl uint8 = iota
machinectlBare
sudo
mustPulse bool
)
func init() {
flag.StringVar(&userName, "u", "ego", "Specify a username")
flag.BoolVar(&methodFlags[0], "sudo", false, "Use 'sudo' to change user")
flag.BoolVar(&methodFlags[1], "bare", false, "Use 'machinectl' but skip xdg-desktop-portal setup")
flag.BoolVar(&mustPulse, "pulse", false, "Treat unavailable PulseAudio as fatal")
flag.BoolVar(&verbose, "v", false, "Verbose output")
flag.BoolVar(&printVersion, "V", false, "Print version")
}
@@ -41,13 +32,6 @@ func copyArgs() {
command = flag.Args()
switch { // zero value is machinectl
case methodFlags[0]:
method = sudo
case methodFlags[1]:
method = machinectlBare
}
if u, err := user.Lookup(userName); err != nil {
if errors.As(err, new(user.UnknownUserError)) {
fmt.Println("unknown user", userName)
@@ -62,6 +46,6 @@ func copyArgs() {
}
if verbose {
fmt.Println("Running command", command, "as user", ego.Username, "("+ego.Uid+")")
fmt.Println("Running as user", ego.Username, "("+ego.Uid+"),", "command:", command)
}
}