github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/travis/yaml/cache.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 Cache struct {
    20  	Directories []string `yaml:"directories,omitempty"`
    21  	Apt         bool     `yaml:"apt,omitempty"`
    22  	Bundler     bool     `yaml:"bundler,omitempty"`
    23  	Cargo       bool     `yaml:"cargo,omitempty"`
    24  	Ccache      bool     `yaml:"ccache,omitempty"`
    25  	Cocoapods   bool     `yaml:"cocoapods,omitempty"`
    26  	Npm         bool     `yaml:"npm,omitempty"`
    27  	Packages    bool     `yaml:"packages,omitempty"`
    28  	Pip         bool     `yaml:"pip,omitempty"`
    29  	Yarn        bool     `yaml:"yarn,omitempty"`
    30  	Edge        bool     `yaml:"edge,omitempty"`
    31  	Branch      string   `yaml:"branch,omitempty"`
    32  	Timeout     int      `yaml:"timeout,omitempty"`
    33  }
    34  
    35  // UnmarshalYAML implements the unmarshal interface.
    36  func (v *Cache) UnmarshalYAML(unmarshal func(interface{}) error) error {
    37  	var out1 bool
    38  	var out2 string
    39  	var out3 = struct {
    40  		Directories Stringorslice `yaml:"directories"`
    41  		Apt         bool          `yaml:"apt"`
    42  		Bundler     bool          `yaml:"bundler"`
    43  		Cargo       bool          `yaml:"cargo"`
    44  		Ccache      bool          `yaml:"ccache"`
    45  		Cocoapods   bool          `yaml:"cocoapods"`
    46  		Npm         bool          `yaml:"npm"`
    47  		Packages    bool          `yaml:"packages"`
    48  		Pip         bool          `yaml:"pip"`
    49  		Yarn        bool          `yaml:"yarn"`
    50  		Edge        bool          `yaml:"edge"`
    51  		Branch      string        `yaml:"branch"`
    52  		Timeout     int           `yaml:"timeout"`
    53  	}{}
    54  
    55  	if err := unmarshal(&out1); err == nil {
    56  		v.Timeout = 3
    57  		return nil
    58  	}
    59  
    60  	if err := unmarshal(&out2); err == nil {
    61  		v.Timeout = 3
    62  		switch out2 {
    63  		case "apt":
    64  			v.Apt = true
    65  		case "bundler":
    66  			v.Bundler = true
    67  		case "cargo":
    68  			v.Cargo = true
    69  		case "ccache":
    70  			v.Ccache = true
    71  		case "cocoapods":
    72  			v.Cocoapods = true
    73  		case "npm":
    74  			v.Npm = true
    75  		case "packages":
    76  			v.Packages = true
    77  		case "pip":
    78  			v.Pip = true
    79  		case "yarn":
    80  			v.Yarn = true
    81  		case "edge":
    82  			v.Edge = true
    83  		}
    84  		return nil
    85  	}
    86  
    87  	if err := unmarshal(&out3); err == nil {
    88  		v.Directories = out3.Directories
    89  		v.Apt = out3.Apt
    90  		v.Bundler = out3.Bundler
    91  		v.Cargo = out3.Cargo
    92  		v.Ccache = out3.Ccache
    93  		v.Cocoapods = out3.Cocoapods
    94  		v.Npm = out3.Npm
    95  		v.Packages = out3.Packages
    96  		v.Pip = out3.Pip
    97  		v.Edge = out3.Edge
    98  		v.Branch = out3.Branch
    99  		v.Timeout = out3.Timeout
   100  		if v.Timeout == 0 {
   101  			v.Timeout = 3
   102  		}
   103  		return nil
   104  	}
   105  
   106  	return errors.New("failed to unmarshal cache")
   107  }