forked from zaquestion/lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snippetList.go
67 lines (61 loc) · 1.42 KB
/
snippetList.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package cmd
import (
"fmt"
"log"
"github.com/spf13/cobra"
"github.com/xanzy/go-gitlab"
"github.com/zaquestion/lab/internal/git"
lab "github.com/zaquestion/lab/internal/gitlab"
)
// snippetListCmd represents the snippetList command
var snippetListCmd = &cobra.Command{
Use: "list [remote] [page]",
Short: "List personal or project snippets",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
remote, page, err := parseArgsRemote(args)
if err != nil {
log.Fatal(err)
}
if remote == "" {
remote = forkedFromRemote
}
listOpts := gitlab.ListOptions{
Page: int(page),
PerPage: 10,
}
// See if we're in a git repo or if global is set to determine
// if this should be a personal snippet
rn, _ := git.PathWithNameSpace(remote)
if global || rn == "" {
opts := gitlab.ListSnippetsOptions{
ListOptions: listOpts,
}
snips, err := lab.SnippetList(&opts)
if err != nil {
log.Fatal(err)
}
for _, snip := range snips {
fmt.Printf("#%d %s\n", snip.ID, snip.Title)
}
return
}
project, err := lab.FindProject(rn)
if err != nil {
log.Fatal(err)
}
opts := gitlab.ListProjectSnippetsOptions{
ListOptions: listOpts,
}
snips, err := lab.ProjectSnippetList(project.ID, &opts)
if err != nil {
log.Fatal(err)
}
for _, snip := range snips {
fmt.Printf("#%d %s\n", snip.ID, snip.Title)
}
},
}
func init() {
snippetCmd.AddCommand(snippetListCmd)
}