2022-06-15 17:29:45 +02:00
|
|
|
package templater
|
|
|
|
|
2022-06-30 15:46:38 +02:00
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"forge.cadoles.com/pcaseiro/templatefile/pkg/utils"
|
|
|
|
)
|
|
|
|
|
2022-06-15 17:29:45 +02:00
|
|
|
type SystemUser struct {
|
|
|
|
UserName string `json:"username"`
|
|
|
|
Group string `json:"group"`
|
|
|
|
Home string `json:"home"`
|
|
|
|
Shell string `json:"shell"`
|
|
|
|
}
|
2022-06-30 15:46:38 +02:00
|
|
|
|
|
|
|
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 {
|
|
|
|
_, _, err := utils.RunSystemCommand("useradd", "-b", su.Home, "-m", "-G", su.Group, su.UserName)
|
|
|
|
if err != nil {
|
|
|
|
return 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
|
|
|
|
}
|