Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added examples with cobra #182

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions _examples/cobra/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go.sum
74 changes: 74 additions & 0 deletions _examples/cobra/cmd/confirm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Copyright © 2021 NAME HERE <EMAIL ADDRESS>

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")
}
94 changes: 94 additions & 0 deletions _examples/cobra/cmd/customPrompt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
Copyright © 2021 NAME HERE <EMAIL ADDRESS>

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")
}
110 changes: 110 additions & 0 deletions _examples/cobra/cmd/customSelect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
Copyright © 2021 NAME HERE <EMAIL ADDRESS>

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")
}
83 changes: 83 additions & 0 deletions _examples/cobra/cmd/prompt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
Copyright © 2021 NAME HERE <EMAIL ADDRESS>

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")
}
Loading