fedilogue/client/fedictl.go

153 lines
3.8 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"`
}
// Instance's new min_id value
type RunningInstance struct {
Software string `json:"software"`
Status int `json:"status"`
LastRun string `json:"lastrun"`
2020-12-13 08:25:58 +00:00
CaptureType string `json:"capturetype"`
}
type ResponseBack struct {
Type string `json:"Type"`
Message string `json:"Message"`
RunningInstances map[string]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")
flag.Parse()
/* Condition verification */
totalflags := 0
var commandMap CommandMap
var responseback ResponseBack
2020-10-30 05:10:04 +00:00
2020-11-13 20:13:13 -05:00
if *shutdownPtr {
2020-10-30 05:10:04 +00:00
totalflags++
commandMap.Type = "shutdown"
}
2020-11-13 20:13:13 -05:00
if *statusPtr {
2020-10-30 05:10:04 +00:00
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))
2020-11-13 20:13:13 -05:00
_, err = c.Write(sizebytes)
if err != nil {
fmt.Println(err)
return
}
_, err = c.Write(commandByte)
if err != nil {
fmt.Println(err)
return
}
// 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)
2020-11-13 20:13:13 -05:00
_, err = io.ReadFull(c, responsebytes)
if err != nil {
fmt.Println("Read Error", err)
}
err = json.Unmarshal(responsebytes, &responseback)
if err != nil {
fmt.Println("Unmarshal error", err)
}
switch commandMap.Type {
case "add":
2020-11-18 10:31:09 -05:00
fmt.Println(responseback.Message)
case "status":
2020-12-13 08:25:58 +00:00
fmt.Println("Status\t\tLastRun\t\t\tCaptureType\tEndpoint")
for endpoint, runninginstance := range responseback.RunningInstances {
2020-11-01 01:59:32 -04:00
if runninginstance.Status == 0 {
fmt.Fprintf(os.Stdout, "New\t")
2020-11-17 19:57:39 -05:00
fmt.Fprintf(os.Stdout, "\tNever\t\t\t")
2020-11-01 01:59:32 -04:00
} else if runninginstance.Status == 200 {
fmt.Fprintf(os.Stdout, "Running\t")
2020-11-17 19:57:39 -05:00
fmt.Fprintf(os.Stdout, "\t%s\t", runninginstance.LastRun)
} else if runninginstance.Status == 429 {
fmt.Fprintf(os.Stdout, "TooManyRequests\t")
fmt.Fprintf(os.Stdout, "%s\t", runninginstance.LastRun)
} else if runninginstance.Status == 600 {
fmt.Fprintf(os.Stdout, "ClientIssue")
fmt.Fprintf(os.Stdout, "\t%s\t", runninginstance.LastRun)
2020-11-01 01:59:32 -04:00
} else if runninginstance.Status == 602 {
fmt.Fprintf(os.Stdout, "BadInstance")
2020-11-17 19:57:39 -05:00
fmt.Fprintf(os.Stdout, "\t%s\t", runninginstance.LastRun)
} else if runninginstance.Status == 605 {
fmt.Fprintf(os.Stdout, "UnsupportedNode")
fmt.Fprintf(os.Stdout, "\t%s\t", runninginstance.LastRun)
2020-11-01 01:59:32 -04:00
} else {
fmt.Fprintf(os.Stdout, "%d\t", runninginstance.Status)
2020-11-17 19:57:39 -05:00
fmt.Fprintf(os.Stdout, "\t%s\t", runninginstance.LastRun)
2020-11-01 01:59:32 -04:00
}
if runninginstance.LastRun == "Queued" {
fmt.Fprintf(os.Stdout, "\t\t")
}
2020-12-13 08:25:58 +00:00
fmt.Fprintf(os.Stdout, "\t%s\t", runninginstance.CaptureType)
fmt.Fprintf(os.Stdout, "%s\n", endpoint)
}
}
2020-10-30 05:10:04 +00:00
}