cmd_errors.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strings"
  6. "github.com/AudriusButkevicius/cli"
  7. )
  8. func init() {
  9. cliCommands = append(cliCommands, cli.Command{
  10. Name: "errors",
  11. HideHelp: true,
  12. Usage: "Error command group",
  13. Subcommands: []cli.Command{
  14. {
  15. Name: "show",
  16. Usage: "Show pending errors",
  17. Requires: &cli.Requires{},
  18. Action: errorsShow,
  19. },
  20. {
  21. Name: "push",
  22. Usage: "Push an error to active clients",
  23. Requires: &cli.Requires{"error message..."},
  24. Action: errorsPush,
  25. },
  26. {
  27. Name: "clear",
  28. Usage: "Clear pending errors",
  29. Requires: &cli.Requires{},
  30. Action: wrappedHttpPost("system/error/clear"),
  31. },
  32. },
  33. })
  34. }
  35. func errorsShow(c *cli.Context) {
  36. response := httpGet(c, "system/error")
  37. var data map[string][]map[string]interface{}
  38. json.Unmarshal(responseToBArray(response), &data)
  39. writer := newTableWriter()
  40. for _, item := range data["errors"] {
  41. time := item["time"].(string)[:19]
  42. time = strings.Replace(time, "T", " ", 1)
  43. err := item["error"].(string)
  44. err = strings.TrimSpace(err)
  45. fmt.Fprintln(writer, time+":\t"+err)
  46. }
  47. writer.Flush()
  48. }
  49. func errorsPush(c *cli.Context) {
  50. err := strings.Join(c.Args(), " ")
  51. response := httpPost(c, "system/error", strings.TrimSpace(err))
  52. if response.StatusCode != 200 {
  53. err = fmt.Sprint("Failed to push error\nStatus code: ", response.StatusCode)
  54. body := string(responseToBArray(response))
  55. if body != "" {
  56. err += "\nBody: " + body
  57. }
  58. die(err)
  59. }
  60. }