]> Vexing Labs - dead-tooter.git/commitdiff
Get account info command
authorAdam Shamblin <adam@vexingworkshop.com>
Wed, 21 Dec 2022 00:40:03 +0000 (17:40 -0700)
committerAdam Shamblin <adam@vexingworkshop.com>
Wed, 21 Dec 2022 00:40:03 +0000 (17:40 -0700)
README.md
cmd/account.go
pkg/mastodon/account.go

index 35b30ecf48329e8f70e2156135f580d67dfc5012..1819d506e062eadf9a4a5c4405cc91d12ec4f5cf 100644 (file)
--- a/README.md
+++ b/README.md
@@ -18,6 +18,33 @@ able to do with my Fedi data.
 
 ## Usage
 
+### Authenticate to a server
+
+
 ```
 dead-tooter login --host hackers.town
 ```
+
+The first time you call `login`, this will register with your chosen server and
+store application information in your `~/.config/` directory. Your default
+browser will open to a login page, or if you are logged in, an app authorization
+page where you will be presented with an authorization token. Copy the token and
+paste it at the prompt. A login token will be stored in the same directory.
+
+### Get account info
+
+```
+dead-tooter --host hackers.town account [id]
+```
+
+### Get a list of followers
+
+```
+dead-tooter --host hackers.town account followers
+```
+
+### Get a list of following
+
+```
+dead-tooter --host hackers.town account following
+```
index 4f4397291adfc4fd3260cf7e78d5941b4d01271a..963c8ee5ad55aab9664923442fdaab8113b29270 100644 (file)
@@ -10,8 +10,10 @@ import (
 var account mastodon.Account
 
 func init() {
+       accountCmd.AddCommand(getAccountCmd)
        accountCmd.AddCommand(getFollowersCmd)
        accountCmd.AddCommand(getFollowingCmd)
+
        rootCmd.AddCommand(accountCmd)
 }
 
@@ -21,6 +23,31 @@ var accountCmd = &cobra.Command{
        Long:  "Commands related to the logged in Mastodon account.",
 }
 
+var getAccountCmd = &cobra.Command{
+       Use:   "get [id]",
+       Short: "Get account info",
+       Long:  "Return information the account specified by [id]. If no id is supplied, return information on the current logged in user.",
+
+       Run: func(cmd *cobra.Command, args []string) {
+               err := account.VerifyCredentials(host, token)
+               if err != nil {
+                       panic(err.Error())
+               }
+
+               if len(args) < 1 {
+                       fmt.Printf("%+v", account)
+                       return
+               }
+
+               acct, err := account.Get(args[0], host, token)
+               if err != nil {
+                       panic(err.Error())
+               }
+
+               fmt.Printf("%+v", acct)
+       },
+}
+
 var getFollowersCmd = &cobra.Command{
        Use:   "followers",
        Short: "Get account followers",
index dcc0896c6dfb206ab1337b21c038c67e7f6c5ff4..75cbf9b5d7acc6148d3bdc20208eafb09815caef 100644 (file)
@@ -171,6 +171,53 @@ func (a *Account) VerifyCredentials(host string, token Token) (err error) {
        return
 }
 
+// Get returns the details of account specified by ID.
+func (a *Account) Get(
+       ID string, host string, token Token) (account Account, err error) {
+
+       client := &http.Client{}
+
+       id := url.PathEscape(ID)
+       path, err := url.JoinPath("api/v1/accounts", id)
+       if err != nil {
+               return
+       }
+
+       u := url.URL{
+               Host: host,
+               Path: path,
+       }
+       u.Scheme = "https"
+
+       req, err := http.NewRequest("GET", u.String(), nil)
+       if err != nil {
+               return
+       }
+       req.Header.Add("Authorization", "Bearer "+token.AccessToken)
+
+       resp, err := client.Do(req)
+       if err != nil {
+               return
+       }
+       if resp.StatusCode != 200 {
+               err = errors.New(resp.Status)
+               return
+       }
+       defer resp.Body.Close()
+
+       body, err := io.ReadAll(resp.Body)
+       if err != nil {
+               return
+       }
+
+       err = json.Unmarshal(body, &account)
+       if err != nil {
+               return
+       }
+
+       return
+}
+
 // GetFollowers returns a list of all accounts following the logged in user
 func (a *Account) GetFollowers(
        host string, token Token) (followers []Account, err error) {