1
0

errors.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright (C) 2019 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package main
  7. import (
  8. "fmt"
  9. "strings"
  10. "github.com/urfave/cli"
  11. )
  12. var errorsCommand = cli.Command{
  13. Name: "errors",
  14. HideHelp: true,
  15. Usage: "Error command group",
  16. Subcommands: []cli.Command{
  17. {
  18. Name: "show",
  19. Usage: "Show pending errors",
  20. Action: expects(0, dumpOutput("system/error")),
  21. },
  22. {
  23. Name: "push",
  24. Usage: "Push an error to active clients",
  25. ArgsUsage: "[error message]",
  26. Action: expects(1, errorsPush),
  27. },
  28. {
  29. Name: "clear",
  30. Usage: "Clear pending errors",
  31. Action: expects(0, emptyPost("system/error/clear")),
  32. },
  33. },
  34. }
  35. func errorsPush(c *cli.Context) error {
  36. client := c.App.Metadata["client"].(*APIClient)
  37. errStr := strings.Join(c.Args(), " ")
  38. response, err := client.Post("system/error", strings.TrimSpace(errStr))
  39. if err != nil {
  40. return err
  41. }
  42. if response.StatusCode != 200 {
  43. errStr = fmt.Sprint("Failed to push error\nStatus code: ", response.StatusCode)
  44. bytes, err := responseToBArray(response)
  45. if err != nil {
  46. return err
  47. }
  48. body := string(bytes)
  49. if body != "" {
  50. errStr += "\nBody: " + body
  51. }
  52. return fmt.Errorf(errStr)
  53. }
  54. return nil
  55. }