execuser

package
v0.0.0-...-26b03ef Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 17, 2018 License: MIT, MIT Imports: 5 Imported by: 0

README

execuser

enchant with RunUser

Installation

$ go get github.com/gnue/goutils/execuser

Usage

import "github.com/gnue/goutils/execuser"

Examples

Lookup
cmd := exec.Command("ps", "u")

if isRootUser() {
    if u, err := execuser.Lookup("username"); err == nil {
        u.RunUser(cmd)
    }
}

cmd.Run()

run user by username

LookupPath
cmd := exec.Command("ps", "u")

if isRootUser() {
    if u, err := execuser.LookupPath("."); err == nil {
        u.RunUser(cmd)
    }
}

cmd.Run()

run user by path

Author

gnue

License

MIT

Documentation

Overview

enchant with RunUser

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type User

type User struct {
	Uid      uint32 // user id
	Gid      uint32 // primary group id
	Username string
	Name     string
	HomeDir  string
}

func Current

func Current() (*User, error)

func Lookup

func Lookup(username string) (*User, error)
Example

run user by username

package main

import (
	"os/exec"
	"os/user"

	"github.com/gnue/goutils/execuser"
)

func isRootUser() bool {
	u, err := user.Current()
	if err != nil {
		return false
	}

	return u.Uid == "0"
}

func main() {
	cmd := exec.Command("ps", "u")

	if isRootUser() {
		if u, err := execuser.Lookup("username"); err == nil {
			u.RunUser(cmd)
		}
	}

	cmd.Run()
}
Output:

func LookupId

func LookupId(uid int) (*User, error)

func LookupPath

func LookupPath(path string) (*User, error)
Example

run user by path

package main

import (
	"os/exec"
	"os/user"

	"github.com/gnue/goutils/execuser"
)

func isRootUser() bool {
	u, err := user.Current()
	if err != nil {
		return false
	}

	return u.Uid == "0"
}

func main() {
	cmd := exec.Command("ps", "u")

	if isRootUser() {
		if u, err := execuser.LookupPath("."); err == nil {
			u.RunUser(cmd)
		}
	}

	cmd.Run()
}
Output:

func New

func New(u *user.User) (*User, error)

func (*User) RunUser

func (u *User) RunUser(cmd *exec.Cmd)

func (*User) SysProcAttr

func (u *User) SysProcAttr() *syscall.SysProcAttr

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL