-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
110 lines (97 loc) · 3.18 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
package main
import (
"context"
"flag"
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
_ "github.com/joho/godotenv/autoload"
"github.com/mattn/go-mastodon"
)
var version = "<dev>"
// Environment variable names used to configure the program.
const (
EnvVarServer = "MASTODON_SERVER"
EnvVarClientID = "MASTODON_CLIENT_ID"
EnvVarClientSecret = "MASTODON_CLIENT_SECRET"
EnvVarAccessToken = "MASTODON_ACCESS_TOKEN"
)
func eprintf(format string, args ...any) {
_, _ = fmt.Fprintf(os.Stderr, format, args...)
}
func usage() {
eprintf("usage: %s -text \"message to post\" [OPTIONS]\n", filepath.Base(os.Args[0]))
eprintf("\nOptions:\n")
flag.PrintDefaults()
eprintf("\nEnvironment variables:\n")
eprintf(" %s\n \tMastodon server's address.\n", EnvVarServer)
eprintf(" %s\n \tYour app's Client ID.\n", EnvVarClientID)
eprintf(" %s\n \tYour app's Client Secret.\n", EnvVarClientSecret)
eprintf(" %s\n \tYour Access Token.\n", EnvVarAccessToken)
eprintf("\nVisit https://github.com/cdzombak/mastodon-post/blob/main/README.md#credentials-and-server-configuration for instructions on obtaining a Client ID, Client Secret, and Access Token.\n")
eprintf("\nVersion:\n mastodon-post %s\n", version)
eprintf("\nGitHub:\n https://github.com/cdzombak/mastodon-post\n")
eprintf("\nAuthor:\n Chris Dzombak <https://www.dzombak.com>\n")
}
func main() {
text := flag.String("text", "", "Text to post to Mastodon")
visibility := flag.String("visibility", "public", "Visibility of the post (public, unlisted, private)")
printVersion := flag.Bool("printVersion", false, "Print printVersion and exit")
flag.Usage = usage
flag.Parse()
if *printVersion {
fmt.Printf("%s %s", filepath.Base(os.Args[0]), version)
os.Exit(0)
}
if *visibility != "public" && *visibility != "unlisted" && *visibility != "private" {
eprintf("invalid post visibility '%s' given\n", *visibility)
os.Exit(1)
}
if *text == "" {
flag.Usage()
os.Exit(1)
}
config := mastodon.Config{
Server: os.Getenv(EnvVarServer),
ClientID: os.Getenv(EnvVarClientID),
ClientSecret: os.Getenv(EnvVarClientSecret),
AccessToken: os.Getenv(EnvVarAccessToken),
}
if config.Server == "" {
eprintf("environment variable %s is missing\n", EnvVarServer)
os.Exit(1)
}
if !strings.HasPrefix(strings.ToLower(config.Server), "http") {
config.Server = "https://" + config.Server
}
if config.ClientID == "" {
eprintf("environment variable %s is missing\n", EnvVarClientID)
os.Exit(1)
}
if config.ClientSecret == "" {
eprintf("environment variable %s is missing\n", EnvVarClientSecret)
os.Exit(1)
}
if config.AccessToken == "" {
eprintf("environment variable %s is missing\n", EnvVarAccessToken)
os.Exit(1)
}
c := &mastodon.Client{
Client: *http.DefaultClient,
Config: &config,
UserAgent: "",
}
result, err := c.PostStatus(context.Background(), &mastodon.Toot{
Status: *text,
Visibility: *visibility,
// Sensitive: false, // https://github.com/cdzombak/mastodon-post/issues/1
// SpoilerText: "", // https://github.com/cdzombak/mastodon-post/issues/1
})
if err != nil {
fmt.Printf("error posting status: %s\n", err)
os.Exit(1)
}
fmt.Printf("posted: %s\n", result.URL)
}