Skip to content

Commit c0f8a59

Browse files
committed
simple table tables
1 parent 493860d commit c0f8a59

File tree

3 files changed

+216
-1
lines changed

3 files changed

+216
-1
lines changed

cmd/posts.go

Lines changed: 207 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,226 @@
11
package cmd
22

33
import (
4+
"encoding/json"
45
"fmt"
6+
"io/ioutil"
7+
"log"
8+
"net/http"
9+
"time"
510

11+
"github.com/alexeyco/simpletable"
612
"github.com/spf13/cobra"
713
)
814

15+
const (
16+
rootAPIURL = "https://hashnode.com/ajax/posts"
17+
)
18+
19+
// API URL's
20+
var (
21+
hotPostsAPI = fmt.Sprintf("%s/%s", rootAPIURL, "hot")
22+
newsAPI = fmt.Sprintf("%s/%s", rootAPIURL, "news")
23+
)
24+
25+
// flags
26+
var (
27+
hot bool
28+
news bool
29+
)
30+
931
// postsCmd represents the posts command
1032
var postsCmd = &cobra.Command{
1133
Use: "posts",
1234
Short: "Lists posts",
1335
Run: func(cmd *cobra.Command, args []string) {
14-
fmt.Println("posts called")
36+
switch {
37+
case news:
38+
getNews()
39+
case hot:
40+
getHotPosts()
41+
default:
42+
log.Println("Specify what posts to get")
43+
}
1544
},
1645
}
1746

