fedilogue/fedictl/fedictl.go

74 lines
1.4 KiB
Go
Raw Permalink Normal View History

2020-10-30 05:10:04 +00:00
package main
import (
"bytes"
2020-10-30 05:10:04 +00:00
"flag"
"fmt"
"io"
"log"
"net/http"
2020-10-30 05:10:04 +00:00
"os"
"time"
2020-10-30 05:10:04 +00:00
)
func main() {
2020-10-30 19:50:45 +00:00
addPtr := flag.String("add", "", "Instance to add")
followPtr := flag.String("follow", "", "Follow a target relay")
statusPtr := flag.Bool("status", false, "Get server status")
2020-10-30 05:10:04 +00:00
flag.Parse()
/* Condition verification */
totalflags := 0
var cmdType string
var requestJson string
if *statusPtr != false {
2020-10-30 05:10:04 +00:00
totalflags++
cmdType = "status"
2020-10-30 05:10:04 +00:00
}
if *followPtr != "" {
2020-10-30 05:10:04 +00:00
totalflags++
cmdType = "follow"
requestJson = fmt.Sprintf(`{"follow": "%s"}`, *followPtr)
2020-10-30 05:10:04 +00:00
}
2020-10-30 19:50:45 +00:00
if *addPtr != "" {
2020-10-30 05:10:04 +00:00
totalflags++
cmdType = "add"
requestJson = fmt.Sprintf(`{"host": "%s"}`, *addPtr)
}
2020-10-30 05:10:04 +00:00
if totalflags > 1 {
fmt.Println("Incompatible arguments, exiting.")
os.Exit(1)
} else if totalflags == 0 {
fmt.Println("No options specified, exiting.")
os.Exit(1)
}
requestBytes := []byte(requestJson)
payload := bytes.NewReader(requestBytes)
req, err := http.NewRequest("POST", "http://127.0.0.1:5555/"+cmdType, payload)
2020-10-30 05:10:04 +00:00
if err != nil {
log.Fatal("Error condition")
2020-10-30 05:10:04 +00:00
}
client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Do(req)
2020-11-13 20:13:13 -05:00
if err != nil {
log.Fatal(err)
2020-11-13 20:13:13 -05:00
}
switch cmdType {
case "add":
fmt.Println("Add")
case "status":
defer resp.Body.Close()
bodyBytes, _ := io.ReadAll(resp.Body)
fmt.Println(string(bodyBytes))
case "follow":
defer resp.Body.Close()
bodyBytes, _ := io.ReadAll(resp.Body)
fmt.Println(string(bodyBytes))
}
2020-10-30 05:10:04 +00:00
}