-
Notifications
You must be signed in to change notification settings - Fork 1
/
godium.go
178 lines (145 loc) · 4.08 KB
/
godium.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"fmt"
"io/ioutil"
"os"
"errors"
"github.com/ericaro/frontmatter"
medium "github.com/medium/medium-sdk-go"
"github.com/mitchellh/go-homedir"
"github.com/skratchdot/open-golang/open"
"github.com/urfave/cli"
)
const TokenFileName string = "~/.godium"
type postData struct {
Title string `fm:"title"`
Tags []string `fm:"tags"`
Content string `fm:"content"`
}
func main() {
app := cli.NewApp()
app.Commands = []cli.Command{
addTokenCommand(),
infoCommand(),
publishCommand(),
}
app.HideVersion = true
app.Copyright = "MIT"
app.Usage = "Interact with the Medium publishing platform through the command line."
err := app.Run(os.Args)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func addTokenCommand() cli.Command {
return cli.Command{
Name: "set-token",
Aliases: []string{"st"},
Usage: "Add an integration token to be used by this application",
Action: func(c *cli.Context) error {
filename, err := homedir.Expand(TokenFileName)
if err != nil {
return err
}
token := c.Args().First()
err = ioutil.WriteFile(filename, []byte(token), 0644)
if err != nil {
return err
}
fmt.Printf("Token was written into %s successfully.", filename)
return nil
},
}
}
func infoCommand() cli.Command {
return cli.Command{
Name: "info",
Usage: "Get the information for the owner of the access token",
Action: func(c *cli.Context) error {
accessToken, err := getAccessToken()
if err != nil {
return err
}
m := medium.NewClientWithAccessToken(accessToken)
user, err := m.GetUser("")
if err != nil {
return err
}
fmt.Println("User ID:", user.ID)
fmt.Println("Username:", user.Username)
fmt.Println("Name:", user.Name)
fmt.Println("Profile URL:", user.URL)
return nil
},
}
}
func publishCommand() cli.Command {
return cli.Command{
Name: "publish",
Aliases: []string{"p"},
Usage: "Publish a markdown file to Medium with the status set to " + string(medium.PublishStatusDraft) + " and open the post editor page in the browser",
Action: publishAction,
}
}
func getAccessToken() (string, error) {
filename, err := homedir.Expand(TokenFileName)
if err != nil {
return "", err
}
bytes, err := ioutil.ReadFile(filename)
// If the file is not found, it means we have no tokens. Try and make it more user-friendly.
if err != nil && os.IsNotExist(err) {
err := open.Run("https://medium.com/me/settings")
if err != nil {
return "", errors.New("We tried to open your browser for you automatically, but for some reason it failed. Please manually browse to https://medium.com/me/settings, generate an integration token, and use the `godium set-token <token>` command to add it.")
}
return "", errors.New("Could not find the token. We have opened your browser for you. Please generate an integration token in your browser window, and use `godium set-token <token>` to add it.")
}
// It's not a file not found error, so just return it
if err != nil {
return "", err
}
return string(bytes), nil
}
func createPostOptionsFromFile(filename string) (*medium.CreatePostOptions, error) {
contents, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
postData := &postData{}
if err := frontmatter.Unmarshal(contents, postData); err != nil {
return nil, err
}
createPostOptions := &medium.CreatePostOptions{
Title: postData.Title,
ContentFormat: medium.ContentFormatMarkdown,
Content: postData.Content,
Tags: postData.Tags,
PublishStatus: medium.PublishStatusDraft,
}
return createPostOptions, nil
}
func publishAction(c *cli.Context) error {
createPostOptions, err := createPostOptionsFromFile(c.Args().First())
if err != nil {
return err
}
accessToken, err := getAccessToken()
if err != nil {
return err
}
m := medium.NewClientWithAccessToken(accessToken)
user, err := m.GetUser("")
if err != nil {
return err
}
createPostOptions.UserID = user.ID
post, err := m.CreatePost(*createPostOptions)
if err != nil {
return err
}
fmt.Println("Post created. URL is ", post.URL)
open.Run(post.URL)
return nil
}