File: cmd_errors.go

package info (click to toggle)
syncthing 0.14.18%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 7,388 kB
  • ctags: 4,608
  • sloc: xml: 781; sh: 271; makefile: 45
file content (67 lines) | stat: -rw-r--r-- 1,563 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
// Copyright (C) 2014 Audrius Butkevičius

package main

import (
	"encoding/json"
	"fmt"
	"strings"

	"github.com/AudriusButkevicius/cli"
)

func init() {
	cliCommands = append(cliCommands, cli.Command{
		Name:     "errors",
		HideHelp: true,
		Usage:    "Error command group",
		Subcommands: []cli.Command{
			{
				Name:     "show",
				Usage:    "Show pending errors",
				Requires: &cli.Requires{},
				Action:   errorsShow,
			},
			{
				Name:     "push",
				Usage:    "Push an error to active clients",
				Requires: &cli.Requires{"error message..."},
				Action:   errorsPush,
			},
			{
				Name:     "clear",
				Usage:    "Clear pending errors",
				Requires: &cli.Requires{},
				Action:   wrappedHTTPPost("system/error/clear"),
			},
		},
	})
}

func errorsShow(c *cli.Context) {
	response := httpGet(c, "system/error")
	var data map[string][]map[string]interface{}
	json.Unmarshal(responseToBArray(response), &data)
	writer := newTableWriter()
	for _, item := range data["errors"] {
		time := item["time"].(string)[:19]
		time = strings.Replace(time, "T", " ", 1)
		err := item["error"].(string)
		err = strings.TrimSpace(err)
		fmt.Fprintln(writer, time+":\t"+err)
	}
	writer.Flush()
}

func errorsPush(c *cli.Context) {
	err := strings.Join(c.Args(), " ")
	response := httpPost(c, "system/error", strings.TrimSpace(err))
	if response.StatusCode != 200 {
		err = fmt.Sprint("Failed to push error\nStatus code: ", response.StatusCode)
		body := string(responseToBArray(response))
		if body != "" {
			err += "\nBody: " + body
		}
		die(err)
	}
}