Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2017 The Kubernetes Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package clientcmd |
| 18 | |
| 19 | // transformingStringValue implements pflag.Value to store string values, |
| 20 | // allowing transforming them while being set |
| 21 | type transformingStringValue struct { |
| 22 | target *string |
| 23 | transformer func(string) (string, error) |
| 24 | } |
| 25 | |
| 26 | func 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 | |
| 34 | func (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 | |
| 43 | func (t *transformingStringValue) Type() string { |
| 44 | return "string" |
| 45 | } |
| 46 | |
| 47 | func (t *transformingStringValue) String() string { |
| 48 | return string(*t.target) |
| 49 | } |