Skip to content

Commit

Permalink
[#82] Embed scripts in binary
Browse files Browse the repository at this point in the history
  • Loading branch information
juampynr committed Jul 4, 2022
1 parent 3e9f9c3 commit a633d18
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 44 deletions.
33 changes: 20 additions & 13 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# PHP CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-php/ for more details
#
version: 2
version: 2.1
orbs:
go: circleci/go@1.18
jobs:
build:
docker:
Expand All @@ -13,14 +11,23 @@ jobs:
steps:
- checkout

- uses: actions/setup-go@v3
with:
go-version: '^1.18'

- run:
name: Build application
command: |
go build
- run:
name: Build Drupal skeleton with Composer Drupal Project
command: |
composer create-project drupal-composer/drupal-project:9.x-dev drupal --stability dev --no-interaction
name: Build Drupal skeleton with Composer Drupal Project
command: |
composer create-project drupal-composer/drupal-project:9.x-dev drupal --stability dev --no-interaction
- run:
name: Run setup script
command: |
cp setup-circleci.sh drupal
cd drupal
./setup-circleci.sh
name: Copy application binary and run it
command: |
cp drupal9ci drupal/
cd drupal
./drupal9ci BitBucket
59 changes: 34 additions & 25 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package cmd

import (
"drupal9ci/scripts"
"fmt"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"io"
"os"
"os/exec"
"strings"
)

var cfgFile string
Expand All @@ -28,43 +31,49 @@ to quickly create a Cobra application.`,

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
prompt := promptui.Select{
Label: "Select CI provider",
Items: []string{"Bitbucket", "CircleCI", "GitHub Actions", "GitLab CI", "Travis CI"},
}

_, result, err := prompt.Run()
func Execute(setupScripts *scripts.SetupScripts) {
var selectedCIProvider string
var setupScript string
var err error

if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
if len(os.Args) > 1 {
selectedCIProvider = os.Args[1]
} else {
ciProviders := []string{"Bitbucket", "CircleCI", "GitHub Actions", "GitLab CI", "Travis CI"}

prompt := promptui.Select{
Label: "Select CI provider",
Items: ciProviders,
}

_, selectedCIProvider, err = prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}
}

var setupScriptUrl string
switch result {
switch selectedCIProvider {
case "Bitbucket":
setupScriptUrl = "https://github.com/lullabot/drupal9ci/raw/master/setup-bitbucket.sh"
setupScript = setupScripts.BitBucket
case "CircleCI":
setupScriptUrl = "https://github.com/lullabot/drupal9ci/raw/master/setup-circleci.sh"
setupScript = setupScripts.CircleCI
case "GitHub Actions":
setupScriptUrl = "https://github.com/lullabot/drupal9ci/raw/master/setup-github-actions.sh"
setupScript = setupScripts.GitHubActions
case "GitLab CI":
setupScriptUrl = "https://github.com/lullabot/drupal9ci/raw/master/setup-gitlab-ci.sh"
setupScript = setupScripts.GitLabCI
case "Travis CI":
setupScriptUrl = "https://github.com/lullabot/drupal9ci/raw/master/setup-travis-ci.sh"
setupScript = setupScripts.TravisCI
default:
fmt.Println("Unknown CI provider")
return
}

getScriptCmd := exec.Command("curl", "-L", setupScriptUrl)
stringReader := strings.NewReader(setupScript)
stringReadCloser := io.NopCloser(stringReader)
execScriptCmd := exec.Command("bash")

pipe, err := getScriptCmd.StdoutPipe()
defer pipe.Close()

execScriptCmd.Stdin = pipe

getScriptCmd.Start()

execScriptCmd.Stdin = stringReadCloser
res, err := execScriptCmd.CombinedOutput()
if err != nil {
fmt.Println("error executing script: ", err.Error())
Expand Down
12 changes: 6 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package main

import "drupal9ci/cmd"
import (
"drupal9ci/cmd"
"drupal9ci/scripts"
_ "embed"
)

func main() {
cmd.Execute()
cmd.Execute(scripts.LoadSetupScripts())
}
36 changes: 36 additions & 0 deletions scripts/scripts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package scripts

import _ "embed"

//go:embed setup-bitbucket.sh
var setupBitbucket string

//go:embed setup-circleci.sh
var setupCircleCI string

//go:embed setup-github-actions.sh
var setupGitHubActions string

//go:embed setup-gitlab-ci.sh
var setupGitLabCI string

//go:embed setup-travis-ci.sh
var setupTravisCI string

type SetupScripts struct {
BitBucket string
CircleCI string
GitHubActions string
GitLabCI string
TravisCI string
}

func LoadSetupScripts() *SetupScripts {
return &SetupScripts{
BitBucket: setupBitbucket,
CircleCI: setupCircleCI,
GitHubActions: setupGitHubActions,
GitLabCI: setupGitLabCI,
TravisCI: setupTravisCI,
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit a633d18

Please sign in to comment.