cmd_general.go 2.2 KB

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