blob: 8d60d201cd6832f69ef62c32c24957facf50c1e9 [file] [log] [blame]
Matteo Scandoloa4285862020-12-01 18:10:10 -08001/*
2Copyright 2017 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 clientcmd
18
19// transformingStringValue implements pflag.Value to store string values,
20// allowing transforming them while being set
21type transformingStringValue struct {
22 target *string
23 transformer func(string) (string, error)
24}
25
26func newTransformingStringValue(val string, target *string, transformer func(string) (string, error)) *transformingStringValue {
27 *target = val
28 return &transformingStringValue{
29 target: target,
30 transformer: transformer,
31 }
32}
33
34func (t *transformingStringValue) Set(val string) error {
35 val, err := t.transformer(val)
36 if err != nil {
37 return err
38 }
39 *t.target = val
40 return nil
41}
42
43func (t *transformingStringValue) Type() string {
44 return "string"
45}
46
47func (t *transformingStringValue) String() string {
48 return string(*t.target)
49}