Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cmd
- import (
- "errors"
- "fmt"
- "io"
- "os"
- "github.com/spf13/cobra"
- )
- func NewCommandSample(out, errOut io.Writer) *cobra.Command {
- cmd := &cobra.Command{
- Use: "sample",
- Short: "A brief description of your command",
- Run: func(cmd *cobra.Command, args []string) {
- if err := RunSample(out, errOut, cmd, args); err != nil {
- fmt.Fprint(errOut, "Failed to execute cmd: ", err)
- }
- },
- }
- return cmd
- }
- func init() {
- out := os.Stdout
- errOut := os.Stderr
- RootCmd.AddCommand(NewCommandSample(out, errOut))
- }
- func RunSample(out, errOut io.Writer, cmd *cobra.Command, args []string) error {
- if len(args) < 1 {
- return errors.New("Wrong number of arguments\n")
- }
- if args[0] != "hiyosi" {
- return fmt.Errorf("unexpected arguments specified: %v", args[0])
- }
- fmt.Fprintf(out, "Hello %v, This is a sample", args[0])
- return nil
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement