Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. package cmd
  2.  
  3. import (
  4. "errors"
  5. "fmt"
  6. "io"
  7. "os"
  8.  
  9. "github.com/spf13/cobra"
  10. )
  11.  
  12. func NewCommandSample(out, errOut io.Writer) *cobra.Command {
  13. cmd := &cobra.Command{
  14. Use: "sample",
  15. Short: "A brief description of your command",
  16. Run: func(cmd *cobra.Command, args []string) {
  17. if err := RunSample(out, errOut, cmd, args); err != nil {
  18. fmt.Fprint(errOut, "Failed to execute cmd: ", err)
  19. }
  20. },
  21. }
  22. return cmd
  23. }
  24.  
  25. func init() {
  26. out := os.Stdout
  27. errOut := os.Stderr
  28.  
  29. RootCmd.AddCommand(NewCommandSample(out, errOut))
  30. }
  31.  
  32. func RunSample(out, errOut io.Writer, cmd *cobra.Command, args []string) error {
  33. if len(args) < 1 {
  34. return errors.New("Wrong number of arguments\n")
  35. }
  36.  
  37. if args[0] != "hiyosi" {
  38. return fmt.Errorf("unexpected arguments specified: %v", args[0])
  39. }
  40.  
  41. fmt.Fprintf(out, "Hello %v, This is a sample", args[0])
  42.  
  43. return nil
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement