Skip to content

Commit

Permalink
[#82] Refactor command
Browse files Browse the repository at this point in the history
  • Loading branch information
juampynr committed Jul 5, 2022
1 parent 28ab0d2 commit 4a919fc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 36 deletions.
66 changes: 30 additions & 36 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,55 +14,31 @@ import (

var cfgFile string

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "drupal9ci",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
Short: "An interactive command to add Continuous Integration to a Drupal project",
}

// 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(setupScripts *scripts.SetupScripts) {
var selectedCIProvider string
var selectedCIProvider *string
var setupScript string
var err error

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
}
selectedCIProvider, err := getCIProvider(os.Args)
if err != nil {
fmt.Printf(err.Error())
return
}

switch selectedCIProvider {
case "Bitbucket":
switch *selectedCIProvider {
case scripts.Bitbucket:
setupScript = setupScripts.BitBucket
case "CircleCI":
case scripts.CircleCI:
setupScript = setupScripts.CircleCI
case "GitHub Actions":
case scripts.GithubActions:
setupScript = setupScripts.GitHubActions
case "GitLab CI":
case scripts.GitLabCI:
setupScript = setupScripts.GitLabCI
case "Travis CI":
case scripts.TravisCI:
setupScript = setupScripts.TravisCI
default:
fmt.Println("Unknown CI provider")
Expand All @@ -81,6 +57,24 @@ func Execute(setupScripts *scripts.SetupScripts) {
fmt.Println("output: ", string(res))
}

func getCIProvider(args []string) (*string, error) {
if len(args) > 1 {
return &args[1], nil
}
ciProviders := []string{"Bitbucket", "CircleCI", "GitHub Actions", "GitLab CI", "Travis CI"}

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

_, ciProvider, err := prompt.Run()
if err != nil {
return nil, fmt.Errorf("Prompt failed %s", err.Error())
}
return &ciProvider, nil
}

func init() {
cobra.OnInitialize(initConfig)

Expand Down
8 changes: 8 additions & 0 deletions scripts/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ package scripts

import _ "embed"

const (
Bitbucket = "Bitbucket"
CircleCI = "CircleCI"
GithubActions = "GitHub Actions"
GitLabCI = "GitLab CI"
TravisCI = "Travis CI"
)

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

Expand Down

0 comments on commit 4a919fc

Please sign in to comment.