package main import ( "encoding/binary" "encoding/json" "flag" "fmt" "net" "os" "io" ) 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"` } 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 var responseback ResponseBack 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 } sizebytes := make([]byte, 4) b := len(commandByte) // Send the request binary.LittleEndian.PutUint32(sizebytes, uint32(b)) c.Write(sizebytes) 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) } } }