blob: bc6867748ba4ed56c2caea341fdd1555493bb079 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001/*
2Copyright 2016 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package flag
18
19import (
20 "errors"
21 "flag"
22 "strings"
23)
24
25// NamedCertKey is a flag value parsing "certfile,keyfile" and "certfile,keyfile:name,name,name".
26type NamedCertKey struct {
27 Names []string
28 CertFile, KeyFile string
29}
30
31var _ flag.Value = &NamedCertKey{}
32
33func (nkc *NamedCertKey) String() string {
34 s := nkc.CertFile + "," + nkc.KeyFile
35 if len(nkc.Names) > 0 {
36 s = s + ":" + strings.Join(nkc.Names, ",")
37 }
38 return s
39}
40
41func (nkc *NamedCertKey) Set(value string) error {
42 cs := strings.SplitN(value, ":", 2)
43 var keycert string
44 if len(cs) == 2 {
45 var names string
46 keycert, names = strings.TrimSpace(cs[0]), strings.TrimSpace(cs[1])
47 if names == "" {
48 return errors.New("empty names list is not allowed")
49 }
50 nkc.Names = nil
51 for _, name := range strings.Split(names, ",") {
52 nkc.Names = append(nkc.Names, strings.TrimSpace(name))
53 }
54 } else {
55 nkc.Names = nil
56 keycert = strings.TrimSpace(cs[0])
57 }
58 cs = strings.Split(keycert, ",")
59 if len(cs) != 2 {
60 return errors.New("expected comma separated certificate and key file paths")
61 }
62 nkc.CertFile = strings.TrimSpace(cs[0])
63 nkc.KeyFile = strings.TrimSpace(cs[1])
64 return nil
65}
66
67func (*NamedCertKey) Type() string {
68 return "namedCertKey"
69}
70
71// NamedCertKeyArray is a flag value parsing NamedCertKeys, each passed with its own
72// flag instance (in contrast to comma separated slices).
73type NamedCertKeyArray struct {
74 value *[]NamedCertKey
75 changed bool
76}
77
78var _ flag.Value = &NamedCertKey{}
79
80// NewNamedKeyCertArray creates a new NamedCertKeyArray with the internal value
81// pointing to p.
82func NewNamedCertKeyArray(p *[]NamedCertKey) *NamedCertKeyArray {
83 return &NamedCertKeyArray{
84 value: p,
85 }
86}
87
88func (a *NamedCertKeyArray) Set(val string) error {
89 nkc := NamedCertKey{}
90 err := nkc.Set(val)
91 if err != nil {
92 return err
93 }
94 if !a.changed {
95 *a.value = []NamedCertKey{nkc}
96 a.changed = true
97 } else {
98 *a.value = append(*a.value, nkc)
99 }
100 return nil
101}
102
103func (a *NamedCertKeyArray) Type() string {
104 return "namedCertKey"
105}
106
107func (a *NamedCertKeyArray) String() string {
108 nkcs := make([]string, 0, len(*a.value))
109 for i := range *a.value {
110 nkcs = append(nkcs, (*a.value)[i].String())
111 }
112 return "[" + strings.Join(nkcs, ";") + "]"
113}