-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
178 lines (141 loc) · 4.1 KB
/
example_test.go
File metadata and controls
178 lines (141 loc) · 4.1 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Use of this source code is governed by the LICENSE file in this module's root
// directory.
package kargs_test
import (
"fmt"
kargs "github.com/synackd/go-kargs"
)
func ExampleKargs_AppendKargs() {
cmdline := `key=val1`
k := kargs.NewKargs([]byte(cmdline))
fmt.Println(k)
// Append new values
k.AppendKargs("key=val2 key=val3")
fmt.Println(k)
// Try to append existing (remains unchanged)
k.AppendKargs("key=val1")
fmt.Println(k)
// Output:
// key=val1
// key=val1 key=val2 key=val3
// key=val1 key=val2 key=val3
}
func ExampleKargs_ContainsKarg() {
cmdline := `key1 key2=val`
k := kargs.NewKargs([]byte(cmdline))
kList := []struct {
key string
exists bool
}{
{key: "key1", exists: k.ContainsKarg("key1")},
{key: "key2", exists: k.ContainsKarg("key2")},
{key: "key3", exists: k.ContainsKarg("key3")},
}
for _, v := range kList {
fmt.Printf("contains %s: %v\n", v.key, v.exists)
}
// Output:
// contains key1: true
// contains key2: true
// contains key3: false
}
func ExampleKargs_DeleteKarg() {
k := kargs.NewKargs([]byte("noval key=val"))
err := k.DeleteKarg("key")
if err != nil {
fmt.Printf("error: %v\n", err)
}
fmt.Println(k)
// Output:
// noval
}
func ExampleKargs_DeleteKargByValue() {
cmdline := `key=val1 key=val2 key=val3`
k := kargs.NewKargs([]byte(cmdline))
err := k.DeleteKargByValue("key", "val2")
if err != nil {
fmt.Printf("error: %v\n", err)
}
fmt.Println(k)
// Output:
// key=val1 key=val3
}
func ExampleKargs_GetKarg() {
cmdline := `nomodeset console=tty0,115200n8 console=ttyS0,115200n8 root=live:https://example.tld/image.squashfs`
k := kargs.NewKargs([]byte(cmdline))
// Get all values of console
console, _ := k.GetKarg("console")
fmt.Printf("console: %v\n", console)
// Get value of single key with a value
root, _ := k.GetKarg("root")
fmt.Printf("root: %v\n", root)
// Get value of single key with no value
nomodeset, _ := k.GetKarg("nomodeset")
fmt.Printf("nomodeset: %v\n", nomodeset)
// Output:
// console: [tty0,115200n8 ttyS0,115200n8]
// root: [live:https://example.tld/image.squashfs]
// nomodeset: []
}
func ExampleKargs_SetKarg_createReplace() {
k := kargs.NewKargsEmpty()
err := k.SetKarg("key", "")
if err != nil {
fmt.Printf("error: %v\n", err)
}
fmt.Println(k)
err = k.SetKarg("key", "val")
if err != nil {
fmt.Printf("error: %v\n", err)
}
fmt.Println(k)
// Output:
// key
// key=val
}
func ExampleKargs_SetKarg_replaceMultiple() {
cmdline := `console=tty0,115200n8 console=ttyS0,115200n8`
k := kargs.NewKargs([]byte(cmdline))
err := k.SetKarg("console", "tty1,115200n8")
if err != nil {
fmt.Printf("error: %v\n", err)
}
fmt.Println(k)
// Output:
// console=tty1,115200n8
}
func ExampleKargs_String() {
cmdline := `nomodeset root=live:https://example.tld/image.squashfs console=tty0,115200n8 console=ttyS0,115200n8 printk.devkmsg=ratelimit printk.time=1`
k := kargs.NewKargs([]byte(cmdline))
fmt.Println(k.String())
// Output:
// nomodeset root=live:https://example.tld/image.squashfs console=tty0,115200n8 console=ttyS0,115200n8 printk.devkmsg=ratelimit printk.time=1
}
func ExampleNewKargs() {
cmdline := `nomodeset root=live:https://example.tld/image.squashfs console=tty0,115200n8 console=ttyS0,115200n8 printk.devkmsg=ratelimit printk.time=1`
// Parse kernel command line arguments
k := kargs.NewKargs([]byte(cmdline))
fmt.Println(k)
// Get values
consoleVals, consoleSet := k.GetKarg("console")
fmt.Printf("console set: %v; values: %v\n", consoleSet, consoleVals)
// Get module flags
modvals := k.FlagsForModule("printk")
fmt.Printf("printk module values: %v\n", modvals)
// Output:
// nomodeset root=live:https://example.tld/image.squashfs console=tty0,115200n8 console=ttyS0,115200n8 printk.devkmsg=ratelimit printk.time=1
// console set: true; values: [tty0,115200n8 ttyS0,115200n8]
// printk module values: devkmsg=ratelimit time=1
}
func ExampleNewKargsEmpty() {
k := kargs.NewKargsEmpty()
fmt.Printf("%q\n", k)
err := k.SetKarg("console", "tty0,115200n8")
if err != nil {
fmt.Printf("error: %v\n", err)
}
fmt.Printf("%q\n", k)
// Output:
// ""
// "console=tty0,115200n8"
}