Skip to content

Commit

Permalink
[#82] Refactor command in smaller functions
Browse files Browse the repository at this point in the history
  • Loading branch information
juampynr committed Jul 5, 2022
1 parent 857be58 commit 7620e39
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
35 changes: 14 additions & 21 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,43 +20,36 @@ var rootCmd = &cobra.Command{
}

func Execute(setupScripts *scripts.SetupScripts) {
var selectedCIProvider *string
var setupScript string

selectedCIProvider, err := getCIProvider(os.Args)
if err != nil {
fmt.Printf(err.Error())
return
}

switch *selectedCIProvider {
case scripts.Bitbucket:
setupScript = setupScripts.BitBucket
case scripts.CircleCI:
setupScript = setupScripts.CircleCI
case scripts.GithubActions:
setupScript = setupScripts.GitHubActions
case scripts.GitLabCI:
setupScript = setupScripts.GitLabCI
case scripts.TravisCI:
setupScript = setupScripts.TravisCI
default:
fmt.Println("Unknown CI provider")
setupScript, err := scripts.MapCIProviderToScript(selectedCIProvider, setupScripts)
if err != nil {
fmt.Printf(err.Error())
return
}

stringReader := strings.NewReader(setupScript)
stringReadCloser := io.NopCloser(stringReader)
execScriptCmd := exec.Command("bash")
fmt.Println("This might take a few seconds...")

execScriptCmd.Stdin = stringReadCloser
res, err := execScriptCmd.CombinedOutput()
res, err := executeCIInstallerScript(setupScript)
if err != nil {
fmt.Println("error executing script: ", err.Error())
}
fmt.Println("output: ", string(res))
}

func executeCIInstallerScript(setupScript *string) ([]byte, error) {
stringReader := strings.NewReader(*setupScript)
stringReadCloser := io.NopCloser(stringReader)
execScriptCmd := exec.Command("bash")

execScriptCmd.Stdin = stringReadCloser
return execScriptCmd.CombinedOutput()
}

func getCIProvider(args []string) (*string, error) {
if len(args) > 1 {
return &args[1], nil
Expand Down
27 changes: 26 additions & 1 deletion scripts/scripts.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package scripts

import _ "embed"
import (
_ "embed"
"fmt"
)

const (
Bitbucket = "Bitbucket"
Expand Down Expand Up @@ -42,3 +45,25 @@ func LoadSetupScripts() *SetupScripts {
TravisCI: setupTravisCI,
}
}

func MapCIProviderToScript(ciProvider *string, setupScripts *SetupScripts) (*string, error) {
var setupScript *string
var err error

switch *ciProvider {
case Bitbucket:
setupScript = &setupScripts.BitBucket
case CircleCI:
setupScript = &setupScripts.CircleCI
case GithubActions:
setupScript = &setupScripts.GitHubActions
case GitLabCI:
setupScript = &setupScripts.GitLabCI
case TravisCI:
setupScript = &setupScripts.TravisCI
default:
err = fmt.Errorf("Unknown CI provider")
}

return setupScript, err
}

0 comments on commit 7620e39

Please sign in to comment.