package templater import ( "fmt" "os" "forge.cadoles.com/pcaseiro/templatefile/pkg/utils" ) type SystemService struct { Name string `json:"name"` Enabled bool `json:"enabled"` Type string `json:"type"` } func (sys *SystemService) SetType() { systemdRunDirectory := "/run/systemd/system" openRcBinaryFile := "/sbin/openrc" // Check if the configuration path is a Directory or a file fileInfo, err := os.Stat(systemdRunDirectory) if err == nil { if fileInfo.IsDir() { sys.Type = "systemd" } } fileInfo, err = os.Stat(openRcBinaryFile) if err == nil { if fileInfo.IsDir() { return } sys.Type = "openrc" } } func (sys *SystemService) Manage() error { if sys.Type == "" { sys.SetType() } if sys.Enabled { err := sys.Enable() if err != nil { return err } } else { fmt.Printf("Nothing to do for daemon %s\n", sys.Name) } return nil } func (sys *SystemService) Start() error { fmt.Printf("Starting %s\n", sys.Name) return nil } func (sys *SystemService) Stop() error { fmt.Printf("Stoping %s\n", sys.Name) return nil } func (sys *SystemService) Enable() error { if sys.Type == "systemd" { _, stdErr, err := utils.RunSystemCommand("systemctl", "enable", sys.Name) if err != nil { return fmt.Errorf("System service %s \n * Enable error:\n - %s", sys.Name, stdErr) } } else if sys.Type == "openrc" { return nil } else { return fmt.Errorf("Unsupported service type %s for service %s", sys.Type, sys.Name) } return nil }