From dc27990fc56ed575c38865b8d0b882c16f0304bb Mon Sep 17 00:00:00 2001 From: mzz2017 Date: Thu, 15 Sep 2022 20:21:12 +0800 Subject: [PATCH] feat: auto su --- cmd/cmd.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/cmd/cmd.go b/cmd/cmd.go index a7f2617..0f0671e 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -11,10 +11,45 @@ import ( "os" "os/exec" "os/signal" + "path/filepath" "runtime" "syscall" ) +func AutoSu() { + if os.Getuid() == 0 { + return + } + program := filepath.Base(os.Args[0]) + pathSudo, err := exec.LookPath("sudo") + if err != nil { + // skip + return + } + // https://github.com/WireGuard/wireguard-tools/blob/71799a8f6d1450b63071a21cad6ed434b348d3d5/src/wg-quick/linux.bash#L85 + p, err := os.StartProcess(pathSudo, append([]string{ + pathSudo, + "-E", + "-p", + fmt.Sprintf("%v must be run as root. Please enter the password for %%u to continue: ", program), + "--", + }, os.Args...), &os.ProcAttr{ + Files: []*os.File{ + os.Stdin, + os.Stdout, + os.Stderr, + }, + }) + if err != nil { + logrus.Fatal(err) + } + stat, err := p.Wait() + if err != nil { + os.Exit(1) + } + os.Exit(stat.ExitCode()) +} + var ( v *viper.Viper Version = "unknown" @@ -34,6 +69,8 @@ or $ gg git clone https://github.com/mzz2017/gg.git`) return } + // auto su + AutoSu() // initiate config from args and config file log := GetLogger(verbose) log.Traceln("Version:", Version)