-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
99 lines (89 loc) · 2.8 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
package main
import (
"encoding/csv"
"fmt"
"io"
"net/http"
"os"
"runtime"
"time"
"github.com/ryanuber/columnize"
"github.com/spf13/cobra"
"github.com/mlafeldt/ck/convertkit"
)
func main() {
config := convertkit.DefaultConfig()
config.HTTPClient = &http.Client{Timeout: 10 * time.Second}
rootCmd := &cobra.Command{
Use: "ck",
Short: "The ConvertKit Tool",
SilenceUsage: true,
}
rootCmd.PersistentFlags().StringVar(&config.Key, "api-key", "", "Set API key for ConvertKit account")
rootCmd.PersistentFlags().StringVar(&config.Secret, "api-secret", "", "Set API secret for ConvertKit account")
rootCmd.PersistentFlags().StringVar(&config.Endpoint, "api-endpoint", "", "Set ConvertKit API endpoint")
var (
query convertkit.SubscriberQuery
csv bool
)
subscribersCmd := &cobra.Command{
Use: "subscribers",
Short: "List subscribers",
RunE: func(cmd *cobra.Command, args []string) error {
client, _ := convertkit.NewClient(config)
subscribers, err := client.Subscribers(&query)
if err != nil {
return err
}
if csv {
return outputCSV(os.Stdout, subscribers)
}
return outputTable(os.Stdout, subscribers)
},
}
subscribersCmd.Flags().StringVar(&query.Since, "since", "", "Filter subscribers added on or after this date (YYYY-MM-DD)")
subscribersCmd.Flags().StringVar(&query.Until, "until", "", "Filter subscribers added on or before this date (YYYY-MM-DD)")
subscribersCmd.Flags().BoolVar(&query.Reverse, "reverse", false, "List subscribers in reverse order")
subscribersCmd.Flags().BoolVar(&query.Cancelled, "cancelled", false, "List cancelled subscribers")
subscribersCmd.Flags().StringVar(&query.EmailAddress, "email", "", "Filter subscribers by email address")
subscribersCmd.Flags().BoolVar(&csv, "csv", false, "Output in CSV format")
rootCmd.AddCommand(subscribersCmd)
versionCmd := &cobra.Command{
Use: "version",
Short: "Show program version",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("ck %s %s/%s %s\n", Version,
runtime.GOOS, runtime.GOARCH, runtime.Version())
},
}
rootCmd.AddCommand(versionCmd)
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
func outputCSV(w io.Writer, subscribers []convertkit.Subscriber) error {
records := [][]string{
{"Email", "Signed up"},
}
for _, s := range subscribers {
records = append(records, []string{
s.EmailAddress,
s.CreatedAt.Format(time.RFC3339),
})
}
cw := csv.NewWriter(w)
cw.WriteAll(records)
return cw.Error()
}
func outputTable(w io.Writer, subscribers []convertkit.Subscriber) error {
lines := []string{"#|Email|Signed up"}
for i, s := range subscribers {
lines = append(lines, fmt.Sprintf("%d|%s|%s",
i+1,
s.EmailAddress,
s.CreatedAt.Format(time.RFC3339),
))
}
_, err := fmt.Fprintln(w, columnize.SimpleFormat(lines))
return err
}