2016-12-17 11:28:59 +00:00
|
|
|
// Copyright (C) 2014 Audrius Butkevičius
|
|
|
|
|
2016-12-17 00:25:45 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2018-07-05 22:37:13 +00:00
|
|
|
"os"
|
2016-12-17 00:25:45 +00:00
|
|
|
|
|
|
|
"github.com/AudriusButkevicius/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cliCommands = append(cliCommands, []cli.Command{
|
|
|
|
{
|
|
|
|
Name: "id",
|
|
|
|
Usage: "Get ID of the Syncthing client",
|
|
|
|
Requires: &cli.Requires{},
|
|
|
|
Action: generalID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "status",
|
|
|
|
Usage: "Configuration status, whether or not a restart is required for changes to take effect",
|
|
|
|
Requires: &cli.Requires{},
|
|
|
|
Action: generalStatus,
|
|
|
|
},
|
2018-07-05 22:37:13 +00:00
|
|
|
{
|
|
|
|
Name: "config",
|
|
|
|
Usage: "Configuration",
|
|
|
|
Requires: &cli.Requires{},
|
|
|
|
Action: generalConfiguration,
|
|
|
|
},
|
2016-12-17 00:25:45 +00:00
|
|
|
{
|
|
|
|
Name: "restart",
|
|
|
|
Usage: "Restart syncthing",
|
|
|
|
Requires: &cli.Requires{},
|
2016-12-17 00:27:48 +00:00
|
|
|
Action: wrappedHTTPPost("system/restart"),
|
2016-12-17 00:25:45 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "shutdown",
|
|
|
|
Usage: "Shutdown syncthing",
|
|
|
|
Requires: &cli.Requires{},
|
2016-12-17 00:27:48 +00:00
|
|
|
Action: wrappedHTTPPost("system/shutdown"),
|
2016-12-17 00:25:45 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "reset",
|
|
|
|
Usage: "Reset syncthing deleting all folders and devices",
|
|
|
|
Requires: &cli.Requires{},
|
2016-12-17 00:27:48 +00:00
|
|
|
Action: wrappedHTTPPost("system/reset"),
|
2016-12-17 00:25:45 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "upgrade",
|
|
|
|
Usage: "Upgrade syncthing (if a newer version is available)",
|
|
|
|
Requires: &cli.Requires{},
|
2016-12-17 00:27:48 +00:00
|
|
|
Action: wrappedHTTPPost("system/upgrade"),
|
2016-12-17 00:25:45 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "version",
|
|
|
|
Usage: "Syncthing client version",
|
|
|
|
Requires: &cli.Requires{},
|
|
|
|
Action: generalVersion,
|
|
|
|
},
|
|
|
|
}...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func generalID(c *cli.Context) {
|
|
|
|
fmt.Println(getMyID(c))
|
|
|
|
}
|
|
|
|
|
|
|
|
func generalStatus(c *cli.Context) {
|
|
|
|
response := httpGet(c, "system/config/insync")
|
2016-12-18 18:57:41 +00:00
|
|
|
var status struct{ ConfigInSync bool }
|
2016-12-17 00:25:45 +00:00
|
|
|
json.Unmarshal(responseToBArray(response), &status)
|
2016-12-18 18:57:41 +00:00
|
|
|
if !status.ConfigInSync {
|
2016-12-17 00:25:45 +00:00
|
|
|
die("Config out of sync")
|
|
|
|
}
|
|
|
|
fmt.Println("Config in sync")
|
|
|
|
}
|
|
|
|
|
2018-07-05 22:37:13 +00:00
|
|
|
func generalConfiguration(c *cli.Context) {
|
|
|
|
response := httpGet(c, "system/config")
|
|
|
|
var jsResponse interface{}
|
|
|
|
json.Unmarshal(responseToBArray(response), &jsResponse)
|
|
|
|
enc := json.NewEncoder(os.Stdout)
|
|
|
|
enc.SetIndent("", " ")
|
|
|
|
enc.Encode(jsResponse)
|
|
|
|
}
|
|
|
|
|
2016-12-17 00:25:45 +00:00
|
|
|
func generalVersion(c *cli.Context) {
|
|
|
|
response := httpGet(c, "system/version")
|
|
|
|
version := make(map[string]interface{})
|
|
|
|
json.Unmarshal(responseToBArray(response), &version)
|
2016-12-17 00:27:48 +00:00
|
|
|
prettyPrintJSON(version)
|
2016-12-17 00:25:45 +00:00
|
|
|
}
|