github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/travis/yaml/addons_homebrew.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 Homebrew struct {
    20  	Update   bool     `yaml:"update,omitempty"`
    21  	Packages []string `yaml:"packages,omitempty"`
    22  	Casks    []string `yaml:"casks,omitempty"`
    23  	Taps     []string `yaml:"taps,omitempty"`
    24  	Brewfile string   `yaml:"brewfile,omitempty"` // string or bool
    25  }
    26  
    27  // UnmarshalYAML implements the unmarshal interface.
    28  func (v *Homebrew) UnmarshalYAML(unmarshal func(interface{}) error) error {
    29  	var out1 bool
    30  	var out2 string
    31  	var out3 []string
    32  	var out4 = struct {
    33  		Update   bool          `yaml:"update,omitempty"`
    34  		Packages Stringorslice `yaml:"packages,omitempty"`
    35  		Casks    Stringorslice `yaml:"casks,omitempty"`
    36  		Taps     Stringorslice `yaml:"taps,omitempty"`
    37  		Brewfile interface{}   `yaml:"brewfile,omitempty"` // string or bool
    38  	}{}
    39  
    40  	if err := unmarshal(&out1); err == nil {
    41  		v.Update = out1
    42  		return nil
    43  	}
    44  	if err := unmarshal(&out2); err == nil {
    45  		v.Update = true
    46  		v.Packages = []string{out2}
    47  		return nil
    48  	}
    49  	if err := unmarshal(&out3); err == nil {
    50  		v.Update = true
    51  		v.Packages = out3
    52  		return nil
    53  	}
    54  	if err := unmarshal(&out4); err == nil {
    55  		v.Update = out4.Update
    56  		v.Packages = out4.Packages
    57  		v.Casks = out4.Casks
    58  		v.Taps = out4.Taps
    59  		switch vv := out4.Brewfile.(type) {
    60  		case bool:
    61  			v.Brewfile = "Brewfile"
    62  		case string:
    63  			v.Brewfile = vv
    64  		}
    65  		return nil
    66  	}
    67  
    68  	return errors.New("failed to unmarshal homebrew")
    69  }