summaryrefslogtreecommitdiff
path: root/cmd/podman/shared/intermediate.go
blob: 5aaac8687030170bb54ae9e59fc5db71641e7ba5 (plain)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
package shared

import (
	"github.com/containers/libpod/cmd/podman/cliconfig"
	"github.com/sirupsen/logrus"
)

/*
attention

in this file you will see alot of struct duplication.  this was done because people wanted a strongly typed
varlink mechanism.  this resulted in us creating this intermediate layer that allows us to take the input
from the cli and make an intermediate layer which can be transferred as strongly typed structures over a varlink
interface.

we intentionally avoided heavy use of reflection here because we were concerned about performance impacts to the
non-varlink intermediate layer generation.
*/

// GenericCLIResult describes the overall interface for dealing with
// the create command cli in both local and remote uses
type GenericCLIResult interface {
	IsSet() bool
	Name() string
	Value() interface{}
}

// CRStringSlice describes a string slice cli struct
type CRStringSlice struct {
	Val []string
	createResult
}

// CRString describes a string cli struct
type CRString struct {
	Val string
	createResult
}

// CRUint64 describes a uint64 cli struct
type CRUint64 struct {
	Val uint64
	createResult
}

// CRFloat64 describes a float64 cli struct
type CRFloat64 struct {
	Val float64
	createResult
}

//CRBool describes a bool cli struct
type CRBool struct {
	Val bool
	createResult
}

// CRInt64 describes an int64 cli struct
type CRInt64 struct {
	Val int64
	createResult
}

// CRUint describes a uint cli struct
type CRUint struct {
	Val uint
	createResult
}

// CRInt describes an int cli struct
type CRInt struct {
	Val int
	createResult
}

// CRStringArray describes a stringarray cli struct
type CRStringArray struct {
	Val []string
	createResult
}

type createResult struct {
	Flag    string
	Changed bool
}

// GenericCLIResults in the intermediate object between the cobra cli
// and createconfig
type GenericCLIResults struct {
	results   map[string]GenericCLIResult
	InputArgs []string
}

// IsSet returns a bool if the flag was changed
func (f GenericCLIResults) IsSet(flag string) bool {
	r := f.findResult(flag)
	if r == nil {
		return false
	}
	return r.IsSet()
}

// Value returns the value of the cli flag
func (f GenericCLIResults) Value(flag string) interface{} {
	r := f.findResult(flag)
	if r == nil {
		return ""
	}
	return r.Value()
}

func (f GenericCLIResults) findResult(flag string) GenericCLIResult {
	val, ok := f.results[flag]
	if ok {
		return val
	}
	logrus.Debugf("unable to find flag %s", flag)
	return nil
}

// Bool is a wrapper to get a bool value from GenericCLIResults
func (f GenericCLIResults) Bool(flag string) bool {
	r := f.findResult(flag)
	if r == nil {
		return false
	}
	return r.Value().(bool)
}

// String is a wrapper to get a string value from GenericCLIResults
func (f GenericCLIResults) String(flag string) string {
	r := f.findResult(flag)
	if r == nil {
		return ""
	}
	return r.Value().(string)
}

// Uint is a wrapper to get an uint value from GenericCLIResults
func (f GenericCLIResults) Uint(flag string) uint {
	r := f.findResult(flag)
	if r == nil {
		return 0
	}
	return r.Value().(uint)
}

// StringSlice is a wrapper to get a stringslice value from GenericCLIResults
func (f GenericCLIResults) StringSlice(flag string) []string {
	r := f.findResult(flag)
	if r == nil {
		return []string{}
	}
	return r.Value().([]string)
}

// StringArray is a wrapper to get a stringslice value from GenericCLIResults
func (f GenericCLIResults) StringArray(flag string) []string {
	r := f.findResult(flag)
	if r == nil {
		return []string{}
	}
	return r.Value().([]string)
}

// Uint64 is a wrapper to get an uint64 value from GenericCLIResults
func (f GenericCLIResults) Uint64(flag string) uint64 {
	r := f.findResult(flag)
	if r == nil {
		return 0
	}
	return r.Value().(uint64)
}

