fedilogue/client/searchctl.go

117 lines
2.6 KiB
Go
Raw Normal View History

2020-10-30 05:10:04 +00:00
package main
import (
"encoding/binary"
"encoding/json"
"flag"
"fmt"
"net"
"os"
"io"
2020-10-30 05:10:04 +00:00
)
type CommandMap struct {
Type string `json:"Type"`
Endpoint string `json:"Endpoint"`
}
type RunningInstance struct {
Endpoint string `json:"endpoint"`
Software string `json:"software"`
Min_id string
Status int `json:"status"`
}
type ResponseBack struct {
Type string `json:"Type"`
Message string `json:"Message"`
RunningInstances []RunningInstance `json:"RunningInstances"`
}
2020-10-30 05:10:04 +00:00
func main() {
shutdownPtr := flag.Bool("shutdown", false, "Shutdown server")
2020-10-30 19:50:45 +00:00
suspendPtr := flag.String("suspend", "", "Instance to Suspend")
resumePtr := flag.String("resume", "", "Instance to Resume")
addPtr := flag.String("add", "", "Instance to add")
2020-10-30 05:10:04 +00:00
statusPtr := flag.Bool("status", false, "Check status")
// usernamePtr := flag.String("username", "", "Set username")
// passwordPtr := flag.String("password", "", "Set password")
flag.Parse()
/* Condition verification */
totalflags := 0
var commandMap CommandMap
var responseback ResponseBack
2020-10-30 05:10:04 +00:00
if *shutdownPtr == true {
totalflags++
commandMap.Type = "shutdown"
}
if *statusPtr == true {
totalflags++
commandMap.Type = "status"
}
2020-10-30 19:50:45 +00:00
if *addPtr != "" {
2020-10-30 05:10:04 +00:00
totalflags++
commandMap.Type = "add"
2020-10-30 19:50:45 +00:00
commandMap.Endpoint = *addPtr
2020-10-30 05:10:04 +00:00
}
2020-10-30 19:50:45 +00:00
if *suspendPtr != "" {
2020-10-30 05:10:04 +00:00
totalflags++
commandMap.Type = "suspend"
}
2020-10-30 19:50:45 +00:00
if *resumePtr != "" {
2020-10-30 05:10:04 +00:00
totalflags++
commandMap.Type = "resume"
}
if totalflags > 1 {
fmt.Println("Incompatible arguments, exiting.")
os.Exit(1)
} else if totalflags == 0 {
fmt.Println("No options specified, exiting.")
os.Exit(1)
}
commandByte, err := json.Marshal(commandMap)
if err != nil {
fmt.Println(err)
return
}
c, err := net.Dial("tcp", "127.0.0.1:5555")
if err != nil {
fmt.Println(err)
return
}
sizebytes := make([]byte, 4)
b := len(commandByte)
// Send the request
binary.LittleEndian.PutUint32(sizebytes, uint32(b))
c.Write(sizebytes)
2020-10-30 05:10:04 +00:00
c.Write(commandByte)
// Read the response
n, err := io.ReadFull(c, sizebytes)
if err != nil || n != 4 {
fmt.Println("err", err, n)
}
jsonsize := int(binary.LittleEndian.Uint32(sizebytes))
responsebytes := make([]byte, jsonsize)
n, err = io.ReadFull(c, responsebytes)
err = json.Unmarshal(responsebytes, &responseback)
if err != nil {
fmt.Println("Unmarshal error", err)
}
switch commandMap.Type {
case "add":
fmt.Println("Add instance")
case "status":
fmt.Println("Status:" + responseback.Message)
for x, runninginstance := range responseback.RunningInstances {
fmt.Println("ID:", x, runninginstance.Endpoint, runninginstance.Status)
}
}
2020-10-30 05:10:04 +00:00
}