github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/circle/internal/orbs/saucelabs/saucelabs.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 saucelabs
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"strings"
    21  
    22  	circle "github.com/drone/go-convert/convert/circle/yaml"
    23  	harness "github.com/drone/spec/dist/go"
    24  )
    25  
    26  // Convert converts an Orb to a Harness step.
    27  // https://circleci.com/developer/orbs/orb/saucelabs/saucectl-run
    28  func Convert(command string, step *circle.Custom) *harness.Step {
    29  	switch command {
    30  	case "saucectl-run":
    31  		return convertRun(step)
    32  	default:
    33  		return nil
    34  	}
    35  }
    36  
    37  func convertRun(step *circle.Custom) *harness.Step {
    38  	var buf bytes.Buffer
    39  	buf.WriteString("saucectl")
    40  
    41  	if s, _ := step.Params["config-file"].(string); s != "" {
    42  		buf.WriteString(" -c " + s)
    43  	}
    44  
    45  	if s, _ := step.Params["region"].(string); s != "" {
    46  		buf.WriteString(" --region " + s)
    47  	}
    48  
    49  	if s, _ := step.Params["select-suite"].(string); s != "" {
    50  		buf.WriteString(" --select-suite" + s)
    51  	}
    52  
    53  	if s, _ := step.Params["sauceignore"].(string); s != "" {
    54  		buf.WriteString(" --sauceignore" + s)
    55  	}
    56  
    57  	if s, _ := step.Params["tunnel-name"].(string); s != "" {
    58  		buf.WriteString(" --tunnel-name" + s)
    59  	}
    60  
    61  	if s, _ := step.Params["tunnel-owner"].(string); s != "" {
    62  		buf.WriteString(" --tunnel-owner" + s)
    63  	}
    64  
    65  	if b, _ := step.Params["show-console-log"].(bool); b {
    66  		buf.WriteString(" --show-console-log")
    67  	}
    68  
    69  	if b, _ := step.Params["ccy"].(bool); b {
    70  		buf.WriteString(" --ccy")
    71  	}
    72  
    73  	if b, _ := step.Params["test-env-silent"].(bool); b {
    74  		buf.WriteString(" --test-env-silent")
    75  	}
    76  
    77  	if n, ok := step.Params["timeout"]; ok {
    78  		buf.WriteString(" --timeout " + fmt.Sprint(n))
    79  	}
    80  
    81  	if n, ok := step.Params["retries"]; ok {
    82  		buf.WriteString(" --retries " + fmt.Sprint(n))
    83  	}
    84  
    85  	cmds := []string{
    86  		"curl -L https://saucelabs.github.io/saucectl/install | bash -s -- -b /usr/local/bin",
    87  		buf.String(),
    88  	}
    89  
    90  	// add username and access key as environment
    91  	// variables.
    92  	envs := map[string]string{}
    93  	if s, _ := step.Params["sauce-username"].(string); s != "" {
    94  		envs["SAUCE_USERNAME"] = s
    95  	}
    96  	if s, _ := step.Params["sauce-access-key"].(string); s != "" {
    97  		envs["SAUCE_ACCESS_KEY"] = s
    98  	}
    99  
   100  	// convert user-defined environment variable string
   101  	// to an environment map.
   102  	if s, _ := step.Params["env"].(string); s != "" {
   103  		for _, env := range strings.Split(s, "\n") {
   104  			if parts := strings.SplitN(env, "=", 2); len(parts) == 2 {
   105  				a := parts[0]
   106  				b := parts[1]
   107  				envs[a] = b
   108  			}
   109  		}
   110  	}
   111  
   112  	// currently harness does not provide an option to
   113  	// change the step working directory, so we need to
   114  	// prepend the cd command to the script.
   115  	if s, _ := step.Params["working-directory"].(string); s != "" {
   116  		cmds = append([]string{"cd " + s}, cmds...)
   117  	}
   118  
   119  	return &harness.Step{
   120  		Name: "saucelabs",
   121  		Type: "background",
   122  		Spec: &harness.StepBackground{
   123  			Run:  strings.Join(cmds, "\n"),
   124  			Envs: envs,
   125  		},
   126  	}
   127  }