// Int64 is a wrapper to get an int64 value from GenericCLIResults
func (f GenericCLIResults) Int64(flag string) int64 {
	r := f.findResult(flag)
	if r == nil {
		return 0
	}
	return r.Value().(int64)
}

// Int is a wrapper to get an int value from GenericCLIResults
func (f GenericCLIResults) Int(flag string) int {
	r := f.findResult(flag)
	if r == nil {
		return 0
	}
	return r.Value().(int)
}

// Float64 is a wrapper to get an float64 value from GenericCLIResults
func (f GenericCLIResults) Float64(flag string) float64 {
	r := f.findResult(flag)
	if r == nil {
		return 0
	}
	return r.Value().(float64)
}

// Float64 is a wrapper to get an float64 value from GenericCLIResults
func (f GenericCLIResults) Changed(flag string) bool {
	r := f.findResult(flag)
	if r == nil {
		return false
	}
	return r.IsSet()
}

// IsSet ...
func (c CRStringSlice) IsSet() bool { return c.Changed }

// Name ...
func (c CRStringSlice) Name() string { return c.Flag }

// Value ...
func (c CRStringSlice) Value() interface{} { return c.Val }

// IsSet ...
func (c CRString) IsSet() bool { return c.Changed }

// Name ...
func (c CRString) Name() string { return c.Flag }

// Value ...
func (c CRString) Value() interface{} { return c.Val }

// IsSet ...
func (c CRUint64) IsSet() bool { return c.Changed }

// Name ...
func (c CRUint64) Name() string { return c.Flag }

// Value ...
func (c CRUint64) Value() interface{} { return c.Val }

// IsSet ...
func (c CRFloat64) IsSet() bool { return c.Changed }

// Name ...
func (c CRFloat64) Name() string { return c.Flag }

// Value ...
func (c CRFloat64) Value() interface{} { return c.Val }

// IsSet ...
func (c CRBool) IsSet() bool { return c.Changed }

// Name ...
func (c CRBool) Name() string { return c.Flag }

// Value ...
func (c CRBool) Value() interface{} { return c.Val }

// IsSet ...
func (c CRInt64) IsSet() bool { return c.Changed }

// Name ...
func (c CRInt64) Name() string { return c.Flag }

// Value ...
func (c CRInt64) Value() interface{} { return c.Val }

// IsSet ...
func (c CRUint) IsSet() bool { return c.Changed }

// Name ...
func (c CRUint) Name() string { return c.Flag }

// Value ...
func (c CRUint) Value() interface{} { return c.Val }

// IsSet ...
func (c CRInt) IsSet() bool { return c.Changed }

// Name ...
func (c CRInt) Name() string { return c.Flag }

// Value ...
func (c CRInt) Value() interface{} { return c.Val }

// IsSet ...
func (c CRStringArray) IsSet() bool { return c.Changed }

// Name ...
func (c CRStringArray) Name() string { return c.Flag }

// Value ...
func (c CRStringArray) Value() interface{} { return c.Val }

func newCreateResult(c *cliconfig.PodmanCommand, flag string) createResult {
	return createResult{
		Flag:    flag,
		Changed: c.IsSet(flag),
	}
}

func newCRStringSlice(c *cliconfig.PodmanCommand, flag string) CRStringSlice {
	return CRStringSlice{
		Val:          c.StringSlice(flag),
		createResult: newCreateResult(c, flag),
	}
}

func newCRString(c *cliconfig.PodmanCommand, flag string) CRString {
	return CRString{
		Val:          c.String(flag),
		createResult: newCreateResult(c, flag),
	}
}

func newCRUint64(c *cliconfig.PodmanCommand, flag string) CRUint64 {
	return CRUint64{
		Val:          c.Uint64(flag),
		createResult: newCreateResult(c, flag),
	}
}

func newCRFloat64(c *cliconfig.PodmanCommand, flag string) CRFloat64 {
	return CRFloat64{
		Val:          c.Float64(flag),
		createResult: newCreateResult(c, flag),
	}
}

