-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for setting userid/groupid for process execution
- Loading branch information
1 parent
e2c675e
commit f257fd9
Showing
2 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"runtime" | ||
|
||
"github.com/vladimirvivien/gexe" | ||
) | ||
|
||
// This examples demonstrates how to use gexe to run | ||
// subprocesses with different userid/groupid. | ||
// Note: setting userid/gid for subprocesses requires | ||
// elevated privilege such as using sudo. | ||
|
||
func main() { | ||
p := gexe.NewProc(`echo "Hello World!`) | ||
var uid string | ||
|
||
switch runtime.GOOS { | ||
case "windows": | ||
case "darwin": | ||
uid = gexe.Run(`id -u`) | ||
case "linux": | ||
uid = gexe.Run(`id -l`) | ||
} | ||
|
||
if uid != "" { | ||
if err := p.SetUserid(uid).Err(); err != nil { | ||
fmt.Println("Failed to set userid: ", err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
result := p.Run() | ||
if err := result.Err(); err != nil { | ||
fmt.Println("Error: ", err) | ||
os.Exit(1) | ||
} | ||
|
||
fmt.Println("Running process with userid:", uid) | ||
fmt.Println(result.Result()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters