/
main.go
112 lines (87 loc) · 2.06 KB
/
main.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
package main
import (
"os"
"fmt"
"io/ioutil"
"log"
"github.com/BurntSushi/toml"
"github.com/codegangsta/cli"
"github.com/mitchellh/go-homedir"
"github.com/russross/blackfriday"
medium "github.com/Medium/medium-sdk-go"
)
type Configuration struct {
Token string
}
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
var config Configuration
app := cli.NewApp()
app.Name = "Medium Cli"
app.Version = "0.0.1"
app.Usage = "Publish to Medium.com using the command line"
app.Flags = []cli.Flag {
cli.StringFlag {
Name: "status, s",
Value: "publish",
Usage: "Status of the post",
},
cli.StringFlag {
Name: "title, t",
Usage: "Post tile",
},
cli.StringFlag {
Name: "markdown, m",
Usage: "Markdown file to post to Medium",
},
}
app.Action = func(c *cli.Context) {
status := c.String("status")
title := c.String("title")
markdownFile := c.String("markdown")
// check post status
if status != "publish" {
println(status + " is not a supported status")
return
}
if markdownFile == "" {
println("Please specify a markdown file")
return
}
if title == "" {
println("Please specify a title")
return
}
// get post as markdown file
data, err := ioutil.ReadFile(markdownFile);
check(err)
markdown := string(blackfriday.MarkdownCommon([]byte(data)))
fmt.Println("Markdown: " + markdown)
dir, err := homedir.Dir()
check(err)
println("Get config file from " + dir)
if _, err := toml.DecodeFile(dir + "/medium-cli-conf.toml", &config); err != nil {
fmt.Println(err)
return
}
println(status + " something")
fmt.Println("Found token: " + config.Token)
mediumClient := medium.NewClientWithAccessToken(config.Token)
mediumUser, err := mediumClient.GetUser()
check(err)
post, err := mediumClient.CreatePost(medium.CreatePostOptions{
UserID: mediumUser.ID,
Title: title,
Content: markdown,
ContentFormat: medium.ContentFormatHTML,
PublishStatus: medium.PublishStatusDraft,
})
check(err)
log.Println(mediumUser, post)
}
app.Run(os.Args)
}