return information on the current logged in user.`,
PreRun: func(cmd *cobra.Command, args []string) {
- err := account.VerifyCredentials(host, token)
+ err := account.VerifyCredentials(&mAPI)
if err != nil {
fmt.Println("Failed to verify credentials.")
os.Exit(1)
accountID = account.ID
}
- acct, err := mastodon.GetAccount(accountID, host, token)
+ acct, err := mastodon.GetAccount(accountID, &mAPI)
if err != nil {
fmt.Println("Failed to get account.")
os.Exit(1)
Long: "Get a list of followers for the current account.",
PreRun: func(cmd *cobra.Command, args []string) {
- err := account.VerifyCredentials(host, token)
+ err := account.VerifyCredentials(&mAPI)
if err != nil {
fmt.Println("Failed to verify credentials.")
os.Exit(1)
},
Run: func(cmd *cobra.Command, args []string) {
- followers, err := account.GetFollowers(host, token)
+ followers, err := account.GetFollowers(&mAPI)
if err != nil {
panic(err.Error())
}
Long: "Get a list of followed accounts for the current account.",
PreRun: func(cmd *cobra.Command, args []string) {
- err := account.VerifyCredentials(host, token)
+ err := account.VerifyCredentials(&mAPI)
if err != nil {
fmt.Println("Failed to verify credentials.")
os.Exit(1)
},
Run: func(cmd *cobra.Command, args []string) {
- following, err := account.GetFollowing(host, token)
+ following, err := account.GetFollowing(&mAPI)
if err != nil {
fmt.Println("Failed to get following list.")
os.Exit(1)
Long: "Fetch all lists the account owns",
PreRun: func(cmd *cobra.Command, args []string) {
- err := account.VerifyCredentials(host, token)
+ err := account.VerifyCredentials(&mAPI)
if err != nil {
fmt.Println("Failed to verify credentials.")
os.Exit(1)
},
Run: func(cmd *cobra.Command, args []string) {
- lists, err := account.GetLists(host, token)
+ lists, err := account.GetLists(&mAPI)
if err != nil {
fmt.Println("Failed to get following list.")
os.Exit(1)
Long: "Fetch statuses posted by account.",
PreRun: func(cmd *cobra.Command, args []string) {
- err := account.VerifyCredentials(host, token)
+ err := account.VerifyCredentials(&mAPI)
if err != nil {
fmt.Println("Failed to verify credentials.")
os.Exit(1)
},
Run: func(cmd *cobra.Command, args []string) {
- statuses, err := account.GetStatuses(host, token)
+ statuses, err := account.GetStatuses(&mAPI)
if err != nil {
fmt.Println("Failed to retrieve statuses.")
os.Exit(1)
},
PreRun: func(cmd *cobra.Command, args []string) {
- err := account.VerifyCredentials(host, token)
+ err := account.VerifyCredentials(&mAPI)
if err != nil {
fmt.Printf("Unable to verify credentials: %s\n", err.Error())
return
},
Run: func(cmd *cobra.Command, args []string) {
- list, err := mastodon.GetList(args[0], host, token)
+ list, err := mastodon.GetList(args[0], &mAPI)
if err != nil {
panic(err.Error())
}
- accounts, err := list.GetAccounts(host, token)
+ accounts, err := list.GetAccounts(&mAPI)
if err != nil {
panic(err.Error())
}
Long: "Get lists containing a specified account",
PreRun: func(cmd *cobra.Command, args []string) {
- err := account.VerifyCredentials(host, token)
+ err := account.VerifyCredentials(&mAPI)
if err != nil {
panic(err.Error())
}
id = args[0]
}
- lists, err := mastodon.ListsContainingAccount(id, host, token)
+ lists, err := mastodon.ListsContainingAccount(id, &mAPI)
if err != nil {
panic(err.Error())
}
Website: "https://dead-tooter.vexingworkshop.com",
}
- app, err = mastodon.Create(host, client)
+ app, err = mastodon.Create(mAPI.Host, client)
if err != nil {
fmt.Printf("Failed to create client: %s\n", err.Error())
}
}
var account mastodon.Account
- code, err := account.Authorize(host, app)
+ code, err := account.Authorize(mAPI.Host, app)
if err != nil {
fmt.Printf("Failed to authorize account: %s\n", err.Error())
}
- token, err = account.RequestToken(host, app, code)
+ mAPI.Token, err = account.RequestToken(mAPI.Host, app, code)
if err != nil {
fmt.Printf("Failed to acquire request token: %s\n", err.Error())
}
- err = token.Save()
+ err = mAPI.Token.Save()
if err != nil {
fmt.Printf("Failed to store access token: %s\n", err.Error())
}
- err = account.VerifyCredentials(host, token)
+ err = account.VerifyCredentials(&mAPI)
if err != nil {
fmt.Printf("Failed to verify credentials: %s\n", err.Error())
}
"github.com/spf13/cobra"
)
-var host string
-var token api.Token
-var proxyStr string
+var mAPI = api.API{}
func init() {
- rootCmd.PersistentFlags().StringVarP(&host,
+ rootCmd.PersistentFlags().StringVarP(&mAPI.Host,
"host", "H", "", "Mastodon host where your account lives.")
rootCmd.MarkPersistentFlagRequired("host")
- rootCmd.PersistentFlags().StringVarP(&proxyStr,
+ rootCmd.PersistentFlags().StringVarP(&mAPI.ProxyURL,
"proxy", "", "", "Address of proxy server.")
}
PersistentPreRun: func(cmd *cobra.Command, args []string) {
var err error
- token, err = api.LoadToken()
+ mAPI.Token, err = api.LoadToken()
if err != nil {
fmt.Println("No authentication token found.")
}
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
- err := token.Save()
+ err := mAPI.Token.Save()
if err != nil {
panic(err.Error())
}
// VerifyCredentials hydrates the account object by validating the bearer token
// against the Mastodon API
-func (a *Account) VerifyCredentials(
- host string, token api.Token, proxy string) (err error) {
+func (a *Account) VerifyCredentials(mAPI *api.API) (err error) {
u := url.URL{
- Host: host,
+ Host: mAPI.Host,
Path: "api/v1/accounts/verify_credentials",
}
u.Scheme = "https"
- body, err := api.Get(u, host, token, proxy)
+ body, err := api.Get(u, mAPI)
if err != nil {
+ fmt.Printf("%+v", err)
return err
}
}
// GetAccount returns the details of account specified by ID.
-func GetAccount(
- ID string, host string, token api.Token, proxy string) (account Account, err error) {
+func GetAccount(ID string, mAPI *api.API) (account Account, err error) {
id := url.PathEscape(ID)
path, err := url.JoinPath("api/v1/accounts", id)
}
u := url.URL{
- Host: host,
+ Host: mAPI.Host,
Path: path,
}
u.Scheme = "https"
- body, err := api.Get(u, host, token, proxy)
+ body, err := api.Get(u, mAPI)
if err != nil {
return account, err
}
}
// Get returns the currently logged in account.
-func (a *Account) Get(
- host string, token api.Token, proxy string) (account Account, err error) {
- return GetAccount(a.ID, host, token, proxy)
+func (a *Account) Get(mAPI *api.API) (account Account, err error) {
+ return GetAccount(a.ID, mAPI)
}
// GetFollowers returns a list of all accounts following the logged in user
-func (a *Account) GetFollowers(
- host string, token api.Token, proxy string) (followers AccountCollection, err error) {
+func (a *Account) GetFollowers(mAPI *api.API) (followers AccountCollection, err error) {
id := url.PathEscape(a.ID)
path, err := url.JoinPath("api/v1/accounts", id, "followers")
}
u := url.URL{
- Host: host,
+ Host: mAPI.Host,
Path: path,
}
u.Scheme = "https"
- body, err := api.Get(u, host, token, proxy)
+ body, err := api.Get(u, mAPI)
if err != nil {
return followers, err
}
}
// GetFollowing returns a list of all accounts followed by the logged in user
-func (a *Account) GetFollowing(
- host string, token api.Token, proxy string) (following AccountCollection, err error) {
+func (a *Account) GetFollowing(mAPI *api.API) (following AccountCollection, err error) {
id := url.PathEscape(a.ID)
path, err := url.JoinPath("api/v1/accounts", id, "following")
}
u := url.URL{
- Host: host,
+ Host: mAPI.Host,
Path: path,
}
u.Scheme = "https"
- body, err := api.Get(u, host, token, proxy)
+ body, err := api.Get(u, mAPI)
if err != nil {
return
}
}
// GetLists fetches all lists the account owns
-func (a *Account) GetLists(
- host string, token api.Token, proxy string) (lists ListCollection, err error) {
+func (a *Account) GetLists(mAPI *api.API) (lists ListCollection, err error) {
u := url.URL{
- Host: host,
+ Host: mAPI.Host,
Path: "api/v1/lists",
}
u.Scheme = "https"
- body, err := api.Get(u, host, token, proxy)
+ body, err := api.Get(u, mAPI)
if err != nil {
return
}
}
// GetStatuses fetches a list of statuses by the account.
-func (a *Account) GetStatuses(
- host string, token api.Token, proxy string) (statuses StatusCollection, err error) {
+func (a *Account) GetStatuses(mAPI *api.API) (statuses StatusCollection, err error) {
id := url.PathEscape(a.ID)
path, err := url.JoinPath("api/v1/accounts", id, "statuses")
}
u := url.URL{
- Host: host,
+ Host: mAPI.Host,
Path: path,
}
u.Scheme = "https"
- body, err := api.Get(u, host, token, proxy)
+ body, err := api.Get(u, mAPI)
if err != nil {
return
}
// API provides connection details necessary for all API requests.
type API struct {
- host string
- token Token
- proxyURL url.URL
+ Host string
+ Token Token
+ ProxyURL string
}
func getClient(proxyStr string) (client *http.Client, err error) {
proxyURL, err := url.Parse(proxyStr)
+ if proxyURL.String() == "" {
+ return &http.Client{}, nil
+ }
if err != nil {
- client = &http.Client{}
return
}
}
// Get provides a request
-func Get(u url.URL, host string, token Token, proxyStr string) (body []byte, err error) {
- client, err := getClient(proxyStr)
+func Get(u url.URL, mAPI *API) (body []byte, err error) {
+ client, err := getClient(mAPI.ProxyURL)
if err != nil {
return
}
if err != nil {
return
}
- req.Header.Add("Authorization", "Bearer "+token.AccessToken)
+ req.Header.Add("Authorization", "Bearer "+mAPI.Token.AccessToken)
resp, err := client.Do(req)
if err != nil {
}
// GetList returns a list specified by id.
-func GetList(ID string, host string, token api.Token) (list List, err error) {
+func GetList(ID string, mAPI *api.API) (list List, err error) {
id := url.PathEscape(ID)
path, err := url.JoinPath("api/v1/lists", id)
if err != nil {
}
u := url.URL{
- Host: host,
+ Host: mAPI.Host,
Path: path,
}
u.Scheme = "https"
- body, err := api.Get(u, host, token)
+ body, err := api.Get(u, mAPI)
if err != nil {
return
}
// ListsContainingAccount returns lists containing this account.
func ListsContainingAccount(
- accountID string, host string, token api.Token) (lists ListCollection, err error) {
+ accountID string, mAPI *api.API) (lists ListCollection, err error) {
id := url.PathEscape(accountID)
path, err := url.JoinPath("api/v1/accounts", id, "lists")
}
u := url.URL{
- Host: host,
+ Host: mAPI.Host,
Path: path,
}
u.Scheme = "https"
- body, err := api.Get(u, host, token)
+ body, err := api.Get(u, mAPI)
if err != nil {
return
}
}
// GetAccounts returns the accounts associated with a list.
-func (l *List) GetAccounts(host string, token api.Token) (accounts AccountCollection, err error) {
+func (l *List) GetAccounts(mAPI *api.API) (accounts AccountCollection, err error) {
id := url.PathEscape(l.ID)
path, err := url.JoinPath("api/v1/lists", id, "accounts")
if err != nil {
}
u := url.URL{
- Host: host,
+ Host: mAPI.Host,
Path: path,
}
u.Scheme = "https"
- body, err := api.Get(u, host, token)
+ body, err := api.Get(u, mAPI)
if err != nil {
return
}