mirror of
https://github.com/EnumeratedDev/enit.git
synced 2026-09-16 10:36:12 +00:00
Move service status functionality to esvm
This commit is contained in:
@@ -22,6 +22,15 @@ const (
|
||||
EnitServiceCompleted
|
||||
)
|
||||
|
||||
var EnitServiceStateNames map[EnitServiceState]string = map[EnitServiceState]string{
|
||||
EnitServiceUnknown: "unknown",
|
||||
EnitServiceUnloaded: "unloaded",
|
||||
EnitServiceRunning: "running",
|
||||
EnitServiceStopped: "stopped",
|
||||
EnitServiceCrashed: "crashed",
|
||||
EnitServiceCompleted: "completed",
|
||||
}
|
||||
|
||||
type EnitService struct {
|
||||
Name string `yaml:"name"`
|
||||
Description string `yaml:"description,omitempty"`
|
||||
|
||||
@@ -22,6 +22,7 @@ func initSocket() (socket net.Listener, err error) {
|
||||
commandHandlers["restart"] = handleRestartServiceCommand
|
||||
commandHandlers["enable"] = handleEnableServiceCommand
|
||||
commandHandlers["disable"] = handleDisableServiceCommand
|
||||
commandHandlers["status"] = handleStatusServiceCommand
|
||||
|
||||
return socket, nil
|
||||
}
|
||||
@@ -207,6 +208,36 @@ func handleDisableServiceCommand(conn net.Conn, jsonData map[string]any) {
|
||||
conn.Write(wrapSuccessMsgInJson(fmt.Sprintf("Service (%s) was disabled sucessfully", serviceName.(string))))
|
||||
}
|
||||
|
||||
func handleStatusServiceCommand(conn net.Conn, jsonData map[string]any) {
|
||||
// Get service name from json data
|
||||
serviceName, ok := jsonData["service"]
|
||||
if !ok {
|
||||
conn.Write(wrapErrorInJson(fmt.Errorf("'service' field missing")))
|
||||
return
|
||||
}
|
||||
|
||||
// Ensure service exists
|
||||
service := GetServiceByName(serviceName.(string))
|
||||
if service == nil {
|
||||
conn.Write(wrapErrorInJson(fmt.Errorf("Service (%s) not found", serviceName.(string))))
|
||||
return
|
||||
}
|
||||
|
||||
statusMap := make(map[string]any)
|
||||
statusMap["name"] = service.Name
|
||||
statusMap["state"] = EnitServiceStateNames[service.GetCurrentState()]
|
||||
statusMap["is_enabled"] = service.isEnabled()
|
||||
|
||||
// Encode map to json string
|
||||
newJsonData, err := json.Marshal(statusMap)
|
||||
if err != nil {
|
||||
conn.Write(wrapErrorInJson(fmt.Errorf("Could not encode JSON data")))
|
||||
return
|
||||
}
|
||||
|
||||
conn.Write(newJsonData)
|
||||
}
|
||||
|
||||
func wrapErrorInJson(err error) []byte {
|
||||
// Wrap error in struct
|
||||
type jsonErrorStruct struct {
|
||||
|
||||
Reference in New Issue
Block a user