yosaictl.go 1011 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "log"
  7. "os"
  8. dclient "git.aetherial.dev/aeth/yosai/pkg/daemonclient"
  9. )
  10. const PRIMARY_SERVER = "primary-vpn"
  11. const SECONDARY_SERVER = "secondary-vpn"
  12. func printUsage() string {
  13. return "Insufficient arguments passed!\nUSAGE: yosaictl <TARGET> <METHOD> <ARGUMENTS>\nExample:\n ~ yosaictl cloud show all"
  14. }
  15. func main() {
  16. var args []string
  17. args = os.Args[1:]
  18. dClient := dclient.DaemonClient{SockPath: dclient.UNIX_DOMAIN_SOCK_PATH}
  19. var rb = bytes.NewBuffer([]byte{})
  20. if len(args) < 2 {
  21. log.Fatal(printUsage())
  22. }
  23. target := args[0]
  24. method := args[1]
  25. var methodArgs []byte
  26. if len(args) == 3 {
  27. methodArgs = dclient.Pack(args[2])
  28. } else {
  29. methodArgs = []byte(dclient.BLANK_JSON)
  30. }
  31. resp := dClient.Call(methodArgs, target, method)
  32. rb.Write(resp.Body)
  33. out := bytes.NewBuffer([]byte{})
  34. err := json.Indent(out, rb.Bytes(), "", " ")
  35. if err != nil {
  36. fmt.Println(string(rb.Bytes()))
  37. os.Exit(0)
  38. }
  39. fmt.Println(string(out.Bytes()))
  40. }