cmd_general.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright (C) 2014 Audrius Butkevičius
  2. package main
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. "github.com/AudriusButkevicius/cli"
  7. )
  8. func init() {
  9. cliCommands = append(cliCommands, []cli.Command{
  10. {
  11. Name: "id",
  12. Usage: "Get ID of the Syncthing client",
  13. Requires: &cli.Requires{},
  14. Action: generalID,
  15. },
  16. {
  17. Name: "status",
  18. Usage: "Configuration status, whether or not a restart is required for changes to take effect",
  19. Requires: &cli.Requires{},
  20. Action: generalStatus,
  21. },
  22. {
  23. Name: "restart",
  24. Usage: "Restart syncthing",
  25. Requires: &cli.Requires{},
  26. Action: wrappedHTTPPost("system/restart"),
  27. },
  28. {
  29. Name: "shutdown",
  30. Usage: "Shutdown syncthing",
  31. Requires: &cli.Requires{},
  32. Action: wrappedHTTPPost("system/shutdown"),
  33. },
  34. {
  35. Name: "reset",
  36. Usage: "Reset syncthing deleting all folders and devices",
  37. Requires: &cli.Requires{},
  38. Action: wrappedHTTPPost("system/reset"),
  39. },
  40. {
  41. Name: "upgrade",
  42. Usage: "Upgrade syncthing (if a newer version is available)",
  43. Requires: &cli.Requires{},
  44. Action: wrappedHTTPPost("system/upgrade"),
  45. },
  46. {
  47. Name: "version",
  48. Usage: "Syncthing client version",
  49. Requires: &cli.Requires{},
  50. Action: generalVersion,
  51. },
  52. }...)
  53. }
  54. func generalID(c *cli.Context) {
  55. fmt.Println(getMyID(c))
  56. }
  57. func generalStatus(c *cli.Context) {
  58. response := httpGet(c, "system/config/insync")
  59. var status struct{ ConfigInSync bool }
  60. json.Unmarshal(responseToBArray(response), &status)
  61. if !status.ConfigInSync {
  62. die("Config out of sync")
  63. }
  64. fmt.Println("Config in sync")
  65. }
  66. func generalVersion(c *cli.Context) {
  67. response := httpGet(c, "system/version")
  68. version := make(map[string]interface{})
  69. json.Unmarshal(responseToBArray(response), &version)
  70. prettyPrintJSON(version)
  71. }