github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/circle/internal/orbs/browser-tools/browser_tools.go (about)

     1  package browser_tools
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	circle "github.com/drone/go-convert/convert/circle/yaml"
     8  	harness "github.com/drone/spec/dist/go"
     9  )
    10  
    11  func Convert(command, version string, step *circle.Custom) *harness.Step {
    12  	switch command {
    13  	case "":
    14  		return nil // not supported
    15  	case "install-browser-tools":
    16  		return convertInstallBrowserTools(step, version)
    17  	case "install-chrome":
    18  		return convertInstallChrome(step, version)
    19  	case "install-chromedriver":
    20  		return convertInstallChromeDriver(step, version)
    21  	case "install-firefox":
    22  		return convertInstallFirefox(step, version)
    23  	case "install-geckodriver":
    24  		return convertInstallGeckoDriver(step, version)
    25  	default:
    26  		return nil // not supported
    27  	}
    28  }
    29  
    30  func convertInstallBrowserTools(step *circle.Custom, version string) *harness.Step {
    31  	// default directories
    32  	firefoxInstallDir := "/usr/local/bin"
    33  	geckodriverInstallDir := "/usr/local/bin"
    34  	chromedriverInstallDir := "/usr/local/bin"
    35  
    36  	// default versions
    37  	firefoxVersion := "latest"
    38  	geckodriverVersion := "latest"
    39  	chromeVersion := "latest"
    40  
    41  	// default installation flags
    42  	installFirefox := true
    43  	installGeckodriver := true
    44  	installChrome := true
    45  	installChromedriver := true
    46  
    47  	// check parameters and override defaults if provided
    48  	if s, ok := step.Params["firefox-install-dir"].(string); ok && s != "" {
    49  		firefoxInstallDir = s
    50  	}
    51  	if s, ok := step.Params["geckodriver-install-dir"].(string); ok && s != "" {
    52  		geckodriverInstallDir = s
    53  	}
    54  	if s, ok := step.Params["chromedriver-install-dir"].(string); ok && s != "" {
    55  		chromedriverInstallDir = s
    56  	}
    57  	if s, ok := step.Params["firefox-version"].(string); ok && s != "" {
    58  		firefoxVersion = s
    59  	}
    60  	if s, ok := step.Params["geckodriver-version"].(string); ok && s != "" {
    61  		geckodriverVersion = s
    62  	}
    63  	if s, ok := step.Params["chrome-version"].(string); ok && s != "" {
    64  		chromeVersion = s
    65  	}
    66  	if b, ok := step.Params["install-firefox"].(bool); ok {
    67  		installFirefox = b
    68  	}
    69  	if b, ok := step.Params["install-geckodriver"].(bool); ok {
    70  		installGeckodriver = b
    71  	}
    72  	if b, ok := step.Params["install-chrome"].(bool); ok {
    73  		installChrome = b
    74  	}
    75  	if b, ok := step.Params["install-chromedriver"].(bool); ok {
    76  		installChromedriver = b
    77  	}
    78  
    79  	var runCommands []string
    80  	if installFirefox {
    81  		runCommands = append(runCommands, fmt.Sprintf("curl https://raw.githubusercontent.com/CircleCI-Public/browser-tools-orb/v%s/src/scripts/install-firefox.sh | bash", version))
    82  	}
    83  	if installGeckodriver {
    84  		runCommands = append(runCommands, fmt.Sprintf("curl https://raw.githubusercontent.com/CircleCI-Public/browser-tools-orb/v%s/src/scripts/install-geckodriver.sh | bash", version))
    85  	}
    86  	if installChrome {
    87  		runCommands = append(runCommands, fmt.Sprintf("curl https://raw.githubusercontent.com/CircleCI-Public/browser-tools-orb/v%s/src/scripts/install-chrome.sh | bash", version))
    88  	}
    89  	if installChromedriver {
    90  		runCommands = append(runCommands, fmt.Sprintf("curl https://raw.githubusercontent.com/CircleCI-Public/browser-tools-orb/v%s/src/scripts/install-chromedriver.sh | bash", version))
    91  	}
    92  	runCommand := strings.Join(runCommands, " && ")
    93  
    94  	return &harness.Step{
    95  		Name: "install_browser_tools",
    96  		Type: "script",
    97  		Spec: &harness.StepExec{
    98  			Run: runCommand,
    99  			Envs: map[string]string{
   100  				"ORB_PARAM_FIREFOX_INSTALL_DIR": firefoxInstallDir,
   101  				"ORB_PARAM_FIREFOX_VERSION":     firefoxVersion,
   102  				"ORB_PARAM_CHROME_VERSION":      chromeVersion,
   103  				"ORB_PARAM_DRIVER_INSTALL_DIR":  chromedriverInstallDir,
   104  				"ORB_PARAM_GECKO_INSTALL_DIR":   geckodriverInstallDir,
   105  				"ORB_PARAM_GECKO_VERSION":       geckodriverVersion,
   106  				"HOME":                          "/root", //required for firefox to install
   107  			},
   108  		},
   109  	}
   110  }
   111  
   112  func convertInstallChrome(step *circle.Custom, version string) *harness.Step {
   113  	channel := "stable" // default value
   114  	if c, ok := step.Params["channel"].(string); ok && c != "" {
   115  		channel = c
   116  	}
   117  
   118  	chromeVersion := "latest" // default value
   119  	if cv, ok := step.Params["chrome-version"].(string); ok && cv != "" {
   120  		chromeVersion = cv
   121  	}
   122  
   123  	replaceExisting := "0" // default value
   124  	if re, ok := step.Params["replace-existing"].(bool); ok && re {
   125  		replaceExisting = "1"
   126  	}
   127  
   128  	runCommand := fmt.Sprintf("curl https://raw.githubusercontent.com/CircleCI-Public/browser-tools-orb/v%s/src/scripts/install-chrome.sh | bash", version)
   129  
   130  	return &harness.Step{
   131  		Name: "install_chrome",
   132  		Type: "script",
   133  		Spec: &harness.StepExec{
   134  			Run: runCommand,
   135  			Envs: map[string]string{
   136  				"ORB_PARAM_CHANNEL":          channel,
   137  				"ORB_PARAM_CHROME_VERSION":   chromeVersion,
   138  				"ORB_PARAM_REPLACE_EXISTING": replaceExisting,
   139  			},
   140  		},
   141  	}
   142  }
   143  
   144  func convertInstallChromeDriver(step *circle.Custom, version string) *harness.Step {
   145  	installDir := "/usr/local/bin" // default value
   146  	if id, ok := step.Params["install-dir"].(string); ok && id != "" {
   147  		installDir = id
   148  	}
   149  
   150  	runCommand := fmt.Sprintf("curl https://raw.githubusercontent.com/CircleCI-Public/browser-tools-orb/v%s/src/scripts/install-chromedriver.sh | bash", version)
   151  
   152  	return &harness.Step{
   153  		Name: "install_chromedriver",
   154  		Type: "script",
   155  		Spec: &harness.StepExec{
   156  			Run: runCommand,
   157  			Envs: map[string]string{
   158  				"ORB_PARAM_DRIVER_INSTALL_DIR": installDir,
   159  			},
   160  		},
   161  	}
   162  }
   163  
   164  func convertInstallFirefox(step *circle.Custom, version string) *harness.Step {
   165  	installDir, _ := step.Params["install-dir"].(string)
   166  	firefoxVersion, _ := step.Params["version"].(string)
   167  
   168  	// Set the default values if they are not provided
   169  	if installDir == "" {
   170  		installDir = "/usr/local/bin"
   171  	}
   172  	if firefoxVersion == "" {
   173  		firefoxVersion = "latest"
   174  	}
   175  
   176  	runCommand := fmt.Sprintf("curl https://raw.githubusercontent.com/CircleCI-Public/browser-tools-orb/v%s/src/scripts/install-firefox.sh | bash", version)
   177  
   178  	return &harness.Step{
   179  		Name: "firefox_setup",
   180  		Type: "script",
   181  		Spec: &harness.StepExec{
   182  			Run: runCommand,
   183  			Envs: map[string]string{
   184  				"ORB_PARAM_FIREFOX_INSTALL_DIR": installDir,
   185  				"ORB_PARAM_FIREFOX_VERSION":     firefoxVersion,
   186  				"HOME":                          "/root", //required for firefox to install
   187  			},
   188  		},
   189  	}
   190  }
   191  
   192  func convertInstallGeckoDriver(step *circle.Custom, version string) *harness.Step {
   193  	installDir := "/usr/local/bin" // default value
   194  	if id, ok := step.Params["install-dir"].(string); ok && id != "" {
   195  		installDir = id
   196  	}
   197  
   198  	geckoVersion := "latest" // default value
   199  	if v, ok := step.Params["version"].(string); ok && v != "" {
   200  		geckoVersion = v
   201  	}
   202  
   203  	runCommand := fmt.Sprintf("curl https://raw.githubusercontent.com/CircleCI-Public/browser-tools-orb/v%s/src/scripts/install-geckodriver.sh | bash", version)
   204  
   205  	return &harness.Step{
   206  		Name: "install_geckodriver",
   207  		Type: "script",
   208  		Spec: &harness.StepExec{
   209  			Run: runCommand,
   210  			Envs: map[string]string{
   211  				"ORB_PARAM_GECKO_INSTALL_DIR": installDir,
   212  				"ORB_PARAM_GECKO_VERSION":     geckoVersion,
   213  			},
   214  		},
   215  	}
   216  }