templatefile/pkg/templater/system_users.go

53 lines
1.0 KiB
Go

package templater
import (
"fmt"
"log"
"os/user"
"forge.cadoles.com/pcaseiro/templatefile/pkg/utils"
)
type SystemUser struct {
UserName string `json:"username"`
Group string `json:"group"`
Home string `json:"home"`
Shell string `json:"shell"`
}
func (su *SystemUser) Manage() error {
_, err := user.Lookup(su.UserName)
if err == nil {
log.Printf("\tUser %s already exists", su.UserName)
return nil
}
return su.Create()
}
func (su *SystemUser) Create() error {
// Manage System Group
grp := SystemGroup{GroupName: su.Group}
err := grp.Manage()
if err != nil {
return err
}
_, stdErr, err := utils.RunSystemCommand("useradd", "-b", su.Home, "-m", "-N", "-g", su.Group, su.UserName)
if err != nil {
return fmt.Errorf("User %s creation failed with error: %s %v", su.UserName, stdErr, err)
}
return nil
}
func (su *SystemUser) Delete() error {
_, _, err := utils.RunSystemCommand("userdel", su.UserName)
if err != nil {
return err
}
return nil
}
func (su *SystemUser) Update() error {
return nil
}