fix: use my-account flag for showing followers

Allow the use of the my-account flag when showing followers
and following accounts for the authenticated user.
This commit is contained in:
Dan Anglin 2024-05-20 18:37:29 +01:00
parent 1a95384ba0
commit de5cf65aa8
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
2 changed files with 63 additions and 17 deletions

View file

@ -4,6 +4,10 @@ import (
"flag" "flag"
"fmt" "fmt"
"os" "os"
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
"codeflow.dananglin.me.uk/apollo/enbas/internal/config"
"codeflow.dananglin.me.uk/apollo/enbas/internal/model"
) )
const ( const (
@ -136,3 +140,19 @@ func run() error {
return nil return nil
} }
func getMyAccount(gts *client.Client) (model.Account, error) {
authConfig, err := config.NewAuthenticationConfigFromFile()
if err != nil {
return model.Account{}, fmt.Errorf("unable to retrieve the authentication configuration; %w", err)
}
accountURI := authConfig.CurrentAccount
account, err := gts.GetAccount(accountURI)
if err != nil {
return model.Account{}, fmt.Errorf("unable to retrieve the account details; %w", err)
}
return account, nil
}

View file

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"codeflow.dananglin.me.uk/apollo/enbas/internal/client" "codeflow.dananglin.me.uk/apollo/enbas/internal/client"
"codeflow.dananglin.me.uk/apollo/enbas/internal/config"
"codeflow.dananglin.me.uk/apollo/enbas/internal/model" "codeflow.dananglin.me.uk/apollo/enbas/internal/model"
"codeflow.dananglin.me.uk/apollo/enbas/internal/utilities" "codeflow.dananglin.me.uk/apollo/enbas/internal/utilities"
) )
@ -85,26 +84,27 @@ func (c *showCommand) showInstance(gts *client.Client) error {
} }
func (c *showCommand) showAccount(gts *client.Client) error { func (c *showCommand) showAccount(gts *client.Client) error {
var accountURI string var (
account model.Account
err error
)
if c.myAccount { if c.myAccount {
authConfig, err := config.NewAuthenticationConfigFromFile() account, err = getMyAccount(gts)
if err != nil { if err != nil {
return fmt.Errorf("unable to retrieve the authentication configuration; %w", err) return fmt.Errorf("received an error while getting account details; %w", err)
} }
accountURI = authConfig.CurrentAccount
} else { } else {
if c.account == "" { if c.account == "" {
return flagNotSetError{flagText: accountFlag} return flagNotSetError{flagText: accountFlag}
} }
accountURI = c.account accountURI := c.account
}
account, err := gts.GetAccount(accountURI) account, err = gts.GetAccount(accountURI)
if err != nil { if err != nil {
return fmt.Errorf("unable to retrieve the account details; %w", err) return fmt.Errorf("unable to retrieve the account details; %w", err)
}
} }
fmt.Println(account) fmt.Println(account)
@ -226,11 +226,24 @@ func (c *showCommand) showLists(gts *client.Client) error {
} }
func (c *showCommand) showFollowers(gts *client.Client) error { func (c *showCommand) showFollowers(gts *client.Client) error {
if c.accountID == "" { var accountID string
return flagNotSetError{flagText: accountIDFlag}
if c.myAccount {
account, err := getMyAccount(gts)
if err != nil {
return fmt.Errorf("received an error while getting account details; %w", err)
}
accountID = account.ID
} else {
if c.accountID == "" {
return flagNotSetError{flagText: accountIDFlag}
}
accountID = c.accountID
} }
followers, err := gts.GetFollowers(c.accountID, c.limit) followers, err := gts.GetFollowers(accountID, c.limit)
if err != nil { if err != nil {
return fmt.Errorf("unable to retrieve the list of followers; %w", err) return fmt.Errorf("unable to retrieve the list of followers; %w", err)
} }
@ -245,11 +258,24 @@ func (c *showCommand) showFollowers(gts *client.Client) error {
} }
func (c *showCommand) showFollowing(gts *client.Client) error { func (c *showCommand) showFollowing(gts *client.Client) error {
if c.accountID == "" { var accountID string
return flagNotSetError{flagText: accountIDFlag}
if c.myAccount {
account, err := getMyAccount(gts)
if err != nil {
return fmt.Errorf("received an error while getting account details; %w", err)
}
accountID = account.ID
} else {
if c.accountID == "" {
return flagNotSetError{flagText: accountIDFlag}
}
accountID = c.accountID
} }
following, err := gts.GetFollowing(c.accountID, c.limit) following, err := gts.GetFollowing(accountID, c.limit)
if err != nil { if err != nil {
return fmt.Errorf("unable to retrieve the list of followed accounts; %w", err) return fmt.Errorf("unable to retrieve the list of followed accounts; %w", err)
} }