File: cmd_general.go

package info (click to toggle)
syncthing 1.0.0~ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 11,096 kB
  • sloc: xml: 1,028; sh: 219; makefile: 61
file content (94 lines) | stat: -rw-r--r-- 2,228 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (C) 2014 Audrius Butkevičius

package main

import (
	"encoding/json"
	"fmt"
	"os"

	"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,
		},
		{
			Name:     "config",
			Usage:    "Configuration",
			Requires: &cli.Requires{},
			Action:   generalConfiguration,
		},
		{
			Name:     "restart",
			Usage:    "Restart syncthing",
			Requires: &cli.Requires{},
			Action:   wrappedHTTPPost("system/restart"),
		},
		{
			Name:     "shutdown",
			Usage:    "Shutdown syncthing",
			Requires: &cli.Requires{},
			Action:   wrappedHTTPPost("system/shutdown"),
		},
		{
			Name:     "reset",
			Usage:    "Reset syncthing deleting all folders and devices",
			Requires: &cli.Requires{},
			Action:   wrappedHTTPPost("system/reset"),
		},
		{
			Name:     "upgrade",
			Usage:    "Upgrade syncthing (if a newer version is available)",
			Requires: &cli.Requires{},
			Action:   wrappedHTTPPost("system/upgrade"),
		},
		{
			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")
	var status struct{ ConfigInSync bool }
	json.Unmarshal(responseToBArray(response), &status)
	if !status.ConfigInSync {
		die("Config out of sync")
	}
	fmt.Println("Config in sync")
}

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)
}

func generalVersion(c *cli.Context) {
	response := httpGet(c, "system/version")
	version := make(map[string]interface{})
	json.Unmarshal(responseToBArray(response), &version)
	prettyPrintJSON(version)
}