2018-11-22 03:35:51 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2020-10-04 01:19:10 +00:00
|
|
|
"strings"
|
2018-11-22 03:35:51 +00:00
|
|
|
|
|
|
|
mastodon "github.com/mattn/go-mastodon"
|
2019-08-11 19:46:42 +00:00
|
|
|
"github.com/spf13/cobra"
|
2018-11-22 03:35:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-08-11 19:46:42 +00:00
|
|
|
client *mastodon.Client
|
|
|
|
self *mastodon.Account
|
|
|
|
configFile string
|
|
|
|
|
2020-08-23 10:09:38 +00:00
|
|
|
// RootCmd is the core command used for cli-arg parsing.
|
2019-08-11 19:46:42 +00:00
|
|
|
RootCmd = &cobra.Command{
|
|
|
|
Use: "mastotool",
|
|
|
|
Short: "mastotool offers a collection of tools to work with your Mastodon account",
|
|
|
|
SilenceErrors: true,
|
|
|
|
SilenceUsage: true,
|
2020-10-06 02:07:22 +00:00
|
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if err := initClient(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
self, err = client.GetAccountCurrentUser(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Can't retrieve user: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
2019-08-11 19:46:42 +00:00
|
|
|
}
|
2018-11-22 03:35:51 +00:00
|
|
|
)
|
|
|
|
|
2020-08-23 10:09:54 +00:00
|
|
|
const (
|
|
|
|
progressBarWidth = 40
|
|
|
|
)
|
|
|
|
|
2018-11-22 21:25:08 +00:00
|
|
|
func registerApp(config *Config) (string, error) {
|
2018-11-22 03:35:51 +00:00
|
|
|
app, err := mastodon.RegisterApp(context.Background(), &mastodon.AppConfig{
|
|
|
|
Server: config.Value("instance").(string),
|
2019-08-11 02:12:37 +00:00
|
|
|
ClientName: "mastotool",
|
2018-11-22 21:25:08 +00:00
|
|
|
Scopes: "read",
|
2018-11-22 03:35:51 +00:00
|
|
|
Website: "",
|
|
|
|
})
|
|
|
|
if err != nil {
|
2018-11-22 21:25:08 +00:00
|
|
|
return "", err
|
2018-11-22 03:35:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
config.Set("id", app.ClientID)
|
|
|
|
config.Set("secret", app.ClientSecret)
|
2018-11-22 21:25:08 +00:00
|
|
|
config.Set("redirectURI", app.RedirectURI)
|
|
|
|
|
|
|
|
return app.AuthURI, nil
|
2018-11-22 03:35:51 +00:00
|
|
|
}
|
|
|
|
|
2019-08-08 04:18:20 +00:00
|
|
|
func initClient() error {
|
2018-11-22 03:35:51 +00:00
|
|
|
var err error
|
2018-11-22 21:25:08 +00:00
|
|
|
var instance, token, redirectURI, authURI, id, secret string
|
2019-08-11 19:46:42 +00:00
|
|
|
config, err := LoadConfig(configFile)
|
2018-11-22 03:35:51 +00:00
|
|
|
if err == nil {
|
|
|
|
instance = config.Value("instance").(string)
|
|
|
|
id = config.Value("id").(string)
|
2018-11-22 21:25:08 +00:00
|
|
|
secret = config.Value("secret").(string)
|
|
|
|
token = config.Value("token").(string)
|
|
|
|
redirectURI = config.Value("redirectURI").(string)
|
2018-11-22 03:35:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
|
|
if len(instance) == 0 {
|
|
|
|
fmt.Print("Which instance to connect to (e.g. https://mastodon.social): ")
|
|
|
|
scanner.Scan()
|
|
|
|
if scanner.Err() != nil {
|
2019-08-08 04:18:20 +00:00
|
|
|
return fmt.Errorf("Can't open input: %s", err)
|
2018-11-22 03:35:51 +00:00
|
|
|
}
|
2020-10-04 01:19:10 +00:00
|
|
|
|
2018-11-22 03:35:51 +00:00
|
|
|
instance = scanner.Text()
|
2020-10-04 01:19:10 +00:00
|
|
|
if !strings.HasPrefix(instance, "http") {
|
|
|
|
instance = "https://" + instance
|
|
|
|
}
|
|
|
|
|
2018-11-22 21:25:08 +00:00
|
|
|
config.Set("instance", instance)
|
2018-11-22 03:35:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(id) == 0 {
|
2018-11-22 21:25:08 +00:00
|
|
|
authURI, err = registerApp(&config)
|
2018-11-22 03:35:51 +00:00
|
|
|
if err != nil {
|
2019-08-08 04:18:20 +00:00
|
|
|
return fmt.Errorf("Can't register app: %s", err)
|
2018-11-22 03:35:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
id = config.Value("id").(string)
|
|
|
|
secret = config.Value("secret").(string)
|
2018-11-22 21:25:08 +00:00
|
|
|
redirectURI = config.Value("redirectURI").(string)
|
2018-11-22 03:35:51 +00:00
|
|
|
}
|
|
|
|
|
2018-11-22 21:25:08 +00:00
|
|
|
mConfig := &mastodon.Config{
|
|
|
|
AccessToken: token,
|
|
|
|
Server: instance,
|
|
|
|
ClientID: id,
|
|
|
|
ClientSecret: secret,
|
|
|
|
}
|
|
|
|
client = mastodon.NewClient(mConfig)
|
|
|
|
|
|
|
|
if len(mConfig.AccessToken) == 0 {
|
|
|
|
fmt.Printf("Please visit %s and enter the generated token: ", authURI)
|
|
|
|
scanner.Scan()
|
|
|
|
if scanner.Err() != nil {
|
2019-08-08 04:18:20 +00:00
|
|
|
return fmt.Errorf("Can't open input: %s", err)
|
2018-11-22 21:25:08 +00:00
|
|
|
}
|
|
|
|
code := scanner.Text()
|
|
|
|
|
|
|
|
err = client.AuthenticateToken(context.Background(), code, redirectURI)
|
2018-11-22 03:35:51 +00:00
|
|
|
if err != nil {
|
2019-08-08 04:18:20 +00:00
|
|
|
return fmt.Errorf("Can't retrieve authentication token: %s", err)
|
2018-11-22 03:35:51 +00:00
|
|
|
}
|
|
|
|
|
2018-11-22 21:25:08 +00:00
|
|
|
config.Set("token", mConfig.AccessToken)
|
2019-08-11 19:46:42 +00:00
|
|
|
err = config.Save(configFile)
|
2019-08-08 04:18:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Can't save config: %s", err)
|
|
|
|
}
|
2018-11-22 03:35:51 +00:00
|
|
|
}
|
|
|
|
|
2019-08-08 04:18:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-11-22 05:48:21 +00:00
|
|
|
|
2019-08-08 04:18:20 +00:00
|
|
|
func main() {
|
2019-08-11 19:46:42 +00:00
|
|
|
RootCmd.PersistentFlags().StringVarP(&configFile, "config", "c", "mastodon.json", "uses the specified config file")
|
|
|
|
|
|
|
|
if err := RootCmd.Execute(); err != nil {
|
2019-08-08 04:18:20 +00:00
|
|
|
fmt.Println(err)
|
2020-10-06 02:07:22 +00:00
|
|
|
os.Exit(1)
|
2019-08-08 04:18:20 +00:00
|
|
|
}
|
2018-11-22 03:35:51 +00:00
|
|
|
}
|