Skip to content

Commit 4c3c5e0

Browse files
committed
add users command
1 parent 6d7777f commit 4c3c5e0

File tree

4 files changed

+141
-37
lines changed

4 files changed

+141
-37
lines changed

cmd/list.go

+10-35
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,24 @@
11
package cmd
22

33
import (
4-
"fmt"
5-
6-
"github.com/dghubble/go-twitter/twitter"
7-
"github.com/dghubble/oauth1"
4+
"github.com/nasum/tt/cmd/list"
85
"github.com/nasum/tt/lib"
96
"github.com/spf13/cobra"
107
)
118

129
func listCmd(config lib.Config) *cobra.Command {
1310

14-
oauthConfig := oauth1.NewConfig(config.ConsumerKey, config.ConsumerSecret)
15-
token := oauth1.NewToken(config.AccessToken, config.AccessSecret)
16-
17-
httpClient := oauthConfig.Client(oauth1.NoContext, token)
18-
client := twitter.NewClient(httpClient)
19-
20-
displayConsole := &lib.DisplayConsole{}
21-
2211
cmd := &cobra.Command{
23-
Use: "list",
24-
Short: "get your list",
25-
RunE: func(cmd *cobra.Command, args []string) error {
26-
user, res, err := client.Accounts.VerifyCredentials(&twitter.AccountVerifyParams{})
27-
28-
if err != nil {
29-
return fmt.Errorf("cannot get VerifyCredentials: %v: %v", err, res.Status)
30-
}
31-
32-
lists, res, err := client.Lists.List(&twitter.ListsListParams{
33-
UserID: user.ID,
34-
})
35-
36-
if err != nil {
37-
return fmt.Errorf("cannot get list: %v: %v", err, res.Status)
38-
}
39-
40-
for _, list := range lists {
41-
displayConsole.ShowList(list.Name, list.URI)
42-
}
43-
44-
return nil
45-
},
12+
Use: "list",
13+
Short: "list command",
14+
SilenceErrors: true,
15+
SilenceUsage: true,
4616
}
4717

18+
cmd.AddCommand(
19+
list.LSCmd(config),
20+
list.UsersCmd(config),
21+
)
22+
4823
return cmd
4924
}

cmd/list/ls.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package list
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/dghubble/go-twitter/twitter"
7+
"github.com/dghubble/oauth1"
8+
"github.com/nasum/tt/lib"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
func LSCmd(config lib.Config) *cobra.Command {
13+
14+
oauthConfig := oauth1.NewConfig(config.ConsumerKey, config.ConsumerSecret)
15+
token := oauth1.NewToken(config.AccessToken, config.AccessSecret)
16+
17+
httpClient := oauthConfig.Client(oauth1.NoContext, token)
18+
client := twitter.NewClient(httpClient)
19+
20+
displayConsole := &lib.DisplayConsole{}
21+
22+
cmd := &cobra.Command{
23+
Use: "ls",
24+
Short: "get your list",
25+
RunE: func(cmd *cobra.Command, args []string) error {
26+
user, res, err := client.Accounts.VerifyCredentials(&twitter.AccountVerifyParams{})
27+
28+
if err != nil {
29+
return fmt.Errorf("cannot get VerifyCredentials: %v: %v", err, res.Status)
30+
}
31+
32+
lists, res, err := client.Lists.List(&twitter.ListsListParams{
33+
UserID: user.ID,
34+
})
35+
36+
if err != nil {
37+
return fmt.Errorf("cannot get list: %v: %v", err, res.Status)
38+
}
39+
40+
for _, list := range lists {
41+
displayConsole.ShowList(list.Name, list.URI, list.ID)
42+
}
43+
44+
return nil
45+
},
46+
}
47+
return cmd
48+
}

