fedilogue/client/searchctl.go

77 lines
1.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"
)
type CommandMap struct {
Type string `json:"Type"`
Endpoint string `json:"Endpoint"`
}
func main() {
shutdownPtr := flag.Bool("shutdown", false, "Shutdown server")
suspendinstancePtr := flag.String("suspend-instance", "", "Instance to Suspend")
resumeinstancePtr := flag.String("resume-instance", "", "Instance to Resume")
addinstancePtr := flag.String("add-instance", "", "Instance to add")
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
if *shutdownPtr == true {
totalflags++
commandMap.Type = "shutdown"
}
if *statusPtr == true {
totalflags++
commandMap.Type = "status"
}
if *addinstancePtr != "" {
totalflags++
commandMap.Type = "add"
commandMap.Endpoint = *addinstancePtr
}
if *suspendinstancePtr != "" {
totalflags++
commandMap.Type = "suspend"
}
if *resumeinstancePtr != "" {
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
}
a := make([]byte, 4)
i := len(commandByte)
binary.LittleEndian.PutUint32(a, uint32(i))
c.Write(a)
c.Write(commandByte)
}