fedilogue/fedictl/fedictl.go
Farhan Khan 2d7c09a905 Relay option added
Restructured ctl to http protocol
Minor modifications throughout
2022-01-13 17:09:12 -05:00

74 lines
1.4 KiB
Go

package main
import (
"bytes"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"time"
)
func main() {
addPtr := flag.String("add", "", "Instance to add")
followPtr := flag.String("follow", "", "Follow a target relay")
statusPtr := flag.Bool("status", false, "Get server status")
flag.Parse()
/* Condition verification */
totalflags := 0
var cmdType string
var requestJson string
if *statusPtr != false {
totalflags++
cmdType = "status"
}
if *followPtr != "" {
totalflags++
cmdType = "follow"
requestJson = fmt.Sprintf(`{"follow": "%s"}`, *followPtr)
}
if *addPtr != "" {
totalflags++
cmdType = "add"
requestJson = fmt.Sprintf(`{"host": "%s"}`, *addPtr)
}
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)
if err != nil {
log.Fatal("Error condition")
}
client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
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))
}
}