cmd/list/users.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package list
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/dghubble/go-twitter/twitter"
7+
"github.com/dghubble/oauth1"
8+
"github.com/nasum/tt/lib"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
// UsersParams is users command paramater
13+
type UsersParams struct {
14+
ListID int64
15+
}
16+
17+
// UsersCmd is users command
18+
func UsersCmd(config lib.Config) *cobra.Command {
19+
20+
oauthConfig := oauth1.NewConfig(config.ConsumerKey, config.ConsumerSecret)
21+
token := oauth1.NewToken(config.AccessToken, config.AccessSecret)
22+
23+
httpClient := oauthConfig.Client(oauth1.NoContext, token)
24+
client := twitter.NewClient(httpClient)
25+
26+
displayConsole := &lib.DisplayConsole{}
27+
28+
usersParams := &UsersParams{}
29+
30+
cmd := &cobra.Command{
31+
Use: "users",
32+
Short: "get your list users",
33+
RunE: func(cmd *cobra.Command, args []string) error {
34+
var cursor int64 = -1
35+
for cursor != 0 {
36+
37+
members, res, err := client.Lists.Members(&twitter.ListsMembersParams{ListID: usersParams.ListID, Cursor: cursor})
38+
39+
if err != nil {
40+
return fmt.Errorf("cannot get users: %v: %v", err, res.Status)
41+
}
42+
43+
for _, user := range members.Users {
44+
displayConsole.ShowUser(user.Name, user.ScreenName, user.URL, user.FriendsCount, user.FollowersCount)
45+
}
46+
cursor = members.NextCursor
47+
}
48+
49+
return nil
50+
},
51+
}
52+
flags := cmd.Flags()
53+
flags.Int64VarP(&usersParams.ListID, "list", "l", 0, "list id")
54+
return cmd
55+
}

lib/display_console.go

+28-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ func (d *DisplayConsole) TweetID(tweetID string) string {
2424
return yellow(tweetID)
2525
}
2626

27+
// ListID is output colored tweet id text
28+
func (d *DisplayConsole) ListID(listID string) string {
29+
yellow := color.New(color.FgYellow).SprintFunc()
30+
return yellow(listID)
31+
}
32+
2733
// ReplyTo is output colored reply text
2834
func (d *DisplayConsole) ReplyTo(tweetID string) string {
2935
cyan := color.New(color.FgCyan).SprintFunc()
@@ -36,6 +42,12 @@ func (d *DisplayConsole) URL(url string) string {
3642
return green(url)
3743
}
3844

45+
// Name is output colored name text
46+
func (d *DisplayConsole) Name(name string) string {
47+
yellow := color.New(color.FgYellow).SprintFunc()
48+
return yellow(name)
49+
}
50+
3951
// ShowTweet is display tweet text
4052
func (d *DisplayConsole) ShowTweet(createdAt time.Time, tweetID int64, screenName string, text string) {
4153
fmt.Fprintf(
@@ -49,11 +61,25 @@ func (d *DisplayConsole) ShowTweet(createdAt time.Time, tweetID int64, screenNam
4961
}
5062

5163
// ShowList is display list text
52-
func (d *DisplayConsole) ShowList(title string, url string) {
64+
func (d *DisplayConsole) ShowList(title string, url string, id int64) {
5365
fmt.Fprintf(
5466
color.Output,
55-
"%s\t%s\n",
67+
"%s\t%s\t%s\n",
5668
d.URL("https://twitter.com/"+url),
69+
d.ListID(strconv.FormatInt(id, 10)),
5770
title,
5871
)
5972
}
73+
74+
// ShowUser is display user
75+
func (d *DisplayConsole) ShowUser(name string, screenName string, url string, friendsCount int, followersCount int) {
76+
fmt.Fprintf(
77+
color.Output,
78+
"%s\t%s\t%s\t%s\t%s\n",
79+
d.URL("https://twitter.com/"+screenName),
80+
d.Name(name),
81+
d.Name("@"+screenName),
82+
strconv.Itoa(friendsCount),
83+
strconv.Itoa(followersCount),
84+
)
85+
}

0 commit comments

Comments
 (0)