1847
func init() {
1948
rootCmd.AddCommand(postsCmd)
49+
50+
postsCmd.PersistentFlags().BoolVar(&hot, "hot", false, "get hot posts")
51+
postsCmd.PersistentFlags().BoolVar(&news, "news", false, "get news")
52+
}
53+
54+
func getHotPosts() {
55+
b, err := makeRequest(hotPostsAPI)
56+
if err != nil {
57+
log.Println(err)
58+
}
59+
var hotposts HotPosts
60+
err = json.Unmarshal(b, &hotposts)
61+
if err != nil {
62+
log.Println(err)
63+
}
64+
table := simpletable.New()
65+
table.Header = &simpletable.Header{
66+
Cells: []*simpletable.Cell{
67+
{Align: simpletable.AlignCenter, Text: "#"},
68+
{Align: simpletable.AlignCenter, Text: "Title"},
69+
{Align: simpletable.AlignCenter, Text: "Link"},
70+
},
71+
}
72+
for ind, post := range hotposts.Posts {
73+
r := []*simpletable.Cell{
74+
{Align: simpletable.AlignRight, Text: fmt.Sprintf("%d", ind+1)},
75+
{Align: simpletable.AlignRight, Text: post.Title},
76+
{Align: simpletable.AlignRight, Text: post.Slug},
77+
}
78+
table.Body.Cells = append(table.Body.Cells, r)
79+
}
80+
table.SetStyle(simpletable.StyleCompactLite)
81+
fmt.Println(table.String())
82+
}
83+
84+
func getNews() {
85+
b, err := makeRequest(newsAPI)
86+
if err != nil {
87+
log.Println(err)
88+
}
89+
m := make(map[string]interface{})
90+
json.Unmarshal(b, &m)
91+
fmt.Println(m)
92+
}
93+
94+
func makeRequest(url string) ([]byte, error) {
95+
client := http.Client{
96+
Timeout: time.Duration(1 * time.Minute),
97+
}
98+
resp, err := client.Get(url)
99+
if err != nil {
100+
return nil, err
101+
}
102+
103+
defer resp.Body.Close()
104+
105+
if resp.StatusCode == http.StatusOK {
106+
// read response body
107+
body, err := ioutil.ReadAll(resp.Body)
108+
if err != nil {
109+
return nil, err
110+
}
111+
return body, nil
112+
}
113+
114+
return nil, fmt.Errorf("Not Found")
115+
116+
}
117+
118+
type HotPosts struct {
119+
Posts []struct {
120+
ID string `json:"_id"`
121+
FollowersCount int `json:"followersCount"`
122+
Author struct {
123+
ID string `json:"_id"`
124+
Role interface{} `json:"role"`
125+
NumFollowing int `json:"numFollowing"`
126+
NumFollowers int `json:"numFollowers"`
127+
Name string `json:"name"`
128+
Tagline string `json:"tagline"`
129+
Photo string `json:"photo"`
130+
Username string `json:"username"`
131+
Appreciations []struct {
132+
Badge string `json:"badge"`
133+
ID string `json:"_id"`
134+
Count int `json:"count"`
135+
} `json:"appreciations"`
136+
DateJoined time.Time `json:"dateJoined"`
137+
SocialMedia struct {
138+
Website string `json:"website"`
139+
Twitter string `json:"twitter"`
140+
Github string `json:"github"`
141+
Linkedin string `json:"linkedin"`
142+
Google string `json:"google"`
143+
Facebook string `json:"facebook"`
144+
} `json:"socialMedia"`
145+
StoriesCreated []string `json:"storiesCreated"`
146+
Location string `json:"location"`
147+
CoverImage string `json:"coverImage"`
148+
BadgesAwarded []interface{} `json:"badgesAwarded"`
149+
TotalUpvotesReceived int `json:"totalUpvotesReceived"`
150+
IsEvangelist bool `json:"isEvangelist"`
151+
NumReactions int `json:"numReactions"`
152+
} `json:"author"`
153+
Cuid string `json:"cuid"`
154+
Slug string `json:"slug"`
155+
Title string `json:"title"`
156+
Type string `json:"type"`
157+
ReactionsByCurrentUser []interface{} `json:"reactionsByCurrentUser"`
158+
TotalReactions int `json:"totalReactions"`
159+
Reactions []struct {
160+
ID string `json:"_id"`
161+
Image string `json:"image"`
162+
Name string `json:"name"`
163+
} `json:"reactions"`
164+
BookmarkedIn []interface{} `json:"bookmarkedIn"`
165+
HasReward bool `json:"hasReward"`
166+
Contributors []struct {
167+
User struct {
168+
ID string `json:"_id"`
169+
Username string `json:"username"`
170+
Name string `json:"name"`
171+
Photo string `json:"photo"`
172+
Tagline string `json:"tagline"`
173+
BadgesAwarded []interface{} `json:"badgesAwarded"`
174+
Appreciations []struct {
175+
Badge string `json:"badge"`
176+
ID string `json:"_id"`
177+
Count int `json:"count"`
178+
} `json:"appreciations"`
179+
DateJoined time.Time `json:"dateJoined"`
180+
SocialMedia struct {
181+
Twitter string `json:"twitter"`
182+
Github string `json:"github"`
183+
Stackoverflow string `json:"stackoverflow"`
184+
Linkedin string `json:"linkedin"`
185+
Google string `json:"google"`
186+
Website string `json:"website"`
187+
} `json:"socialMedia"`
188+
StoriesCreated []string `json:"storiesCreated"`
189+
NumFollowing int `json:"numFollowing"`
190+
NumFollowers int `json:"numFollowers"`
191+
Location string `json:"location"`
192+
Role interface{} `json:"role"`
193+
CoverImage string `json:"coverImage"`
194+
TotalUpvotesReceived int `json:"totalUpvotesReceived"`
195+
IsEvangelist bool `json:"isEvangelist"`
196+
NumReactions int `json:"numReactions"`
197+
} `json:"user"`
198+
Stamp string `json:"stamp"`
199+
ID string `json:"_id"`
200+
} `json:"contributors"`
201+
IsActive bool `json:"isActive"`
202+
ResponseCount int `json:"responseCount"`
203+
DateAdded time.Time `json:"dateAdded"`
204+
Tags []struct {
205+
ID string `json:"_id"`
206+
Name string `json:"name"`
207+
Slug string `json:"slug"`
208+
MergedWith interface{} `json:"mergedWith,omitempty"`
209+
IsApproved bool `json:"isApproved"`
210+
IsActive bool `json:"isActive"`
211+
} `json:"tags"`
212+
Downvotes int `json:"downvotes"`
213+
Upvotes int `json:"upvotes"`
214+
TotalPollVotes int `json:"totalPollVotes"`
215+
PollOptions []interface{} `json:"pollOptions"`
216+
HasPolls bool `json:"hasPolls"`
217+
Brief string `json:"brief"`
218+
CoverImage string `json:"coverImage"`
219+
Views int `json:"views"`
220+
IsAnonymous bool `json:"isAnonymous"`
221+
DateUpdated time.Time `json:"dateUpdated"`
222+
IndexVotedByCurrentUser int `json:"indexVotedByCurrentUser"`
223+
IsFollowing bool `json:"isFollowing"`
224+
DateFeatured time.Time `json:"dateFeatured,omitempty"`
225+
} `json:"posts"`
20226
}

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
module github.com/hashnode/hashnode-cli
22

33
require (
4+
github.com/alexeyco/simpletable v0.0.0-20180729223640-1fa9009f1080
5+
github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8
46
github.com/mitchellh/go-homedir v1.0.0
57
github.com/spf13/cobra v0.0.3
68
github.com/spf13/viper v1.3.1
9+
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce
710
)

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
github.com/alexeyco/simpletable v0.0.0-20180729223640-1fa9009f1080 h1:LxG2QAVrS0Ml5A5/YUG5BLOJOrx2OR9yT9vkKW3CmUQ=
2+
github.com/alexeyco/simpletable v0.0.0-20180729223640-1fa9009f1080/go.mod h1:gx4+gp4N5VWqThMIidoUMBNUCT4Pan3J8ETR1ParWUU=
13
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
24
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
35
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
46
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
57
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
68
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
79
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
10+
github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8 h1:DujepqpGd1hyOd7aW59XpK7Qymp8iy83xq74fLr21is=
11+
github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q=
812
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
913
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
1014
github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY=
@@ -37,5 +41,7 @@ golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5h
3741
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
3842
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
3943
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
44+
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce h1:xcEWjVhvbDy+nHP67nPDDpbYrY+ILlfndk4bRioVHaU=
45+
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=
4046
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
4147
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

0 commit comments

Comments
 (0)