From 4b72f228d3cf28bf9ba2122ff9b36f25983b5484 Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Sat, 31 Aug 2024 21:13:46 +0200 Subject: [PATCH] prompting for command variants in config --- cmd/save.go | 6 ++++ util/util.go | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 util/util.go diff --git a/cmd/save.go b/cmd/save.go index 8fc1166..e93efb5 100644 --- a/cmd/save.go +++ b/cmd/save.go @@ -22,6 +22,12 @@ var saveCmd = &cobra.Command{ fmt.Print("Command? ") fmt.Scanln(&command) + fmt.Println("\nPrompting 'set' variants:") + set := util.PromptVariants() + + fmt.Println("\nPrompting 'unset' variants:") + unset := util.PromptVariants() + config := util.Config{Name: name, Cmd: command, Set: set, Unset: unset} fmt.Println("\n\nPlease confirm the following data:") diff --git a/util/util.go b/util/util.go new file mode 100644 index 0000000..bf2876d --- /dev/null +++ b/util/util.go @@ -0,0 +1,78 @@ +package util + +import ( + "fmt" + "strings" +) + +func promptArguments() []string { + var args []string = []string{} + var count int + +argumentLoop: + for true { + var arg string + fmt.Printf("Arg #%d? ", count+1) + fmt.Scanln(&arg) + + if arg == "" { + break argumentLoop + } + + args = append(args, arg) + count++ + } + + return args +} + +func promptType() VariantType { + var variantType VariantType + +typeLoop: + for true { + var input string + fmt.Print("Type? ") + fmt.Scanln(&input) + + switch strings.ToLower(input) { + case "": + variantType = TEXT + break typeLoop + case "variable": + variantType = VARIABLE + break typeLoop + default: + fmt.Println("Given input not convertable to type!") + } + } + + return variantType +} + +func PromptVariants() []*Variant { + var variants []*Variant + var count int + +variantLoop: + for true { + fmt.Printf("Prompting 'args' for variant #%d:\n", count+1) + args := promptArguments() + + var equator string + fmt.Print("Equator? ") + fmt.Scanln(&equator) + + if strings.ToLower(equator) == "skip" { + break variantLoop + } + + variantType := promptType() + + variant := Variant{Arguments: args, Type: variantType, Equator: equator} + variants = append(variants, &variant) + count++ + } + + return variants +}