-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
147 lines (130 loc) · 3.09 KB
/
list.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"fmt"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/srvc/fail/v4"
)
type SortKey int
const (
_ SortKey = iota
Updated
Created
Number
Stars
Watches
Comments
BestMatch
)
func sortKeyFromString(key string) (SortKey, error) {
switch key {
case "updated":
return Updated, nil
case "created":
return Created, nil
case "number":
return Number, nil
case "stars":
return Stars, nil
case "watches":
return Watches, nil
case "comments":
return Comments, nil
case "best_match":
return BestMatch, nil
}
return 0, fail.New(fmt.Sprintf("Unknown SortKey: %s", key))
}
func NewListSubCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "list posts",
RunE: func(cmd *cobra.Command, args []string) error {
// Setup
ctx := cmd.Context()
fs := afero.NewOsFs()
diApp, err := NewDiApp(ctx, fs)
if err != nil {
return fail.Wrap(err)
}
client := diApp.Client
count, err := cmd.Flags().GetInt("count")
if err != nil {
return fail.Wrap(err)
}
sortKeyName, err := cmd.Flags().GetString("sort")
if err != nil {
return fail.Wrap(err)
}
sortKey, err := sortKeyFromString(sortKeyName)
if err != nil {
return fail.Wrap(err)
}
onlyTemplates, err := cmd.Flags().GetBool("only-templates")
if err != nil {
return fail.Wrap(err)
}
var posts []*Post
if onlyTemplates {
posts, err = client.GetTemplatePosts(cmd.Context(), count, sortKey)
if err != nil {
return fail.Wrap(err)
}
} else {
posts, err = client.GetPosts(cmd.Context(), count, sortKey)
if err != nil {
return fail.Wrap(err)
}
}
for _, post := range posts {
fmt.Printf("%d: %s\n", post.Number, post.FullName)
}
return nil
},
}
cmd.Flags().IntP("count", "c", 20, "Only print the number of posts")
cmd.Flags().String("sort", "updated", "Sort key [updated:created:number:stars:watches:comments:best_match]")
cmd.Flags().Bool("only-templates", false, "Show only template posts")
cmd.AddCommand(
newListTemplatesSubCmd(),
)
return cmd
}
func newListTemplatesSubCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "templates",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
fs := afero.NewOsFs()
diApp, err := NewDiApp(ctx, fs)
if err != nil {
return fail.Wrap(err)
}
client := diApp.Client
count, err := cmd.Flags().GetInt("count")
if err != nil {
return fail.Wrap(err)
}
sortKeyName, err := cmd.Flags().GetString("sort")
if err != nil {
return fail.Wrap(err)
}
sortKey, err := sortKeyFromString(sortKeyName)
if err != nil {
return fail.Wrap(err)
}
var posts []*Post
posts, err = client.GetTemplatePosts(cmd.Context(), count, sortKey)
if err != nil {
return fail.Wrap(err)
}
for _, post := range posts {
fmt.Printf("%d: %s\n", post.Number, post.FullName)
}
return nil
},
}
cmd.Flags().IntP("count", "c", 20, "Only print the number of posts")
cmd.Flags().String("sort", "updated", "Sort key [updated:created:number:stars:watches:comments:best_match]")
return cmd
}