templatefile/pkg/templater/system_users.go

60 lines
1.2 KiB
Go

package templater
import (
"fmt"
"log"
"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) exists() (bool, error) {
_, _, err := utils.RunSystemCommand("getent", "passwd", su.UserName)
if err != nil {
return false, err
}
return true, nil
}
func (su *SystemUser) Manage() error {
exist, _ := su.exists()
if exist {
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
}