github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/travis/addons.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 travis
    16  
    17  import (
    18  	"strings"
    19  
    20  	travis "github.com/drone/go-convert/convert/travis/yaml"
    21  	harness "github.com/drone/spec/dist/go"
    22  )
    23  
    24  func (d *Converter) convertAddons(ctx *context) []*harness.Step {
    25  	addons := ctx.config.Addons
    26  
    27  	// return if no addons defined
    28  	if addons == nil {
    29  		return nil
    30  	}
    31  
    32  	// aggregate list of steps
    33  	var dst []*harness.Step
    34  
    35  	// TODO support for addons.Chrome
    36  	// TODO support for addons.Firefox
    37  	// TODO support for addons.Hostname
    38  	// TODO support for addons.Hosts
    39  
    40  	if v := addons.Apt; v != nil {
    41  		// TODO only run apt step if `os` is linux
    42  		dst = append(dst, d.convertApt(v))
    43  	}
    44  	if v := addons.AptPackage; len(v) != 0 {
    45  		// TODO only run apt step if `os` is linux
    46  		dst = append(dst, d.convertAptPackage(v))
    47  	}
    48  	if v := addons.Artifacts; v != nil {
    49  		// TODO support addons.artifacts
    50  		// https://config.travis-ci.com/ref/job/addons/artifacts
    51  	}
    52  	if v := addons.Browserstack; v != nil {
    53  		// TODO support addons.browsertack
    54  		// https://config.travis-ci.com/ref/job/addons/browserstack
    55  	}
    56  	if v := addons.Codeclimate; v != nil {
    57  		// TODO support addons.codeclimate
    58  		// https://config.travis-ci.com/ref/job/addons/code_climate
    59  	}
    60  	if v := addons.Coverity; v != nil {
    61  		// TODO support addons.coverity
    62  		// https://config.travis-ci.com/ref/job/addons/coverity_scan
    63  	}
    64  	if v := addons.Homebrew; v != nil {
    65  		// TODO only run homebrew step if `os` is macos
    66  		dst = append(dst, d.convertHomebrew(v))
    67  	}
    68  	if v := addons.Sauce; v != nil {
    69  		// TODO support addons.sauce_connect
    70  		// https://config.travis-ci.com/ref/job/addons/sauce_connect
    71  	}
    72  	if v := addons.Snaps; v != nil {
    73  		// TODO support addons.snaps
    74  		// https://config.travis-ci.com/ref/job/addons/snaps
    75  	}
    76  	if v := addons.Sonarcloud; v != nil {
    77  		// TODO support addons.sonarcloud
    78  		// https://config.travis-ci.com/ref/job/addons/sonarcloud
    79  	}
    80  
    81  	// return if no steps added
    82  	if len(dst) == 0 {
    83  		return nil
    84  	}
    85  	return dst
    86  }
    87  
    88  func (d *Converter) convertApt(apt *travis.Apt) *harness.Step {
    89  	var lines []string
    90  	if apt.Enabled {
    91  		// TODO research behavior of `addons: { apt: true }`
    92  	}
    93  	if apt.Update {
    94  		lines = append(lines, "sudo apt-get -qq update")
    95  	}
    96  	for _, s := range apt.Sources {
    97  		// TODO support addon.apt.sources
    98  		if s == nil {
    99  			continue
   100  		}
   101  	}
   102  	for _, s := range apt.Packages {
   103  		lines = append(lines, "sudo apt-get -y install "+s)
   104  	}
   105  	if s := apt.Dist; s != "" {
   106  		// TODO support addon.apt.dist
   107  		// https: //docs.travis-ci.com/user/installing-dependencies/#adding-apt-packages
   108  	}
   109  
   110  	if len(lines) == 0 {
   111  		return nil
   112  	}
   113  
   114  	return &harness.Step{
   115  		Name: d.identifiers.Generate("apt"),
   116  		Type: "script",
   117  		Spec: &harness.StepExec{
   118  			Run: strings.Join(lines, "\n"),
   119  		},
   120  	}
   121  }
   122  
   123  func (d *Converter) convertAptPackage(packages travis.Stringorslice) *harness.Step {
   124  	lines := []string{"sudo apt-get -qq update"}
   125  	for _, s := range packages {
   126  		lines = append(lines, "sudo apt-get -y install "+s)
   127  	}
   128  	return &harness.Step{
   129  		Name: d.identifiers.Generate("apt_packages"),
   130  		Type: "script",
   131  		Spec: &harness.StepExec{
   132  			Run: strings.Join(lines, "\n"),
   133  		},
   134  	}
   135  }
   136  
   137  func (d *Converter) convertHomebrew(homebrew *travis.Homebrew) *harness.Step {
   138  	var lines []string
   139  	if homebrew.Update {
   140  		lines = append(lines, "brew update")
   141  	}
   142  	for _, v := range homebrew.Taps {
   143  		lines = append(lines, "brew tap "+v)
   144  	}
   145  	for _, v := range homebrew.Casks {
   146  		lines = append(lines, "brew cask install "+v)
   147  	}
   148  	for _, v := range homebrew.Packages {
   149  		lines = append(lines, "brew install "+v)
   150  	}
   151  	if v := homebrew.Brewfile; v != "" {
   152  		lines = append(lines, "brew bundle --file="+v)
   153  	}
   154  	if len(lines) == 0 {
   155  		return nil
   156  	}
   157  
   158  	return &harness.Step{
   159  		Name: d.identifiers.Generate("homebrew"),
   160  		Type: "script",
   161  		Spec: &harness.StepExec{
   162  			Run: strings.Join(lines, "\n"),
   163  		},
   164  	}
   165  }