From 3f68983f2f3cc030920dc87607e45e25650fcac5 Mon Sep 17 00:00:00 2001 From: whiteCcinn <471113744@qq.com> Date: Fri, 21 May 2021 11:22:18 +0800 Subject: [PATCH 1/2] Add cobra examples --- _examples/cobra/.DS_Store | Bin 0 -> 6148 bytes _examples/cobra/.gitignore | 1 + _examples/cobra/cmd/confirm.go | 74 +++++++++++++++++ _examples/cobra/cmd/customPrompt.go | 94 ++++++++++++++++++++++ _examples/cobra/cmd/customSelect.go | 110 ++++++++++++++++++++++++++ _examples/cobra/cmd/prompt.go | 83 +++++++++++++++++++ _examples/cobra/cmd/promptDefault.go | 89 +++++++++++++++++++++ _examples/cobra/cmd/promptPassword.go | 82 +++++++++++++++++++ _examples/cobra/cmd/root.go | 85 ++++++++++++++++++++ _examples/cobra/cmd/select.go | 74 +++++++++++++++++ _examples/cobra/cmd/selectAdd.go | 86 ++++++++++++++++++++ _examples/cobra/go.mod | 12 +++ _examples/cobra/main.go | 22 ++++++ 13 files changed, 812 insertions(+) create mode 100644 _examples/cobra/.DS_Store create mode 100644 _examples/cobra/.gitignore create mode 100644 _examples/cobra/cmd/confirm.go create mode 100644 _examples/cobra/cmd/customPrompt.go create mode 100644 _examples/cobra/cmd/customSelect.go create mode 100644 _examples/cobra/cmd/prompt.go create mode 100644 _examples/cobra/cmd/promptDefault.go create mode 100644 _examples/cobra/cmd/promptPassword.go create mode 100644 _examples/cobra/cmd/root.go create mode 100644 _examples/cobra/cmd/select.go create mode 100644 _examples/cobra/cmd/selectAdd.go create mode 100644 _examples/cobra/go.mod create mode 100644 _examples/cobra/main.go diff --git a/_examples/cobra/.DS_Store b/_examples/cobra/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package cmd + +import ( + "fmt" + "github.com/manifoldco/promptui" + + "github.com/spf13/cobra" +) + +/* +➜ cobra git:(master) ✗ go run main.go confirm +continue confirm command?: y +confirm called + */ + +// confirmCmd represents the confirm command +var confirmCmd = &cobra.Command{ + Use: "confirm", + Short: "A brief description of your command", + Long: `A longer description that spans multiple lines and likely contains examples +and usage of using your command. 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.`, + Run: func(cmd *cobra.Command, args []string) { + prompt := promptui.Prompt{ + Label: "continue confirm command?", + IsConfirm: true, + } + + result, err := prompt.Run() + + if err != nil && err != promptui.ErrAbort { + fmt.Printf("Prompt failed %v\n", err) + return + } + + if result == "y" { + fmt.Println("confirm called") + } else { + fmt.Println("confirm cancel") + } + }, +} + +func init() { + rootCmd.AddCommand(confirmCmd) + + // Here you will define your flags and confirmuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // confirmCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // confirmCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/_examples/cobra/cmd/customPrompt.go b/_examples/cobra/cmd/customPrompt.go new file mode 100644 index 0000000..e84e67d --- /dev/null +++ b/_examples/cobra/cmd/customPrompt.go @@ -0,0 +1,94 @@ +/* +Copyright © 2021 NAME HERE + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package cmd + +import ( + "fmt" + "github.com/manifoldco/promptui" + "strconv" + + "github.com/spf13/cobra" +) + +type pepper struct { + Name string + HeatUnit int + Peppers int +} + +/* +➜ cobra git:(master) ✗ go run main.go customPrompt +Spicy Level 1 +You answered 1 +customPrompt called + */ + + +// customPromptCmd represents the customPrompt command +var customPromptCmd = &cobra.Command{ + Use: "customPrompt", + Short: "A brief description of your command", + Long: `A longer description that spans multiple lines and likely contains examples +and usage of using your command. 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.`, + Run: func(cmd *cobra.Command, args []string) { + validate := func(input string) error { + _, err := strconv.ParseFloat(input, 64) + return err + } + + templates := &promptui.PromptTemplates{ + Prompt: "{{ . }} ", + Valid: "{{ . | green }} ", + Invalid: "{{ . | red }} ", + Success: "{{ . | bold }} ", + } + + prompt := promptui.Prompt{ + Label: "Spicy Level", + Templates: templates, + Validate: validate, + } + + result, err := prompt.Run() + + if err != nil { + fmt.Printf("Prompt failed %v\n", err) + return + } + + fmt.Printf("You answered %s\n", result) + + fmt.Println("customPrompt called") + }, +} + +func init() { + rootCmd.AddCommand(customPromptCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // customPromptCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // customPromptCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/_examples/cobra/cmd/customSelect.go b/_examples/cobra/cmd/customSelect.go new file mode 100644 index 0000000..a5aebfe --- /dev/null +++ b/_examples/cobra/cmd/customSelect.go @@ -0,0 +1,110 @@ +/* +Copyright © 2021 NAME HERE + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package cmd + +import ( + "fmt" + "github.com/manifoldco/promptui" + "strings" + + "github.com/spf13/cobra" +) + +/* +➜ cobra git:(master) ✗ go run main.go customSelect +🌶 Bell Pepper +You choose number 1: Bell Pepper +customSelect called + */ + +// customSelectCmd represents the customSelect command +var customSelectCmd = &cobra.Command{ + Use: "customSelect", + Short: "A brief description of your command", + Long: `A longer description that spans multiple lines and likely contains examples +and usage of using your command. 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.`, + Run: func(cmd *cobra.Command, args []string) { + peppers := []pepper{ + {Name: "Bell Pepper", HeatUnit: 0, Peppers: 0}, + {Name: "Banana Pepper", HeatUnit: 100, Peppers: 1}, + {Name: "Poblano", HeatUnit: 1000, Peppers: 2}, + {Name: "Jalapeño", HeatUnit: 3500, Peppers: 3}, + {Name: "Aleppo", HeatUnit: 10000, Peppers: 4}, + {Name: "Tabasco", HeatUnit: 30000, Peppers: 5}, + {Name: "Malagueta", HeatUnit: 50000, Peppers: 6}, + {Name: "Habanero", HeatUnit: 100000, Peppers: 7}, + {Name: "Red Savina Habanero", HeatUnit: 350000, Peppers: 8}, + {Name: "Dragon’s Breath", HeatUnit: 855000, Peppers: 9}, + } + + templates := &promptui.SelectTemplates{ + Label: "{{ . }}?", + Active: "\U0001F336 {{ .Name | cyan }} ({{ .HeatUnit | red }})", + Inactive: " {{ .Name | cyan }} ({{ .HeatUnit | red }})", + Selected: "\U0001F336 {{ .Name | red | cyan }}", + Details: ` +--------- Pepper ---------- +{{ "Name:" | faint }} {{ .Name }} +{{ "Heat Unit:" | faint }} {{ .HeatUnit }} +{{ "Peppers:" | faint }} {{ .Peppers }}`, + } + + searcher := func(input string, index int) bool { + pepper := peppers[index] + name := strings.Replace(strings.ToLower(pepper.Name), " ", "", -1) + input = strings.Replace(strings.ToLower(input), " ", "", -1) + + return strings.Contains(name, input) + } + + prompt := promptui.Select{ + Label: "Spicy Level", + Items: peppers, + Templates: templates, + Size: 4, + Searcher: searcher, + } + + i, _, err := prompt.Run() + + if err != nil { + fmt.Printf("Prompt failed %v\n", err) + return + } + + fmt.Printf("You choose number %d: %s\n", i+1, peppers[i].Name) + + fmt.Println("customSelect called") + }, +} + +func init() { + rootCmd.AddCommand(customSelectCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // customSelectCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // customSelectCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/_examples/cobra/cmd/prompt.go b/_examples/cobra/cmd/prompt.go new file mode 100644 index 0000000..ffa53af --- /dev/null +++ b/_examples/cobra/cmd/prompt.go @@ -0,0 +1,83 @@ +/* +Copyright © 2021 NAME HERE + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package cmd + +import ( + "errors" + "fmt" + "github.com/manifoldco/promptui" + "strconv" + + "github.com/spf13/cobra" +) + +/* +➜ cobra git:(master) ✗ go run main.go prompt +Number: 2 +You choose "2" +prompt called + */ + +// promptCmd represents the prompt command +var promptCmd = &cobra.Command{ + Use: "prompt", + Short: "A brief description of your command", + Long: `A longer description that spans multiple lines and likely contains examples +and usage of using your command. 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.`, + Run: func(cmd *cobra.Command, args []string) { + validate := func(input string) error { + _, err := strconv.ParseFloat(input, 64) + if err != nil { + return errors.New("Invalid number") + } + return nil + } + + prompt := promptui.Prompt{ + Label: "Number", + Validate: validate, + } + + result, err := prompt.Run() + + if err != nil { + fmt.Printf("Prompt failed %v\n", err) + return + } + + fmt.Printf("You choose %q\n", result) + + fmt.Println("prompt called") + }, +} + +func init() { + rootCmd.AddCommand(promptCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // promptCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // promptCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/_examples/cobra/cmd/promptDefault.go b/_examples/cobra/cmd/promptDefault.go new file mode 100644 index 0000000..cea5058 --- /dev/null +++ b/_examples/cobra/cmd/promptDefault.go @@ -0,0 +1,89 @@ +/* +Copyright © 2021 NAME HERE + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package cmd + +import ( + "errors" + "fmt" + "github.com/manifoldco/promptui" + "os/user" + + "github.com/spf13/cobra" +) + +/* +➜ cobra git:(master) ✗ go run main.go promptDefault +Username: promptui-name +Your username is "promptui-name" +promptDefault called + */ + +// promptDefaultCmd represents the promptDefault command +var promptDefaultCmd = &cobra.Command{ + Use: "promptDefault", + Short: "A brief description of your command", + Long: `A longer description that spans multiple lines and likely contains examples +and usage of using your command. 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.`, + Run: func(cmd *cobra.Command, args []string) { + validate := func(input string) error { + if len(input) < 3 { + return errors.New("Username must have more than 3 characters") + } + return nil + } + + var username string + u, err := user.Current() + if err == nil { + username = u.Username + } + + prompt := promptui.Prompt{ + Label: "Username", + Validate: validate, + Default: username, + } + + result, err := prompt.Run() + + if err != nil { + fmt.Printf("Prompt failed %v\n", err) + return + } + + fmt.Printf("Your username is %q\n", result) + + fmt.Println("promptDefault called") + }, +} + +func init() { + rootCmd.AddCommand(promptDefaultCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // promptDefaultCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // promptDefaultCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/_examples/cobra/cmd/promptPassword.go b/_examples/cobra/cmd/promptPassword.go new file mode 100644 index 0000000..63e7c72 --- /dev/null +++ b/_examples/cobra/cmd/promptPassword.go @@ -0,0 +1,82 @@ +/* +Copyright © 2021 NAME HERE + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package cmd + +import ( + "errors" + "fmt" + "github.com/manifoldco/promptui" + + "github.com/spf13/cobra" +) + +/* +➜ cobra git:(master) ✗ go run main.go promptPassword +Password: ********* +Your password is "123123456" +promptPassword called + */ + +// promptPasswordCmd represents the promptPassword command +var promptPasswordCmd = &cobra.Command{ + Use: "promptPassword", + Short: "A brief description of your command", + Long: `A longer description that spans multiple lines and likely contains examples +and usage of using your command. 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.`, + Run: func(cmd *cobra.Command, args []string) { + validate := func(input string) error { + if len(input) < 6 { + return errors.New("Password must have more than 6 characters") + } + return nil + } + + prompt := promptui.Prompt{ + Label: "Password", + Validate: validate, + Mask: '*', + } + + result, err := prompt.Run() + + if err != nil { + fmt.Printf("Prompt failed %v\n", err) + return + } + + fmt.Printf("Your password is %q\n", result) + + fmt.Println("promptPassword called") + }, +} + +func init() { + rootCmd.AddCommand(promptPasswordCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // promptPasswordCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // promptPasswordCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/_examples/cobra/cmd/root.go b/_examples/cobra/cmd/root.go new file mode 100644 index 0000000..232b862 --- /dev/null +++ b/_examples/cobra/cmd/root.go @@ -0,0 +1,85 @@ +/* +Copyright © 2021 NAME HERE + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package cmd + +import ( + "fmt" + "os" + "github.com/spf13/cobra" + + homedir "github.com/mitchellh/go-homedir" + "github.com/spf13/viper" +) + +var cfgFile string + +// rootCmd represents the base command when called without any subcommands +var rootCmd = &cobra.Command{ + Use: "promptui-cobra-example", + 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() { + cobra.CheckErr(rootCmd.Execute()) +} + +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/.promptui-cobra-example.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 := homedir.Dir() + cobra.CheckErr(err) + + // Search config in home directory with name ".promptui-cobra-example" (without extension). + viper.AddConfigPath(home) + viper.SetConfigName(".promptui-cobra-example") + } + + 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()) + } +} diff --git a/_examples/cobra/cmd/select.go b/_examples/cobra/cmd/select.go new file mode 100644 index 0000000..ab48a0f --- /dev/null +++ b/_examples/cobra/cmd/select.go @@ -0,0 +1,74 @@ +/* +Copyright © 2021 NAME HERE + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package cmd + +import ( + "fmt" + "github.com/manifoldco/promptui" + + "github.com/spf13/cobra" +) + +/* +➜ cobra git:(master) ✗ go run main.go select +✔ Wednesday +You choose "Wednesday" +select called + */ + +// selectCmd represents the select command +var selectCmd = &cobra.Command{ + Use: "select", + Short: "A brief description of your command", + Long: `A longer description that spans multiple lines and likely contains examples +and usage of using your command. 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.`, + Run: func(cmd *cobra.Command, args []string) { + prompt := promptui.Select{ + Label: "Select Day", + Items: []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", + "Saturday", "Sunday"}, + } + + _, result, err := prompt.Run() + + if err != nil { + fmt.Printf("Prompt failed %v\n", err) + return + } + + fmt.Printf("You choose %q\n", result) + + fmt.Println("select called") + }, +} + +func init() { + rootCmd.AddCommand(selectCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // selectCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // selectCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/_examples/cobra/cmd/selectAdd.go b/_examples/cobra/cmd/selectAdd.go new file mode 100644 index 0000000..fc78580 --- /dev/null +++ b/_examples/cobra/cmd/selectAdd.go @@ -0,0 +1,86 @@ +/* +Copyright © 2021 NAME HERE + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package cmd + +import ( + "fmt" + "github.com/manifoldco/promptui" + + "github.com/spf13/cobra" +) + +/* +➜ cobra git:(master) ✗ go run main.go selectAdd +Other: add +✔ add +You choose add +selectAdd called + */ + +// selectAddCmd represents the selectAdd command +var selectAddCmd = &cobra.Command{ + Use: "selectAdd", + Short: "A brief description of your command", + Long: `A longer description that spans multiple lines and likely contains examples +and usage of using your command. 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.`, + Run: func(cmd *cobra.Command, args []string) { + items := []string{"Vim", "Emacs", "Sublime", "VSCode", "Atom"} + index := -1 + var result string + var err error + + for index < 0 { + prompt := promptui.SelectWithAdd{ + Label: "What's your text editor", + Items: items, + AddLabel: "Other", + } + + index, result, err = prompt.Run() + + if index == -1 { + items = append(items, result) + } + } + + if err != nil { + fmt.Printf("Prompt failed %v\n", err) + return + } + + fmt.Printf("You choose %s\n", result) + + fmt.Println("selectAdd called") + }, +} + +func init() { + rootCmd.AddCommand(selectAddCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // selectAddCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // selectAddCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/_examples/cobra/go.mod b/_examples/cobra/go.mod new file mode 100644 index 0000000..4892724 --- /dev/null +++ b/_examples/cobra/go.mod @@ -0,0 +1,12 @@ +module promptui-cobra-example + +go 1.15 + +require ( + github.com/mitchellh/go-homedir v1.1.0 + github.com/spf13/cobra v1.1.3 + github.com/spf13/viper v1.7.1 + github.com/manifoldco/promptui v0.0.1 +) + +replace github.com/manifoldco/promptui => ../../ diff --git a/_examples/cobra/main.go b/_examples/cobra/main.go new file mode 100644 index 0000000..2fc70ac --- /dev/null +++ b/_examples/cobra/main.go @@ -0,0 +1,22 @@ +/* +Copyright © 2021 NAME HERE + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package main + +import "promptui-cobra-example/cmd" + +func main() { + cmd.Execute() +} From 5084078ad6607436d3456b8077c4e943a7f011f5 Mon Sep 17 00:00:00 2001 From: whiteCcinn <471113744@qq.com> Date: Fri, 21 May 2021 11:23:29 +0800 Subject: [PATCH 2/2] Add cobra examples --- _examples/cobra/.DS_Store | Bin 6148 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 _examples/cobra/.DS_Store diff --git a/_examples/cobra/.DS_Store b/_examples/cobra/.DS_Store deleted file mode 100644 index 5008ddfcf53c02e82d7eee2e57c38e5672ef89f6..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0