Skip to content

Commit 701a673

Browse files
committed
add show image flag
1 parent 465f5cd commit 701a673

File tree

6 files changed

+24
-20
lines changed

6 files changed

+24
-20
lines changed

cmd/root.go

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/spf13/viper"
1010
)
1111

12+
// RootCmd is root command
1213
var RootCmd = &cobra.Command{
1314
Use: "tt",
1415
Short: "Twitter Client",

cmd/timeline.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ import (
99
"github.com/spf13/cobra"
1010
)
1111

12+
// TimelineParams command params
1213
type TimelineParams struct {
13-
Count int
14-
SinceID int64
15-
MaxID int64
16-
Reply bool
14+
Count int
15+
SinceID int64
16+
MaxID int64
17+
Reply bool
18+
ShowImage bool
1719
}
1820

1921
func timelineCmd(config lib.Config) *cobra.Command {
@@ -33,9 +35,8 @@ func timelineCmd(config lib.Config) *cobra.Command {
3335
RunE: func(cmd *cobra.Command, args []string) error {
3436
if timelineParams.Reply == true {
3537
return mentionTimeline(*client, *timelineParams, displayConsole)
36-
} else {
37-
return homeTimeline(*client, *timelineParams, displayConsole)
3838
}
39+
return homeTimeline(*client, *timelineParams, displayConsole)
3940
},
4041
}
4142

@@ -44,6 +45,7 @@ func timelineCmd(config lib.Config) *cobra.Command {
4445
flags.Int64VarP(&timelineParams.SinceID, "since-id", "S", 0, "Set since tweet id")
4546
flags.Int64VarP(&timelineParams.MaxID, "max-id", "M", 0, "Set max tweet id")
4647
flags.BoolVarP(&timelineParams.Reply, "mention", "m", false, "show mention timeline")
48+
flags.BoolVarP(&timelineParams.ShowImage, "show-images", "s", false, "show images")
4749

4850
return cmd
4951
}
@@ -61,8 +63,8 @@ func homeTimeline(client twitter.Client, timelineParams TimelineParams, displayC
6163
}
6264

6365
for _, tweet := range tweets {
64-
err := displayConsole.ShowTweet(tweet)
65-
66+
err := displayConsole.ShowTweet(tweet, timelineParams.ShowImage)
67+
6668
if err != nil {
6769
return fmt.Errorf("cannot display tweet: %v", err)
6870
}
@@ -85,7 +87,7 @@ func mentionTimeline(client twitter.Client, timelineParams TimelineParams, displ
8587
}
8688

8789
for _, tweet := range tweets {
88-
err = displayConsole.ShowTweet(tweet)
90+
err = displayConsole.ShowTweet(tweet, timelineParams.ShowImage)
8991

9092
if err != nil {
9193
return fmt.Errorf("cannot display tweet: %v", err)

cmd/tweet.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func tweetCmd(config lib.Config) *cobra.Command {
2929
return err
3030
}
3131

32-
err = displayConsole.ShowTweet(*tweet)
32+
err = displayConsole.ShowTweet(*tweet, true)
3333
if err != nil {
3434
return err
3535
}
@@ -43,7 +43,3 @@ func tweetCmd(config lib.Config) *cobra.Command {
4343

4444
return cmd
4545
}
46-
47-
func tweet(client twitter.Client, text string, reply_to string) error {
48-
return nil
49-
}

lib/config.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package lib
22

3+
// Config is twitter authentication
34
type Config struct {
4-
ConsumerKey string
5+
ConsumerKey string
56
ConsumerSecret string
6-
AccessToken string
7-
AccessSecret string
8-
}
7+
AccessToken string
8+
AccessSecret string
9+
}

lib/display_console.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (d *DisplayConsole) Name(name string) string {
5555
}
5656

5757
// ShowTweet is display tweet text
58-
func (d *DisplayConsole) ShowTweet(tweet twitter.Tweet) error {
58+
func (d *DisplayConsole) ShowTweet(tweet twitter.Tweet, showImage bool) error {
5959
createdAt, err := tweet.CreatedAtTime()
6060

6161
if err != nil {
@@ -72,6 +72,10 @@ func (d *DisplayConsole) ShowTweet(tweet twitter.Tweet) error {
7272
tweet.Text,
7373
)
7474

75+
if showImage == false {
76+
return nil
77+
}
78+
7579
media := &Media{}
7680

7781
medias := tweet.Entities.Media

lib/media.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type Media struct{}
2121
func (m *Media) ShowImage(url string) error {
2222
res, err := http.Get(url)
2323
if err != nil {
24-
fmt.Errorf("cannot get image: %v: %v", err, res.Status)
24+
return fmt.Errorf("cannot get image: %v: %v", err, res.Status)
2525
}
2626
defer res.Body.Close()
2727

0 commit comments

Comments
 (0)