File: config_list.go

package info (click to toggle)
rclone 1.45-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 24,300 kB
  • sloc: python: 364; sh: 255; makefile: 190; xml: 30
file content (94 lines) | stat: -rw-r--r-- 2,033 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
package fs

import (
	"bytes"
	"encoding/csv"
	"fmt"
)

// CommaSepList is a comma separated config value
// It uses the encoding/csv rules for quoting and escaping
type CommaSepList []string

// SpaceSepList is a space separated config value
// It uses the encoding/csv rules for quoting and escaping
type SpaceSepList []string

type genericList []string

func (l CommaSepList) String() string {
	return genericList(l).string(',')
}

// Set the List entries
func (l *CommaSepList) Set(s string) error {
	return (*genericList)(l).set(',', []byte(s))
}

// Type of the value
func (CommaSepList) Type() string {
	return "[]string"
}

// Scan implements the fmt.Scanner interface
func (l *CommaSepList) Scan(s fmt.ScanState, ch rune) error {
	return (*genericList)(l).scan(',', s, ch)
}

func (l SpaceSepList) String() string {
	return genericList(l).string(' ')
}

// Set the List entries
func (l *SpaceSepList) Set(s string) error {
	return (*genericList)(l).set(' ', []byte(s))
}

// Type of the value
func (SpaceSepList) Type() string {
	return "[]string"
}

// Scan implements the fmt.Scanner interface
func (l *SpaceSepList) Scan(s fmt.ScanState, ch rune) error {
	return (*genericList)(l).scan(' ', s, ch)
}

func (gl genericList) string(sep rune) string {
	var buf bytes.Buffer
	w := csv.NewWriter(&buf)
	w.Comma = sep
	err := w.Write(gl)
	if err != nil {
		// can only happen if w.Comma is invalid
		panic(err)
	}
	w.Flush()
	return string(bytes.TrimSpace(buf.Bytes()))
}

func (gl *genericList) set(sep rune, b []byte) error {
	if len(b) == 0 {
		*gl = nil
		return nil
	}
	r := csv.NewReader(bytes.NewReader(b))
	r.Comma = sep

	record, err := r.Read()
	switch _err := err.(type) {
	case nil:
		*gl = record
	case *csv.ParseError:
		err = _err.Err // remove line numbers from the error message
	}
	return err
}

func (gl *genericList) scan(sep rune, s fmt.ScanState, ch rune) error {
	token, err := s.Token(true, func(rune) bool { return true })
	if err != nil {
		return err
	}
	return gl.set(sep, bytes.TrimSpace(token))
}