cmd_errors.go 1.5 KB

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