gitlab.com/prarit/lab@v0.14.0/cmd/snippet_list.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/spf13/cobra" 8 "github.com/xanzy/go-gitlab" 9 lab "github.com/zaquestion/lab/internal/gitlab" 10 ) 11 12 var snippetListConfig struct { 13 Number int 14 All bool 15 } 16 17 // snippetListCmd represents the snippetList command 18 var snippetListCmd = &cobra.Command{ 19 Use: "list [remote]", 20 Aliases: []string{"ls"}, 21 Short: "List personal or project snippets", 22 Long: ``, 23 Run: func(cmd *cobra.Command, args []string) { 24 rn, _, err := parseArgs(args) 25 if err != nil { 26 log.Fatal(err) 27 } 28 listOpts := gitlab.ListOptions{ 29 PerPage: snippetListConfig.Number, 30 } 31 32 num := snippetListConfig.Number 33 if snippetListConfig.All { 34 num = -1 35 } 36 // See if we're in a git repo or if global is set to determine 37 // if this should be a personal snippet 38 if global || rn == "" { 39 opts := gitlab.ListSnippetsOptions(listOpts) 40 snips, err := lab.SnippetList(opts, num) 41 if err != nil { 42 log.Fatal(err) 43 } 44 for _, snip := range snips { 45 fmt.Printf("#%d %s\n", snip.ID, snip.Title) 46 } 47 return 48 } 49 50 project, err := lab.FindProject(rn) 51 if err != nil { 52 log.Fatal(err) 53 } 54 opts := gitlab.ListProjectSnippetsOptions(listOpts) 55 snips, err := lab.ProjectSnippetList(project.ID, opts, num) 56 if err != nil { 57 log.Fatal(err) 58 } 59 for _, snip := range snips { 60 fmt.Printf("#%d %s\n", snip.ID, snip.Title) 61 } 62 }, 63 } 64 65 func init() { 66 snippetListCmd.Flags().IntVarP(&snippetListConfig.Number, "number", "n", 10, "Number of snippets to return") 67 snippetListCmd.Flags().BoolVarP(&snippetListConfig.All, "all", "a", false, "List all snippets") 68 snippetCmd.AddCommand(snippetListCmd) 69 }