summaryrefslogtreecommitdiff
path: root/stdlib/os/linux/useradd.go
blob: 34c26e67a996e3ad93ab98910c21513d36ccd581 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package linux

import (
	"git.adyxax.org/adyxax/gonf/pkg"
	"os/exec"
)

func Useradd(data gonf.UserData) (gonf.Status, error) {
	getent := exec.Command("getent", "passwd", data.Name)
	if err := getent.Run(); err == nil {
		return gonf.KEPT, nil
	}
	args := make([]string, 0)
	if data.HomeDir != "" {
		args = append(args, "--home-dir="+data.HomeDir)
	}
	if data.System {
		args = append(args, "--system")
	}
	args = append(args, data.Name)
	cmd := exec.Command("useradd", args...)
	if err := cmd.Run(); err != nil {
		return gonf.BROKEN, err
	}
	return gonf.REPAIRED, nil
}