/
postsAdd.go
95 lines (87 loc) · 2.68 KB
/
postsAdd.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
package posts
import (
"encoding/json"
"fmt"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/rmrfslashbin/thumbtack"
"github.com/rmrfslashbin/thumbtack/cmd/thumbtack/clictx"
)
// PostsAddCmd is the command to add a bookmark.
type PostsAddCmd struct {
Url string `name:"url" required:"" help:"URL to bookmark" type:"string"`
Title string `name:"title" required:"" help:"Title of bookmark" type:"string"`
Descr *string `name:"descr" help:"Description of bookmark" type:"string"`
Replace bool `name:"replace" negatable:"" help:"Replace existing bookmark" default:"true" type:"bool"`
Shared bool `name:"shared" negatable:"" help:"Share bookmark with everyone" default:"true" type:"bool"`
Tags []string `name:"tag" help:"Tags to add to bookmark" type:"string"`
Timestamp *time.Time `name:"timestamp" help:"Timestamp to add bookmark(format: 2006-01-02T15:04:05Z)" type:"date"`
Json bool `name:"json" help:"Output as JSON" default:"false" type:"bool"`
Unread *bool `name:"unread" help:"Mark bookmark as unread" default:"false" type:"bool"`
}
// Run runs the command
func (cmd *PostsAddCmd) Run(ctx *clictx.Context) error {
// Say hello
ctx.Log.Debug().
Str("cmd", "posts add").
Str("app_name", ctx.Appname).
Msg("Running command")
// Create thumbtack client
client, err := thumbtack.New(
thumbtack.WithEndpoint(ctx.Endpoint),
thumbtack.WithToken(ctx.Token),
thumbtack.WithLogger(ctx.Log),
thumbtack.WithUserAgent(ctx.UserAgent),
)
if err != nil {
ctx.Log.Error().
Str("cmd", "posts add").
Str("app_name", ctx.Appname).
Msg("Failed to create client")
return err
}
// Add bookmark with params
add, err := client.PostsAdd(
&thumbtack.PostsAddInput{
Url: &cmd.Url,
Title: &cmd.Title,
Description: cmd.Descr,
Replace: &cmd.Replace,
Shared: &cmd.Shared,
Tags: cmd.Tags,
Timestamp: cmd.Timestamp,
ToRead: cmd.Unread,
},
)
if err != nil {
if _, ok := err.(*thumbtack.ErrUnexpectedResponse); ok {
ctx.Log.Error().
Str("cmd", "posts add").
Str("app_name", ctx.Appname).
Str("result", err.(*thumbtack.ErrUnexpectedResponse).ResultCode).
Msg("Failed to add bookmark")
} else {
ctx.Log.Error().
Str("cmd", "posts add").
Str("app_name", ctx.Appname).
Msg("Failed to add bookmark")
}
return err
}
if cmd.Json {
// Print the result as JSON
data, err := json.Marshal(add)
if err != nil {
ctx.Log.Error().
Str("cmd", "posts add").
Str("app_name", ctx.Appname).
Msg("Failed to marshal add")
return err
}
fmt.Println(string(data))
} else {
// Spew the result
spew.Dump(add)
}
return nil
}