File: cmd_conf.go

package info (click to toggle)
golang-github-cretz-bine 0.2.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports, sid, trixie
  • size: 652 kB
  • sloc: makefile: 3
file content (65 lines) | stat: -rw-r--r-- 1,565 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
package control

import (
	"strings"

	"github.com/cretz/bine/torutil"
)

// SetConf invokes SETCONF.
func (c *Conn) SetConf(entries ...*KeyVal) error {
	return c.sendSetConf("SETCONF", entries)
}

// ResetConf invokes RESETCONF.
func (c *Conn) ResetConf(entries ...*KeyVal) error {
	return c.sendSetConf("RESETCONF", entries)
}

func (c *Conn) sendSetConf(cmd string, entries []*KeyVal) error {
	for _, entry := range entries {
		cmd += " " + entry.Key
		if entry.ValSet() {
			cmd += "=" + torutil.EscapeSimpleQuotedStringIfNeeded(entry.Val)
		}
	}
	return c.sendRequestIgnoreResponse(cmd)
}

// GetConf invokes GETCONF and returns the values for the requested keys.
func (c *Conn) GetConf(keys ...string) ([]*KeyVal, error) {
	resp, err := c.SendRequest("GETCONF %v", strings.Join(keys, " "))
	if err != nil {
		return nil, err
	}
	data := resp.DataWithReply()
	ret := make([]*KeyVal, 0, len(data))
	for _, data := range data {
		key, val, ok := torutil.PartitionString(data, '=')
		entry := &KeyVal{Key: key}
		if ok {
			if entry.Val, err = torutil.UnescapeSimpleQuotedStringIfNeeded(val); err != nil {
				return nil, err
			}
			if len(entry.Val) == 0 {
				entry.ValSetAndEmpty = true
			}
		}
		ret = append(ret, entry)
	}
	return ret, nil
}

// SaveConf invokes SAVECONF.
func (c *Conn) SaveConf(force bool) error {
	cmd := "SAVECONF"
	if force {
		cmd += " FORCE"
	}
	return c.sendRequestIgnoreResponse(cmd)
}

// LoadConf invokes LOADCONF.
func (c *Conn) LoadConf(conf string) error {
	return c.sendRequestIgnoreResponse("+LOADCONF\r\n%v\r\n.", conf)
}