github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/travis/yaml/addons_apt.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  // the alias details come from here:
    20  // https://github.com/travis-ci/apt-source-safelist/blob/master/ubuntu.json
    21  
    22  type Apt struct {
    23  	Enabled  bool         `yaml:"enabled,omitempty"`
    24  	Packages []string     `yaml:"packages,omitempty"`
    25  	Sources  []*AptSource `yaml:"sources,omitempty"`
    26  	Dist     string       `yaml:"dist,omitempty"`
    27  	Update   bool         `yaml:"bool,omitempty"`
    28  }
    29  
    30  // UnmarshalYAML implements the unmarshal interface.
    31  func (v *Apt) UnmarshalYAML(unmarshal func(interface{}) error) error {
    32  	var out1 bool
    33  	var out2 string
    34  	var out3 []string
    35  	var out4 = struct {
    36  		Enabled  *bool         `yaml:"enabled"`
    37  		Packages Stringorslice `yaml:"packages,omitempty"`
    38  		Package  Stringorslice `yaml:"package,omitempty"` // alias
    39  		Sources  AptSources    `yaml:"sources,omitempty"`
    40  		Source   AptSources    `yaml:"source,omitempty"` // alias
    41  		Dist     string        `yaml:"dist,omitempty"`
    42  		Update   bool          `yaml:"bool,omitempty"`
    43  	}{}
    44  	if err := unmarshal(&out1); err == nil {
    45  		v.Enabled = out1
    46  		return nil
    47  	}
    48  	if err := unmarshal(&out2); err == nil {
    49  		v.Enabled = true
    50  		v.Packages = append(v.Packages, out2)
    51  		return nil
    52  	}
    53  	if err := unmarshal(&out3); err == nil {
    54  		v.Enabled = true
    55  		v.Packages = append(v.Packages, out3...)
    56  		return nil
    57  	}
    58  	if err := unmarshal(&out4); err == nil {
    59  		v.Enabled = true
    60  		if out4.Enabled != nil {
    61  			v.Enabled = *out4.Enabled
    62  		}
    63  		v.Packages = out4.Packages
    64  		v.Sources = append(v.Sources, out4.Sources.Items...)
    65  		v.Dist = out4.Dist
    66  		v.Update = out4.Update
    67  		v.Packages = append(v.Packages, out4.Package...)
    68  		v.Sources = append(v.Sources, out4.Source.Items...)
    69  		return nil
    70  	}
    71  	return errors.New("failed to unmarshal apt")
    72  }
    73  
    74  type AptSources struct {
    75  	Items []*AptSource
    76  }
    77  
    78  // UnmarshalYAML implements the unmarshal interface.
    79  func (v *AptSources) UnmarshalYAML(unmarshal func(interface{}) error) error {
    80  	var out1 string
    81  	var out2 *AptSource
    82  	var out3 []*AptSource
    83  	if err := unmarshal(&out1); err == nil {
    84  		v.Items = append(v.Items, &AptSource{Alias: out1})
    85  		return nil
    86  	}
    87  	if err := unmarshal(&out2); err == nil {
    88  		v.Items = append(v.Items, out2)
    89  		return nil
    90  	}
    91  	if err := unmarshal(&out3); err == nil {
    92  		v.Items = append(v.Items, out3...)
    93  		return nil
    94  	}
    95  	return errors.New("failed to unmarshal apt source list")
    96  }
    97  
    98  type AptSource struct {
    99  	Alias           string `yaml:"alias,omitempty"`
   100  	Sourceline      string `yaml:"sourceline,omitempty"`
   101  	KeyURL          string `yaml:"key_url,omitempty"`
   102  	CanonicalKeyURL string `yaml:"canonical_key_url,omitempty"`
   103  }
   104  
   105  // UnmarshalYAML implements the unmarshal interface.
   106  func (v *AptSource) UnmarshalYAML(unmarshal func(interface{}) error) error {
   107  	var out1 string
   108  	var out2 = struct {
   109  		Alias           string `yaml:"alias,omitempty"`
   110  		Sourceline      string `yaml:"sourceline,omitempty"`
   111  		KeyURL          string `yaml:"key_url,omitempty"`
   112  		CanonicalKeyURL string `yaml:"canonical_key_url,omitempty"`
   113  	}{}
   114  	if err := unmarshal(&out1); err == nil {
   115  		v.Alias = out1
   116  		return nil
   117  	}
   118  	if err := unmarshal(&out2); err == nil {
   119  		v.Alias = out2.Alias
   120  		v.Sourceline = out2.Sourceline
   121  		v.KeyURL = out2.KeyURL
   122  		v.CanonicalKeyURL = out2.CanonicalKeyURL
   123  		return nil
   124  	}
   125  	return errors.New("failed to unmarshal apt source")
   126  }