This repository has been archived by the owner on Apr 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer_push.go
101 lines (82 loc) · 2.26 KB
/
buffer_push.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
package command
import (
"flag"
"fmt"
"strings"
"github.com/TailorDev/msw/buffer"
"github.com/TailorDev/msw/config"
"github.com/TailorDev/msw/issue"
"github.com/mitchellh/cli"
)
// BufferPushCommand is a Command that pushes links to Buffer.com's queue.
type BufferPushCommand struct {
UI cli.Ui
Conf config.Config
}
// Run runs the code of the comand.
func (c *BufferPushCommand) Run(args []string) int {
var apply bool
cmdFlags := flag.NewFlagSet("buffer push", flag.ContinueOnError)
cmdFlags.Usage = func() { c.UI.Output(c.Help()) }
cmdFlags.BoolVar(&apply, "apply", false, "")
if err := cmdFlags.Parse(args); err != nil {
return 1
}
args = cmdFlags.Args()
if len(args) != 1 {
cmdFlags.Usage()
return 1
}
i, err := issue.Parse(args[0])
if err != nil {
c.UI.Error(fmt.Sprintf("%s", err))
return 1
}
if c.Conf.Buffer.AccessToken == "" {
c.UI.Error("You must specify an access token in the configuration file.")
return 1
}
if len(c.Conf.Buffer.ProfileIDs) == 0 {
c.UI.Error("You must specify at least one profile ID in the configuration file.")
return 1
}
if !apply {
c.UI.Output("Re-run the command with `-apply` to actually push to Buffer.com\n")
}
cli := buffer.NewClient(c.Conf.Buffer.AccessToken)
for _, category := range i.Categories {
for _, link := range category.Links {
if link.Name != "" && link.URL != "" {
text := link.GetBufferText()
if apply {
updates, err := cli.Push(text, c.Conf.Buffer.ProfileIDs)
if err != nil {
c.UI.Error(fmt.Sprintf("%s", err))
continue
}
for _, u := range updates {
c.UI.Output(fmt.Sprintf("[%s] %s", u.ProfileService, u.Text))
}
} else {
c.UI.Output(fmt.Sprintf("[?] %s", text))
}
}
}
}
return 0
}
// Help returns the description of the command.
func (*BufferPushCommand) Help() string {
helpText := `
Usage: msw buffer push [options] FILENAME
This command pushes each entry of an issue to Buffer.com's queue. You need
a configuration file with Buffer credentials ('~/.msw/msw.yml').
Options:
-apply Push to Buffer.com's queue.
`
return strings.TrimSpace(helpText)
}
// Synopsis returns the short description of the command.
func (*BufferPushCommand) Synopsis() string {
return "push links to Buffer.com's queue"
}