github.com/ianfoo/lab@v0.9.5-0.20180123060006-5ed79f2ccfc7/cmd/snippet.go (about) 1 package cmd 2 3 import ( 4 "os" 5 6 "github.com/spf13/cobra" 7 ) 8 9 // snippetCmd represents the snippet command 10 var snippetCmd = &cobra.Command{ 11 Use: "snippet", 12 Aliases: []string{"snip"}, 13 Short: snippetCreateCmd.Short, 14 Long: snippetCreateCmd.Long, 15 Run: func(cmd *cobra.Command, args []string) { 16 if list, _ := cmd.Flags().GetBool("list"); list { 17 snippetListCmd.Run(cmd, args) 18 return 19 } 20 21 if browse, _ := cmd.Flags().GetBool("browse"); browse { 22 snippetBrowseCmd.Run(cmd, args) 23 return 24 } 25 26 if deleteID, _ := cmd.Flags().GetString("delete"); deleteID != "" { 27 // append the args here so that remote can stil be 28 // properly passed in 29 snippetDeleteCmd.Run(cmd, append(args, deleteID)) 30 return 31 } 32 33 if stat, _ := os.Stdin.Stat(); (stat.Mode() & os.ModeCharDevice) == 0 { 34 snippetCreateCmd.Run(cmd, args) 35 return 36 } 37 38 if len(args) > 0 || file != "" { 39 snippetCreateCmd.Run(cmd, args) 40 return 41 } 42 43 cmd.Help() 44 }, 45 } 46 47 var ( 48 global bool 49 ) 50 51 func init() { 52 snippetCmd.PersistentFlags().BoolVarP(&global, "global", "g", false, "create as a personal snippet") 53 snippetCmd.Flags().BoolP("list", "l", false, "list snippets") 54 snippetCmd.Flags().BoolP("browse", "b", false, "browse snippets") 55 snippetCmd.Flags().StringP("delete", "d", "", "delete snippet with id") 56 // Create flags added in snippetCreate.go 57 RootCmd.AddCommand(snippetCmd) 58 }