Skip to content

Commit

Permalink
Changes arg parsing to use getopt library
Browse files Browse the repository at this point in the history
Also  adds a help flag,
and enables single argument switching directly from command lin and not just from the menu

Closes: #16, #17
  • Loading branch information
ajjl committed Jun 22, 2018
1 parent 37ccf70 commit d597c29
Showing 1 changed file with 47 additions and 23 deletions.
70 changes: 47 additions & 23 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (

"github.com/manifoldco/promptui"
lib "github.com/warrensbox/terraform-switcher/lib"
"github.com/pborman/getopt"
"regexp"
)

const (
Expand All @@ -44,38 +46,60 @@ var version = "0.0.1\n"
// )

func main() {
versionFlag := getopt.BoolLong("version", 'v', "displays the version of tfswitch", "something")
helpFlag := getopt.BoolLong("help", 'h', "displays help message", "something")
_ = versionFlag

args := os.Args
getopt.Parse()
args := getopt.Args()

if len(os.Args) > 1 {
switch os := args[1]; os {
case "--version":
fmt.Println(version)
case "version":
fmt.Println(version)
case "-v":
fmt.Println(version)
}
if *versionFlag {
fmt.Println(version)
} else if *helpFlag {
UsageMessage()
} else {

tflist, _ := lib.GetTFList(hashiURL)
if len(args) == 1 {
semver_regex := regexp.MustCompile(`\A\d+(\.\d+){2}\z`)
if semver_regex.MatchString(args[0]) {
requested_version := args[0]
lib.Install(requested_version)
} else {
fmt.Println("Not a valid terraform version")
fmt.Println("Args must be a valid terraform version")
UsageMessage()
}

/* prompt user to select version of terraform */
prompt := promptui.Select{
Label: "Select Terraform version",
Items: tflist,
}
} else if len(args) == 0 {

_, tfversion, errPrompt := prompt.Run()
// os.Exit(-1)
tflist, _ := lib.GetTFList(hashiURL)

if errPrompt != nil {
log.Printf("Prompt failed %v\n", errPrompt)
os.Exit(1)
}
/* prompt user to select version of terraform */
prompt := promptui.Select{
Label: "Select Terraform version",
Items: tflist,
}

_, tfversion, errPrompt := prompt.Run()

fmt.Printf("Terraform version %q selected\n", tfversion)
if errPrompt != nil {
log.Printf("Prompt failed %v\n", errPrompt)
os.Exit(1)
}

lib.Install(tfversion)
fmt.Printf("Terraform version %q selected\n", tfversion)

lib.Install(tfversion)
} else {
UsageMessage()
}
}
}


func UsageMessage() {
fmt.Println("\n\nInvalid Selection")
getopt.PrintUsage(os.Stderr)
fmt.Println("Supply the terraform version as an argument, or choose from a menu")
}

0 comments on commit d597c29

Please sign in to comment.