File: cmd_gui.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 (127 lines) | stat: -rw-r--r-- 2,906 bytes parent folder | download | duplicates (2)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright (C) 2014 Audrius Butkevičius

package main

import (
	"fmt"
	"strings"

	"github.com/AudriusButkevicius/cli"
)

func init() {
	cliCommands = append(cliCommands, cli.Command{
		Name:     "gui",
		HideHelp: true,
		Usage:    "GUI command group",
		Subcommands: []cli.Command{
			{
				Name:     "dump",
				Usage:    "Show all GUI configuration settings",
				Requires: &cli.Requires{},
				Action:   guiDump,
			},
			{
				Name:     "get",
				Usage:    "Get a GUI configuration setting",
				Requires: &cli.Requires{"setting"},
				Action:   guiGet,
			},
			{
				Name:     "set",
				Usage:    "Set a GUI configuration setting",
				Requires: &cli.Requires{"setting", "value"},
				Action:   guiSet,
			},
			{
				Name:     "unset",
				Usage:    "Unset a GUI configuration setting",
				Requires: &cli.Requires{"setting"},
				Action:   guiUnset,
			},
		},
	})
}

func guiDump(c *cli.Context) {
	cfg := getConfig(c).GUI
	writer := newTableWriter()
	fmt.Fprintln(writer, "Enabled:\t", cfg.Enabled, "\t(enabled)")
	fmt.Fprintln(writer, "Use HTTPS:\t", cfg.UseTLS(), "\t(tls)")
	fmt.Fprintln(writer, "Listen Addresses:\t", cfg.Address(), "\t(address)")
	if cfg.User != "" {
		fmt.Fprintln(writer, "Authentication User:\t", cfg.User, "\t(username)")
		fmt.Fprintln(writer, "Authentication Password:\t", cfg.Password, "\t(password)")
	}
	if cfg.APIKey != "" {
		fmt.Fprintln(writer, "API Key:\t", cfg.APIKey, "\t(apikey)")
	}
	writer.Flush()
}

func guiGet(c *cli.Context) {
	cfg := getConfig(c).GUI
	arg := c.Args()[0]
	switch strings.ToLower(arg) {
	case "enabled":
		fmt.Println(cfg.Enabled)
	case "tls":
		fmt.Println(cfg.UseTLS())
	case "address":
		fmt.Println(cfg.Address())
	case "user":
		if cfg.User != "" {
			fmt.Println(cfg.User)
		}
	case "password":
		if cfg.User != "" {
			fmt.Println(cfg.Password)
		}
	case "apikey":
		if cfg.APIKey != "" {
			fmt.Println(cfg.APIKey)
		}
	default:
		die("Invalid setting: " + arg + "\nAvailable settings: enabled, tls, address, user, password, apikey")
	}
}

func guiSet(c *cli.Context) {
	cfg := getConfig(c)
	arg := c.Args()[0]
	val := c.Args()[1]
	switch strings.ToLower(arg) {
	case "enabled":
		cfg.GUI.Enabled = parseBool(val)
	case "tls":
		cfg.GUI.RawUseTLS = parseBool(val)
	case "address":
		validAddress(val)
		cfg.GUI.RawAddress = val
	case "user":
		cfg.GUI.User = val
	case "password":
		cfg.GUI.Password = val
	case "apikey":
		cfg.GUI.APIKey = val
	default:
		die("Invalid setting: " + arg + "\nAvailable settings: enabled, tls, address, user, password, apikey")
	}
	setConfig(c, cfg)
}

func guiUnset(c *cli.Context) {
	cfg := getConfig(c)
	arg := c.Args()[0]
	switch strings.ToLower(arg) {
	case "user":
		cfg.GUI.User = ""
	case "password":
		cfg.GUI.Password = ""
	case "apikey":
		cfg.GUI.APIKey = ""
	default:
		die("Invalid setting: " + arg + "\nAvailable settings: user, password, apikey")
	}
	setConfig(c, cfg)
}