mirror of
https://github.com/EnumeratedDev/enit.git
synced 2026-09-16 10:36:12 +00:00
Move service list functionality to esvm
This commit is contained in:
+66
-6
@@ -57,13 +57,13 @@ func main() {
|
|||||||
if len(flag.Args()) <= 1 {
|
if len(flag.Args()) <= 1 {
|
||||||
fmt.Println("Usage: ectl service <start/stop/enable/disable/status/list> [service]")
|
fmt.Println("Usage: ectl service <start/stop/enable/disable/status/list> [service]")
|
||||||
return
|
return
|
||||||
} else if flag.Args()[1] == "list" {
|
|
||||||
fmt.Println("To be impemented")
|
|
||||||
return
|
|
||||||
} else if len(flag.Args()) <= 2 {
|
|
||||||
fmt.Printf("Usage: ectl service %s <service>\n", flag.Args()[1])
|
|
||||||
return
|
|
||||||
} else if flag.Arg(1) == "start" || flag.Arg(1) == "stop" || flag.Arg(1) == "restart" || flag.Arg(1) == "enable" || flag.Arg(1) == "disable" {
|
} else if flag.Arg(1) == "start" || flag.Arg(1) == "stop" || flag.Arg(1) == "restart" || flag.Arg(1) == "enable" || flag.Arg(1) == "disable" {
|
||||||
|
// Ensure service name argument has been set
|
||||||
|
if len(flag.Args()) <= 2 {
|
||||||
|
fmt.Printf("Usage: ectl service %s <service>\n", flag.Args()[1])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
type ServiceCommandJsonStruct struct {
|
type ServiceCommandJsonStruct struct {
|
||||||
Command string `json:"command"`
|
Command string `json:"command"`
|
||||||
Service string `json:"service"`
|
Service string `json:"service"`
|
||||||
@@ -113,6 +113,12 @@ func main() {
|
|||||||
|
|
||||||
return
|
return
|
||||||
} else if flag.Args()[1] == "status" {
|
} else if flag.Args()[1] == "status" {
|
||||||
|
// Ensure service name argument has been set
|
||||||
|
if len(flag.Args()) <= 2 {
|
||||||
|
fmt.Printf("Usage: ectl service %s <service>\n", flag.Args()[1])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
type ServiceCommandJsonStruct struct {
|
type ServiceCommandJsonStruct struct {
|
||||||
Command string `json:"command"`
|
Command string `json:"command"`
|
||||||
Service string `json:"service"`
|
Service string `json:"service"`
|
||||||
@@ -163,6 +169,60 @@ func main() {
|
|||||||
fmt.Printf("State: %s\n", serviceState)
|
fmt.Printf("State: %s\n", serviceState)
|
||||||
fmt.Printf("Enabled: %t\n", serviceEnabled)
|
fmt.Printf("Enabled: %t\n", serviceEnabled)
|
||||||
|
|
||||||
|
return
|
||||||
|
} else if flag.Arg(1) == "list" {
|
||||||
|
type ServiceCommandJsonStruct struct {
|
||||||
|
Command string `json:"command"`
|
||||||
|
}
|
||||||
|
serviceCommandJson := ServiceCommandJsonStruct{
|
||||||
|
Command: flag.Arg(1),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encode struct to json string
|
||||||
|
jsonData, err := json.Marshal(serviceCommandJson)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Could not encode JSON data! Error: %s\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = conn.Write(jsonData)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Could not write JSON data to socket! Error: %s\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a buffer for incoming data.
|
||||||
|
buf := make([]byte, 4096)
|
||||||
|
|
||||||
|
// Read data from the connection.
|
||||||
|
n, err := conn.Read(buf)
|
||||||
|
if err == io.EOF {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decoode JSON data
|
||||||
|
var returnedJsonData map[string]any
|
||||||
|
err = json.Unmarshal(buf[:n], &returnedJsonData)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Could not decode JSON data from connection!")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err, ok := returnedJsonData["error"]; ok {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, serviceMap := range returnedJsonData["services"].([]any) {
|
||||||
|
serviceName := serviceMap.(map[string]any)["name"].(string)
|
||||||
|
serviceState := serviceMap.(map[string]any)["state"].(string)
|
||||||
|
serviceEnabled := serviceMap.(map[string]any)["is_enabled"].(bool)
|
||||||
|
|
||||||
|
fmt.Printf("Name: %s\n", serviceName)
|
||||||
|
fmt.Printf("State: %s\n", serviceState)
|
||||||
|
fmt.Printf("Enabled: %t\n", serviceEnabled)
|
||||||
|
fmt.Println()
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ func initSocket() (socket net.Listener, err error) {
|
|||||||
commandHandlers["enable"] = handleEnableServiceCommand
|
commandHandlers["enable"] = handleEnableServiceCommand
|
||||||
commandHandlers["disable"] = handleDisableServiceCommand
|
commandHandlers["disable"] = handleDisableServiceCommand
|
||||||
commandHandlers["status"] = handleStatusServiceCommand
|
commandHandlers["status"] = handleStatusServiceCommand
|
||||||
|
commandHandlers["list"] = handleListServicesCommand
|
||||||
|
|
||||||
return socket, nil
|
return socket, nil
|
||||||
}
|
}
|
||||||
@@ -238,6 +239,29 @@ func handleStatusServiceCommand(conn net.Conn, jsonData map[string]any) {
|
|||||||
conn.Write(newJsonData)
|
conn.Write(newJsonData)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handleListServicesCommand(conn net.Conn, _ map[string]any) {
|
||||||
|
servicesMap := make(map[string]any)
|
||||||
|
servicesMap["services"] = make([]map[string]any, 0)
|
||||||
|
|
||||||
|
// Loop through each service
|
||||||
|
for _, service := range Services {
|
||||||
|
statusMap := make(map[string]any)
|
||||||
|
statusMap["name"] = service.Name
|
||||||
|
statusMap["state"] = EnitServiceStateNames[service.GetCurrentState()]
|
||||||
|
statusMap["is_enabled"] = service.isEnabled()
|
||||||
|
servicesMap["services"] = append(servicesMap["services"].([]map[string]any), statusMap)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encode map to json string
|
||||||
|
newJsonData, err := json.Marshal(servicesMap)
|
||||||
|
if err != nil {
|
||||||
|
conn.Write(wrapErrorInJson(fmt.Errorf("Could not encode JSON data")))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
conn.Write(newJsonData)
|
||||||
|
}
|
||||||
|
|
||||||
func wrapErrorInJson(err error) []byte {
|
func wrapErrorInJson(err error) []byte {
|
||||||
// Wrap error in struct
|
// Wrap error in struct
|
||||||
type jsonErrorStruct struct {
|
type jsonErrorStruct struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user