github.com/octohelm/wagon@v0.0.0-20240308040401-88662650dc0b/pkg/engine/plan/task/core/utils.go (about) 1 package core 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "golang.org/x/net/context" 7 "os" 8 "strconv" 9 10 "dagger.io/dagger" 11 ) 12 13 type ImagePullPrefixier interface { 14 ImagePullPrefix(name string) string 15 } 16 17 type imagePullPrefixierContext struct { 18 } 19 20 func ContextWithImagePullPrefixier(ctx context.Context, p ImagePullPrefixier) context.Context { 21 return context.WithValue(ctx, imagePullPrefixierContext{}, p) 22 } 23 24 func ImagePullPrefixierFromContext(ctx context.Context) ImagePullPrefixier { 25 if f, ok := ctx.Value(imagePullPrefixierContext{}).(ImagePullPrefixier); ok { 26 return f 27 } 28 return &imagePullPrefixierDiscord{} 29 } 30 31 type imagePullPrefixierDiscord struct { 32 } 33 34 func (imagePullPrefixierDiscord) ImagePullPrefix(name string) string { 35 return name 36 } 37 38 func DefaultPlatform(platform string) dagger.Platform { 39 if platform == "" { 40 arch := os.Getenv("BUILDKIT_ARCH") 41 if arch != "" { 42 return dagger.Platform(fmt.Sprintf("linux/%s", arch)) 43 } 44 } 45 return dagger.Platform(platform) 46 } 47 48 type StringOrBool struct { 49 String string 50 Bool *bool 51 } 52 53 func (StringOrBool) OneOf() []any { 54 return []any{ 55 "", 56 true, 57 } 58 } 59 60 func (s *StringOrBool) UnmarshalJSON(data []byte) error { 61 if len(data) > 0 && data[0] != '"' { 62 b := false 63 if err := json.Unmarshal(data, &b); err != nil { 64 return err 65 } 66 s.Bool = &b 67 return nil 68 } 69 return json.Unmarshal(data, &s.String) 70 } 71 72 func (s StringOrBool) MarshalJSON() ([]byte, error) { 73 if s.Bool != nil { 74 return []byte(strconv.FormatBool(*s.Bool)), nil 75 } 76 return []byte(strconv.Quote(s.String)), nil 77 } 78 79 type StringOrSlice struct { 80 Values []string 81 } 82 83 func (StringOrSlice) OneOf() []any { 84 return []any{ 85 "", 86 []string{}, 87 } 88 } 89 90 func (s *StringOrSlice) UnmarshalJSON(data []byte) error { 91 if len(data) > 0 && data[0] == '"' { 92 b := "" 93 if err := json.Unmarshal(data, &b); err != nil { 94 return err 95 } 96 s.Values = []string{b} 97 return nil 98 } 99 return json.Unmarshal(data, &s.Values) 100 } 101 102 func (s StringOrSlice) MarshalJSON() ([]byte, error) { 103 if len(s.Values) == 1 { 104 return []byte(s.Values[0]), nil 105 } 106 return json.Marshal(s.Values) 107 }