github.com/zaquestion/lab@v0.25.1/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 PersistentPreRun: labPersistentPreRun, 16 Run: func(cmd *cobra.Command, args []string) { 17 if list, _ := cmd.Flags().GetBool("list"); list { 18 snippetListCmd.Run(cmd, args) 19 return 20 } 21 22 if browse, _ := cmd.Flags().GetBool("browse"); browse { 23 snippetBrowseCmd.Run(cmd, args) 24 return 25 } 26 27 if deleteID, _ := cmd.Flags().GetString("delete"); deleteID != "" { 28 // append the args here so that remote can stil be 29 // properly passed in 30 snippetDeleteCmd.Run(cmd, append(args, deleteID)) 31 return 32 } 33 34 if stat, _ := os.Stdin.Stat(); (stat.Mode() & os.ModeCharDevice) == 0 { 35 snippetCreateCmd.Run(cmd, args) 36 return 37 } 38 39 if len(args) > 0 || file != "" { 40 snippetCreateCmd.Run(cmd, args) 41 return 42 } 43 44 cmd.Help() 45 }, 46 } 47 48 var ( 49 global bool 50 ) 51 52 func init() { 53 snippetCmd.PersistentFlags().BoolVarP(&global, "global", "g", false, "create as a personal snippet") 54 snippetCmd.Flags().BoolP("list", "l", false, "list snippets") 55 snippetCmd.Flags().MarkDeprecated("list", "use the \"list\" subcommand instead") 56 snippetCmd.Flags().BoolP("browse", "b", false, "browse snippets") 57 snippetCmd.Flags().MarkDeprecated("browse", "use the \"browse\" subcommand instead") 58 snippetCmd.Flags().StringP("delete", "d", "", "delete snippet with id") 59 snippetCmd.Flags().MarkDeprecated("delete", "use the \"delete\" subcommand instead") 60 // Create flags added in snippetCreate.go 61 RootCmd.AddCommand(snippetCmd) 62 }