forked from NEaaS/nescript
-
Notifications
You must be signed in to change notification settings - Fork 1
/
formatter.go
59 lines (53 loc) · 1.82 KB
/
formatter.go
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
package nescript
import (
"fmt"
"strings"
)
// Formatter is a function that can convert a raw command (a string slice) into
// a single string. However, as different commands should be treated differently
// in this regard, the formatter dictates how this conversion happens.
type Formatter func([]string) string
var (
defaultScriptFormatter Formatter = QuoteLastArgFormatter("\"", "\"")
defaultCmdFormatter Formatter = SpaceSepFormatter()
)
// SpaceSepFormatter joins all command and all arguments with a single space to
// create a single string value. This is perhaps the most basic formatter. For
// example, if a single argument contains spaces, the resulting string will
// present no differences between an argument that contains spaces, and the
// spaces used to separate arguments.
func SpaceSepFormatter() Formatter {
return func(raw []string) string {
if len(raw) <= 0 {
return ""
}
return strings.Join(raw, " ")
}
}
// QuoteIfSpaceFormatter acts similarly to SpaceSepFormatter, however if any
// argument contains a space, it will be wrapped in quotes.
func QuoteIfSpaceFormatter(openQuote, closeQuote string) Formatter {
return func(raw []string) string {
if len(raw) <= 0 {
return ""
}
for idx, arg := range raw {
if strings.Contains(arg, " ") {
raw[idx] = fmt.Sprintf("%s%s%s", openQuote, raw[idx], closeQuote)
}
}
return strings.Join(raw, " ")
}
}
// QuoteLastArgFormatter joins all command and all arguments with a single space
// to create a string. However, it will wrap the final argument in quotes (using
// a the given quote chars).
func QuoteLastArgFormatter(openQuote, closeQuote string) Formatter {
return func(raw []string) string {
if len(raw) <= 0 {
return ""
}
raw[len(raw)-1] = fmt.Sprintf("%s%s%s", openQuote, raw[len(raw)-1], closeQuote)
return strings.Join(raw, " ")
}
}