github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/bitbucket/yaml/image.go (about) 1 // Copyright 2022 Harness, Inc. 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 yaml 16 17 import "errors" 18 19 type ( 20 // Image configures the container image. 21 Image struct { 22 Name string 23 Username string 24 Password string 25 Email string 26 RunAsUser int 27 AWS *AWS 28 } 29 30 // temporary data structure for unmarshaling and 31 // marsaling images. 32 image struct { 33 Name string `yaml:"name,omitempty"` 34 Username string `yaml:"username,omitempty"` 35 Password string `yaml:"password,omitempty"` 36 Email string `yaml:"email,omitempty"` 37 RunAsUser int `yaml:"run-as-user,omitempty"` 38 AWS *AWS `yaml:"aws,omitempty"` 39 } 40 ) 41 42 // UnmarshalYAML implements the unmarshal interface. 43 func (v *Image) UnmarshalYAML(unmarshal func(interface{}) error) error { 44 var out1 string 45 var out2 *image 46 if err := unmarshal(&out1); err == nil { 47 v.Name = out1 48 return nil 49 } 50 if err := unmarshal(&out2); err == nil { 51 v.Name = out2.Name 52 v.Username = out2.Username 53 v.Password = out2.Password 54 v.Email = out2.Email 55 v.RunAsUser = out2.RunAsUser 56 v.AWS = out2.AWS 57 return nil 58 } 59 return errors.New("failed to unmarshal image") 60 } 61 62 // MarshalYAML implements the marshal interface. 63 func (v *Image) MarshalYAML() (interface{}, error) { 64 // marshal the image using short syntax if only the image 65 // name is provided. 66 if v.Username == "" && v.Password == "" && v.Email == "" && v.RunAsUser == 0 && v.AWS == nil { 67 return v.Name, nil 68 } 69 // else marshal the image using the long syntax. 70 return &image{ 71 Name: v.Name, 72 Username: v.Username, 73 Password: v.Password, 74 Email: v.Email, 75 RunAsUser: v.RunAsUser, 76 AWS: v.AWS, 77 }, nil 78 }