templatefile/pkg/templater/system_group.go

50 lines
962 B
Go

package templater
import (
"fmt"
"log"
"forge.cadoles.com/pcaseiro/templatefile/pkg/utils"
)
type SystemGroup struct {
GroupName string `json:"groupname"`
}
func (sg *SystemGroup) exists() (bool, error) {
_, _, err := utils.RunSystemCommand("getent", "group", sg.GroupName)
if err != nil {
return false, err
}
return true, nil
}
func (sg *SystemGroup) Manage() error {
exist, _ := sg.exists()
if exist {
log.Printf("\tGroup %s already exists", sg.GroupName)
return nil
}
return sg.Create()
}
func (sg *SystemGroup) Create() error {
_, stdErr, err := utils.RunSystemCommand("groupadd", "-r", sg.GroupName)
if err != nil {
return fmt.Errorf("Group %s creation failed with error: %s %v", sg.GroupName, stdErr, err)
}
return nil
}
func (sg *SystemGroup) Delete() error {
_, _, err := utils.RunSystemCommand("userdel", sg.GroupName)
if err != nil {
return err
}
return nil
}
func (sg *SystemGroup) Update() error {
return nil
}