Skip to content

Commit

Permalink
[#82] Initial command line interface
Browse files Browse the repository at this point in the history
Lists available CI providers and executes the respective script
  • Loading branch information
juampynr committed Jul 1, 2022
1 parent 519a879 commit fa5de26
Show file tree
Hide file tree
Showing 4 changed files with 627 additions and 0 deletions.
111 changes: 111 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package cmd

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

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) { },
}

// 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()

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

var setupScriptUrl string
switch result {
case "Bitbucket":
setupScriptUrl = "https://github.com/lullabot/drupal9ci/raw/master/setup-bitbucket.sh"
case "CircleCI":
setupScriptUrl = "https://github.com/lullabot/drupal9ci/raw/master/setup-circleci.sh"
case "GitHub Actions":
setupScriptUrl = "https://github.com/lullabot/drupal9ci/raw/master/setup-github-actions.sh"
case "GitLab CI":
setupScriptUrl = "https://github.com/lullabot/drupal9ci/raw/master/setup-gitlab-ci.sh"
case "Travis CI":
setupScriptUrl = "https://github.com/lullabot/drupal9ci/raw/master/setup-travis-ci.sh"
}

getScriptCmd := exec.Command("curl", "-L", setupScriptUrl)
execScriptCmd := exec.Command("bash")

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

execScriptCmd.Stdin = pipe

getScriptCmd.Start()

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

func init() {
cobra.OnInitialize(initConfig)

// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.drupal9ci.yaml)")

// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := os.UserHomeDir()
cobra.CheckErr(err)

// Search config in home directory with name ".drupal9ci" (without extension).
viper.AddConfigPath(home)
viper.SetConfigType("yaml")
viper.SetConfigName(".drupal9ci")
}

viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
}
}
27 changes: 27 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module drupal9ci

go 1.18

require (
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/manifoldco/promptui v0.9.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/cobra v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.12.0 // indirect
github.com/subosito/gotenv v1.3.0 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0 // indirect
)

0 comments on commit fa5de26

Please sign in to comment.