func newCRBool(c *cliconfig.PodmanCommand, flag string) CRBool {
	return CRBool{
		Val:          c.Bool(flag),
		createResult: newCreateResult(c, flag),
	}
}

func newCRInt64(c *cliconfig.PodmanCommand, flag string) CRInt64 {
	return CRInt64{
		Val:          c.Int64(flag),
		createResult: newCreateResult(c, flag),
	}
}

func newCRUint(c *cliconfig.PodmanCommand, flag string) CRUint {
	return CRUint{
		Val:          c.Uint(flag),
		createResult: newCreateResult(c, flag),
	}
}

func newCRInt(c *cliconfig.PodmanCommand, flag string) CRInt {
	return CRInt{
		Val:          c.Int(flag),
		createResult: newCreateResult(c, flag),
	}
}

func newCRStringArray(c *cliconfig.PodmanCommand, flag string) CRStringArray {
	return CRStringArray{
		Val:          c.StringArray(flag),
		createResult: newCreateResult(c, flag),
	}
}

// NewIntermediateLayer creates a GenericCLIResults from a create or run cli-command
func NewIntermediateLayer(c *cliconfig.PodmanCommand, remote bool) GenericCLIResults {
	m := make(map[string]GenericCLIResult)

	m["add-host"] = newCRStringSlice(c, "add-host")
	m["annotation"] = newCRStringSlice(c, "annotation")
	m["attach"] = newCRStringSlice(c, "attach")
	m["blkio-weight"] = newCRString(c, "blkio-weight")
	m["blkio-weight-device"] = newCRStringSlice(c, "blkio-weight-device")
	m["cap-add"] = newCRStringSlice(c, "cap-add")
	m["cap-drop"] = newCRStringSlice(c, "cap-drop")
	m["cgroup-parent"] = newCRString(c, "cgroup-parent")
	m["cidfile"] = newCRString(c, "cidfile")
	m["conmon-pidfile"] = newCRString(c, "conmon-pidfile")
	m["cpu-period"] = newCRUint64(c, "cpu-period")
	m["cpu-quota"] = newCRInt64(c, "cpu-quota")
	m["cpu-rt-period"] = newCRUint64(c, "cpu-rt-period")
	m["cpu-rt-runtime"] = newCRInt64(c, "cpu-rt-runtime")
	m["cpu-shares"] = newCRUint64(c, "cpu-shares")
	m["cpus"] = newCRFloat64(c, "cpus")
	m["cpuset-cpus"] = newCRString(c, "cpuset-cpus")
	m["cpuset-mems"] = newCRString(c, "cpuset-mems")
	m["detach"] = newCRBool(c, "detach")
	m["detach-keys"] = newCRString(c, "detach-keys")
	m["device"] = newCRStringSlice(c, "device")
	m["device-read-bps"] = newCRStringSlice(c, "device-read-bps")
	m["device-read-iops"] = newCRStringSlice(c, "device-read-iops")
	m["device-write-bps"] = newCRStringSlice(c, "device-write-bps")
	m["device-write-iops"] = newCRStringSlice(c, "device-write-iops")
	m["dns"] = newCRStringSlice(c, "dns")
	m["dns-opt"] = newCRStringSlice(c, "dns-opt")
	m["dns-search"] = newCRStringSlice(c, "dns-search")
	m["entrypoint"] = newCRString(c, "entrypoint")
	m["env"] = newCRStringArray(c, "env")
	m["env-file"] = newCRStringSlice(c, "env-file")
	m["expose"] = newCRStringSlice(c, "expose")
	m["gidmap"] = newCRStringSlice(c, "gidmap")
	m["group-add"] = newCRStringSlice(c, "group-add")
	m["help"] = newCRBool(c, "help")
	m["healthcheck-command"] = newCRString(c, "health-cmd")
	m["healthcheck-interval"] = newCRString(c, "health-interval")
	m["healthcheck-retries"] = newCRUint(c, "health-retries")
	m["healthcheck-start-period"] = newCRString(c, "health-start-period")
	m["healthcheck-timeout"] = newCRString(c, "health-timeout")
	m["hostname"] = newCRString(c, "hostname")
	m["image-volume"] = newCRString(c, "image-volume")
	m["init"] = newCRBool(c, "init")
	m["init-path"] = newCRString(c, "init-path")
	m["interactive"] = newCRBool(c, "interactive")
	m["ip"] = newCRString(c, "ip")
	m["ipc"] = newCRString(c, "ipc")
	m["kernel-memory"] = newCRString(c, "kernel-memory")
	m["label"] = newCRStringArray(c, "label")
	m["label-file"] = newCRStringSlice(c, "label-file")
	m["log-driver"] = newCRString(c, "log-driver")
	m["log-opt"] = newCRStringSlice(c, "log-opt")
	m["mac-address"] = newCRString(c, "mac-address")
	m["memory"] = newCRString(c, "memory")
	m["memory-reservation"] = newCRString(c, "memory-reservation")
	m["memory-swap"] = newCRString(c, "memory-swap")
	m["memory-swappiness"] = newCRInt64(c, "memory-swappiness")
	m["name"] = newCRString(c, "name")
	m["net"] = newCRString(c, "net")
	m["network"] = newCRString(c, "network")
	m["no-hosts"] = newCRBool(c, "no-hosts")
	m["oom-kill-disable"] = newCRBool(c, "oom-kill-disable")
	m["oom-score-adj"] = newCRInt(c, "oom-score-adj")
	m["pid"] = newCRString(c, "pid")
	m["pids-limit"] = newCRInt64(c, "pids-limit")
	m["pod"] = newCRString(c, "pod")
	m["privileged"] = newCRBool(c, "privileged")
	m["publish"] = newCRStringSlice(c, "publish")
	m["publish-all"] = newCRBool(c, "publish-all")
	m["pull"] = newCRString(c, "pull")
	m["quiet"] = newCRBool(c, "quiet")
	m["read-only"] = newCRBool(c, "read-only")
	m["read-only-tmpfs"] = newCRBool(c, "read-only-tmpfs")
	m["restart"] = newCRString(c, "restart")
	m["rm"] = newCRBool(c, "rm")
	m["rootfs"] = newCRBool(c, "rootfs")
	m["security-opt"] = newCRStringArray(c, "security-opt")
	m["shm-size"] = newCRString(c, "shm-size")
	m["stop-signal"] = newCRString(c, "stop-signal")
	m["stop-timeout"] = newCRUint(c, "stop-timeout")
	m["storage-opt"] = newCRStringSlice(c, "storage-opt")
	m["subgidname"] = newCRString(c, "subgidname")
	m["subuidname"] = newCRString(c, "subuidname")
	m["sysctl"] = newCRStringSlice(c, "sysctl")
	m["systemd"] = newCRBool(c, "systemd")
	m["tmpfs"] = newCRStringArray(c, "tmpfs")
	m["tty"] = newCRBool(c, "tty")
	m["uidmap"] = newCRStringSlice(c, "uidmap")
	m["ulimit"] = newCRStringSlice(c, "ulimit")
	m["user"] = newCRString(c, "user")
	m["userns"] = newCRString(c, "userns")
	m["uts"] = newCRString(c, "uts")
	m["mount"] = newCRStringArray(c, "mount")
	m["volume"] = newCRStringArray(c, "volume")
	m["volumes-from"] = newCRStringSlice(c, "volumes-from")
	m["workdir"] = newCRString(c, "workdir")
	// global flag
	if !remote {
		m["authfile"] = newCRString(c, "authfile")
		m["cgroupns"] = newCRString(c, "cgroupns")
		m["env-host"] = newCRBool(c, "env-host")
		m["http-proxy"] = newCRBool(c, "http-proxy")
		m["trace"] = newCRBool(c, "trace")
		m["syslog"] = newCRBool(c, "syslog")
	}

	return GenericCLIResults{m, c.InputArgs}
}