checkpoint: get account ID from account name

- Added new functionality to get account ID from account name
- Users know can use account names in their requests instead of using
  the account IDs.
This commit is contained in:
Dan Anglin 2024-05-21 19:53:35 +01:00
parent f9f817eaa1
commit 0002aa347e
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
7 changed files with 132 additions and 85 deletions

76
cmd/enbas/account.go Normal file
View file

@ -0,0 +1,76 @@
package main
import (
"fmt"
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
"codeflow.dananglin.me.uk/apollo/enbas/internal/config"
"codeflow.dananglin.me.uk/apollo/enbas/internal/model"
)
func getAccountID(gts *client.Client, myAccount bool, accountName string) (string, error) {
var (
accountID string
err error
)
switch {
case myAccount:
accountID, err = getMyAccountID(gts)
if err != nil {
return "", fmt.Errorf("unable to get your account ID; %w", err)
}
case accountName != "":
accountID, err = getTheirAccountID(gts, accountName)
if err != nil {
return "", fmt.Errorf("unable to get their account ID; %w", err)
}
default:
return "", noAccountSpecifiedError{}
}
return accountID, nil
}
func getTheirAccountID(gts *client.Client, accountURI string) (string, error) {
account, err := getAccount(gts, accountURI)
if err != nil {
return "", fmt.Errorf("unable to retrieve your account; %w", err)
}
return account.ID, nil
}
func getMyAccountID(gts *client.Client) (string, error) {
account, err := getMyAccount(gts)
if err != nil {
return "", fmt.Errorf("received an error while getting your account details; %w", err)
}
return account.ID, 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 := getAccount(gts, accountURI)
if err != nil {
return model.Account{}, fmt.Errorf("unable to retrieve your account; %w", err)
}
return account, nil
}
func getAccount(gts *client.Client, accountURI string) (model.Account, error) {
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

@ -12,7 +12,7 @@ type addCommand struct {
toResourceType string
listID string
accountIDs accountIDs
accountNames accountNames
}
func newAddCommand(name, summary string) *addCommand {
@ -20,12 +20,12 @@ func newAddCommand(name, summary string) *addCommand {
command := addCommand{
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
accountIDs: accountIDs(emptyArr),
accountNames: accountNames(emptyArr),
}
command.StringVar(&command.toResourceType, addToFlag, "", "specify the type of resource to add to")
command.StringVar(&command.listID, listIDFlag, "", "the ID of the list to add to")
command.Var(&command.accountIDs, accountIDFlag, "the ID of the account to add to the list")
command.Var(&command.accountNames, accountNameFlag, "the name of the account to add to the resource")
command.Usage = commandUsageFunc(name, summary, command.FlagSet)
@ -34,7 +34,7 @@ func newAddCommand(name, summary string) *addCommand {
func (c *addCommand) Execute() error {
if c.toResourceType == "" {
return flagNotSetError{flagText: "add-to"}
return flagNotSetError{flagText: addToFlag}
}
funcMap := map[string]func(*client.Client) error{
@ -59,11 +59,21 @@ func (c *addCommand) addAccountsToList(gtsClient *client.Client) error {
return flagNotSetError{flagText: listIDFlag}
}
if len(c.accountIDs) == 0 {
return noAccountIDsSpecifiedError{}
if len(c.accountNames) == 0 {
return noAccountSpecifiedError{}
}
if err := gtsClient.AddAccountsToList(c.listID, []string(c.accountIDs)); err != nil {
accountIDs := make([]string, len(c.accountNames))
for i := range c.accountNames {
accountID, err := getTheirAccountID(gtsClient, c.accountNames[i])
if err != nil {
return fmt.Errorf("unable to get the account ID for %s, %w", c.accountNames[i], err)
}
accountIDs[i] = accountID
}
if err := gtsClient.AddAccountsToList(c.listID, accountIDs); err != nil {
return fmt.Errorf("unable to add the accounts to the list; %w", err)
}

View file

@ -32,8 +32,8 @@ func (e unknownSubcommandError) Error() string {
return "unknown subcommand '" + e.subcommand + "'"
}
type noAccountIDsSpecifiedError struct{}
type noAccountSpecifiedError struct{}
func (e noAccountIDsSpecifiedError) Error() string {
return "no account IDs specified"
func (e noAccountSpecifiedError) Error() string {
return "no account specified in this request"
}

View file

@ -2,13 +2,13 @@ package main
import "strings"
type accountIDs []string
type accountNames []string
func (a *accountIDs) String() string {
func (a *accountNames) String() string {
return strings.Join(*a, ", ")
}
func (a *accountIDs) Set(value string) error {
func (a *accountNames) Set(value string) error {
if len(value) > 0 {
*a = append(*a, value)
}

View file

@ -4,15 +4,10 @@ import (
"flag"
"fmt"
"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 (
accountFlag = "account"
accountIDFlag = "account-id"
accountNameFlag = "account-name"
addToFlag = "add-to"
instanceFlag = "instance"
listIDFlag = "list-id"
@ -151,19 +146,3 @@ func run() error {
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

@ -12,7 +12,7 @@ type removeCommand struct {
fromResourceType string
listID string
accountIDs accountIDs
accountNames accountNames
}
func newRemoveCommand(name, summary string) *removeCommand {
@ -20,12 +20,12 @@ func newRemoveCommand(name, summary string) *removeCommand {
command := removeCommand{
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
accountIDs: accountIDs(emptyArr),
accountNames: accountNames(emptyArr),
}
command.StringVar(&command.fromResourceType, removeFromFlag, "", "specify the type of resource to remove from")
command.StringVar(&command.listID, listIDFlag, "", "the ID of the list to remove from")
command.Var(&command.accountIDs, accountIDFlag, "the ID of the account to remove from the list")
command.Var(&command.accountNames, accountNameFlag, "the name of the account to remove from the resource")
command.Usage = commandUsageFunc(name, summary, command.FlagSet)
@ -34,7 +34,7 @@ func newRemoveCommand(name, summary string) *removeCommand {
func (c *removeCommand) Execute() error {
if c.fromResourceType == "" {
return flagNotSetError{flagText: "remove-from"}
return flagNotSetError{flagText: removeFromFlag}
}
funcMap := map[string]func(*client.Client) error{
@ -59,11 +59,21 @@ func (c *removeCommand) removeAccountsFromList(gtsClient *client.Client) error {
return flagNotSetError{flagText: listIDFlag}
}
if len(c.accountIDs) == 0 {
return noAccountIDsSpecifiedError{}
if len(c.accountNames) == 0 {
return noAccountSpecifiedError{}
}
if err := gtsClient.RemoveAccountsFromList(c.listID, []string(c.accountIDs)); err != nil {
accountIDs := make([]string, len(c.accountNames))
for i := range c.accountNames {
accountID, err := getTheirAccountID(gtsClient, c.accountNames[i])
if err != nil {
return fmt.Errorf("unable to get the account ID for %s, %w", c.accountNames[i], err)
}
accountIDs[i] = accountID
}
if err := gtsClient.RemoveAccountsFromList(c.listID, accountIDs); err != nil {
return fmt.Errorf("unable to remove the accounts from the list; %w", err)
}

View file

@ -15,8 +15,7 @@ type showCommand struct {
showAccountRelationship bool
showUserPreferences bool
resourceType string
account string
accountID string
accountName string
statusID string
timelineCategory string
listID string
@ -33,8 +32,7 @@ func newShowCommand(name, summary string) *showCommand {
command.BoolVar(&command.showAccountRelationship, showAccountRelationshipFlag, false, "show your relationship to the specified account")
command.BoolVar(&command.showUserPreferences, showUserPreferencesFlag, false, "show your preferences")
command.StringVar(&command.resourceType, resourceTypeFlag, "", "specify the type of resource to display")
command.StringVar(&command.account, accountFlag, "", "specify the account URI to lookup")
command.StringVar(&command.accountID, accountIDFlag, "", "specify the account ID")
command.StringVar(&command.accountName, accountNameFlag, "", "specify the account name in full (username@domain)")
command.StringVar(&command.statusID, statusIDFlag, "", "specify the ID of the status to display")
command.StringVar(&command.timelineCategory, timelineCategoryFlag, "home", "specify the type of timeline to display (valid values are home, public, list and tag)")
command.StringVar(&command.listID, listIDFlag, "", "specify the ID of the list to display")
@ -95,18 +93,16 @@ func (c *showCommand) showAccount(gts *client.Client) error {
if c.myAccount {
account, err = getMyAccount(gts)
if err != nil {
return fmt.Errorf("received an error while getting account details; %w", err)
return fmt.Errorf("received an error while getting the account details; %w", err)
}
} else {
if c.account == "" {
return flagNotSetError{flagText: accountFlag}
if c.accountName == "" {
return flagNotSetError{flagText: accountNameFlag}
}
accountURI := c.account
account, err = gts.GetAccount(accountURI)
account, err = getAccount(gts, c.accountName)
if err != nil {
return fmt.Errorf("unable to retrieve the account details; %w", err)
return fmt.Errorf("received an error while getting the account details; %w", err)
}
}
@ -238,21 +234,9 @@ func (c *showCommand) showLists(gts *client.Client) error {
}
func (c *showCommand) showFollowers(gts *client.Client) error {
var accountID string
if c.myAccount {
account, err := getMyAccount(gts)
accountID, err := getAccountID(gts, c.myAccount, c.accountName)
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
return fmt.Errorf("received an error while getting the account ID; %w", err)
}
followers, err := gts.GetFollowers(accountID, c.limit)
@ -270,21 +254,9 @@ func (c *showCommand) showFollowers(gts *client.Client) error {
}
func (c *showCommand) showFollowing(gts *client.Client) error {
var accountID string
if c.myAccount {
account, err := getMyAccount(gts)
accountID, err := getAccountID(gts, c.myAccount, c.accountName)
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
return fmt.Errorf("received an error while getting the account ID; %w", err)
}
following, err := gts.GetFollowing(accountID, c.limit)