aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--.gitignore25
-rw-r--r--cli.go67
-rw-r--r--go.mod3
-rw-r--r--main.go12
4 files changed, 107 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 00000000..ae08513c
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,25 @@
+# Binaries for programs and plugins
+*.exe
+*.exe~
+*.dll
+*.so
+*.dylib
+/ego
+
+# Test binary, built with `go test -c`
+*.test
+
+# Output of the go coverage tool, specifically when used with LiteIDE
+*.out
+
+# Dependency directories (remove the comment below to include it)
+# vendor/
+
+# Go workspace file
+go.work
+go.work.sum
+
+# env file
+.env
+.idea
+.vscode \ No newline at end of file
diff --git a/cli.go b/cli.go
new file mode 100644
index 00000000..e85917e1
--- /dev/null
+++ b/cli.go
@@ -0,0 +1,67 @@
+package main
+
+import (
+ "errors"
+ "flag"
+ "fmt"
+ "os"
+ "os/user"
+)
+
+var (
+ ego *user.User
+ command []string
+ verbose bool
+ method = machinectl
+
+ userName string
+ methodFlags [2]bool
+ printVersion bool
+)
+
+const (
+ machinectl uint8 = iota
+ machinectlBare
+ sudo
+)
+
+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(&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()
+
+ 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)
+ } else {
+ // unreachable
+ panic(err)
+ }
+
+ os.Exit(1)
+ } else {
+ ego = u
+ }
+
+ if verbose {
+ fmt.Println("Running command", command, "as user", ego.Username, "("+ego.Uid+")")
+ }
+}
diff --git a/go.mod b/go.mod
new file mode 100644
index 00000000..c3d79715
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,3 @@
+module git.ophivana.moe/cat/ego
+
+go 1.22
diff --git a/main.go b/main.go
new file mode 100644
index 00000000..159d6d77
--- /dev/null
+++ b/main.go
@@ -0,0 +1,12 @@
+package main
+
+import (
+ "flag"
+)
+
+var Version = "impure"
+
+func main() {
+ flag.Parse()
+ copyArgs()
+}