2024-07-09 15:39:40 +09:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
userName string
|
|
|
|
methodFlags [2]bool
|
|
|
|
printVersion bool
|
2024-07-15 23:29:21 +09:00
|
|
|
mustPulse bool
|
2024-07-09 15:39:40 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
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")
|
2024-07-15 23:29:21 +09:00
|
|
|
flag.BoolVar(&mustPulse, "pulse", false, "Treat unavailable PulseAudio as fatal")
|
2024-07-09 15:39:40 +09:00
|
|
|
flag.BoolVar(&verbose, "v", false, "Verbose output")
|
|
|
|
flag.BoolVar(&printVersion, "V", false, "Print version")
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyArgs() {
|
|
|
|
if printVersion {
|
|
|
|
fmt.Println(Version)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
command = flag.Args()
|
|
|
|
|
|
|
|
if u, err := user.Lookup(userName); err != nil {
|
|
|
|
if errors.As(err, new(user.UnknownUserError)) {
|
|
|
|
fmt.Println("unknown user", userName)
|
|
|
|
} else {
|
|
|
|
// unreachable
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(1)
|
|
|
|
} else {
|
|
|
|
ego = u
|
|
|
|
}
|
|
|
|
|
|
|
|
if verbose {
|
2024-07-15 23:29:21 +09:00
|
|
|
fmt.Println("Running as user", ego.Username, "("+ego.Uid+"),", "command:", command)
|
2024-07-09 15:39:40 +09:00
|
|
|
}
|
|
|
|
}
|