github.com/GoogleContainerTools/kpt@v1.0.0-beta.50.0.20240520170205-c25345ffcbee/internal/fnruntime/imagepullpolicy.go (about) 1 // Copyright 2021 The kpt Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package fnruntime 16 17 import ( 18 "fmt" 19 "strings" 20 21 "github.com/spf13/pflag" 22 ) 23 24 type ImagePullPolicy string 25 26 const ( 27 AlwaysPull ImagePullPolicy = "Always" 28 IfNotPresentPull ImagePullPolicy = "IfNotPresent" 29 NeverPull ImagePullPolicy = "Never" 30 ) 31 32 var allImagePullPolicy = []ImagePullPolicy{ 33 AlwaysPull, 34 IfNotPresentPull, 35 NeverPull, 36 } 37 38 // ImagePullPolicy can be used in pflag 39 var _ pflag.Value = ((*ImagePullPolicy)(nil)) 40 41 // String implements pflag.Value and fmt.Stringer 42 func (e *ImagePullPolicy) String() string { 43 return string(*e) 44 } 45 46 // Set implements pflag.Value 47 func (e *ImagePullPolicy) Set(v string) error { 48 l := strings.ToLower(v) 49 for _, c := range allImagePullPolicy { 50 s := strings.ToLower(string(c)) 51 if s == l { 52 *e = c 53 return nil 54 } 55 } 56 return fmt.Errorf("must must be one of " + strings.Join(e.AllStrings(), ", ")) 57 } 58 59 func (e *ImagePullPolicy) AllStrings() []string { 60 var allStrings []string 61 for _, c := range allImagePullPolicy { 62 allStrings = append(allStrings, string(c)) 63 } 64 return allStrings 65 } 66 67 // HelpAllowedValues builds help text for the allowed values 68 func (e *ImagePullPolicy) HelpAllowedValues() string { 69 return "(one of " + strings.Join(e.AllStrings(), ", ") + ")" 70 } 71 72 // Type implements pflag.Value 73 func (e *ImagePullPolicy) Type() string { 74 return "ImagePullPolicy" 75 }