-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
powershell.go
59 lines (52 loc) · 1.81 KB
/
powershell.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 powershell
import (
"fmt"
"strings"
)
// A KeyValue holds a key-value pair.
type KeyValue struct {
Key string
Value string
}
// FormatStringVariables formats params as PowerShell string variables.
// The caller is responsible for ensuring the keys are valid PowerShell variable names
// and the values can be surrounded by single-quoted strings.
func FormatStringVariables(params []KeyValue) string {
variables := make([]string, 0, len(params))
for _, param := range params {
variables = append(variables, fmt.Sprintf("[string]$%s = '%s'", param.Key, param.Value))
}
return strings.Join(variables, "\n")
}
// JoinVariables joins the specified variables.
func JoinVariables(variables ...string) string {
return strings.Join(variables, "\n")
}
// FormatHashTable formats table as a PowerShell hashtable.
// The caller is responsible for ensuring variableName is a valid PowerShell variable name
// and the key-value pairs can be surrounded by single-quoted strings.
func FormatHashTable(table []KeyValue, variableName string) string {
values := make([]string, 0, len(table))
for _, kv := range table {
values = append(values, fmt.Sprintf(" '%s' = '%s'", kv.Key, kv.Value))
}
return fmt.Sprintf("$%s = @{%s}", variableName, strings.Join(values, ";"))
}
// FormatParams formats params into `-key "value"`, ignoring keys with empty values.
func FormatParams(params []KeyValue) string {
var args []string
for _, param := range params {
if param.Value != "" {
args = append(args, fmt.Sprintf("-%s %q", param.Key, param.Value))
}
}
return strings.Join(args, " ")
}
// ToCLIArgs formats params as CLI arguments.
func ToCLIArgs(params []KeyValue) string {
var args []string
for _, param := range params {
args = append(args, fmt.Sprintf("--%s=%s", param.Key, param.Value))
}
return strings.Join(args, " ")
}