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:
parent
f9f817eaa1
commit
0002aa347e
7 changed files with 132 additions and 85 deletions
76
cmd/enbas/account.go
Normal file
76
cmd/enbas/account.go
Normal 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
|
||||||
|
}
|
|
@ -12,7 +12,7 @@ type addCommand struct {
|
||||||
|
|
||||||
toResourceType string
|
toResourceType string
|
||||||
listID string
|
listID string
|
||||||
accountIDs accountIDs
|
accountNames accountNames
|
||||||
}
|
}
|
||||||
|
|
||||||
func newAddCommand(name, summary string) *addCommand {
|
func newAddCommand(name, summary string) *addCommand {
|
||||||
|
@ -20,12 +20,12 @@ func newAddCommand(name, summary string) *addCommand {
|
||||||
|
|
||||||
command := addCommand{
|
command := addCommand{
|
||||||
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
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.toResourceType, addToFlag, "", "specify the type of resource to add to")
|
||||||
command.StringVar(&command.listID, listIDFlag, "", "the ID of the list 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)
|
command.Usage = commandUsageFunc(name, summary, command.FlagSet)
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ func newAddCommand(name, summary string) *addCommand {
|
||||||
|
|
||||||
func (c *addCommand) Execute() error {
|
func (c *addCommand) Execute() error {
|
||||||
if c.toResourceType == "" {
|
if c.toResourceType == "" {
|
||||||
return flagNotSetError{flagText: "add-to"}
|
return flagNotSetError{flagText: addToFlag}
|
||||||
}
|
}
|
||||||
|
|
||||||
funcMap := map[string]func(*client.Client) error{
|
funcMap := map[string]func(*client.Client) error{
|
||||||
|
@ -59,11 +59,21 @@ func (c *addCommand) addAccountsToList(gtsClient *client.Client) error {
|
||||||
return flagNotSetError{flagText: listIDFlag}
|
return flagNotSetError{flagText: listIDFlag}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(c.accountIDs) == 0 {
|
if len(c.accountNames) == 0 {
|
||||||
return noAccountIDsSpecifiedError{}
|
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)
|
return fmt.Errorf("unable to add the accounts to the list; %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -32,8 +32,8 @@ func (e unknownSubcommandError) Error() string {
|
||||||
return "unknown subcommand '" + e.subcommand + "'"
|
return "unknown subcommand '" + e.subcommand + "'"
|
||||||
}
|
}
|
||||||
|
|
||||||
type noAccountIDsSpecifiedError struct{}
|
type noAccountSpecifiedError struct{}
|
||||||
|
|
||||||
func (e noAccountIDsSpecifiedError) Error() string {
|
func (e noAccountSpecifiedError) Error() string {
|
||||||
return "no account IDs specified"
|
return "no account specified in this request"
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,13 +2,13 @@ package main
|
||||||
|
|
||||||
import "strings"
|
import "strings"
|
||||||
|
|
||||||
type accountIDs []string
|
type accountNames []string
|
||||||
|
|
||||||
func (a *accountIDs) String() string {
|
func (a *accountNames) String() string {
|
||||||
return strings.Join(*a, ", ")
|
return strings.Join(*a, ", ")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *accountIDs) Set(value string) error {
|
func (a *accountNames) Set(value string) error {
|
||||||
if len(value) > 0 {
|
if len(value) > 0 {
|
||||||
*a = append(*a, value)
|
*a = append(*a, value)
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,15 +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 (
|
||||||
accountFlag = "account"
|
accountNameFlag = "account-name"
|
||||||
accountIDFlag = "account-id"
|
|
||||||
addToFlag = "add-to"
|
addToFlag = "add-to"
|
||||||
instanceFlag = "instance"
|
instanceFlag = "instance"
|
||||||
listIDFlag = "list-id"
|
listIDFlag = "list-id"
|
||||||
|
@ -151,19 +146,3 @@ 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
|
|
||||||
}
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ type removeCommand struct {
|
||||||
|
|
||||||
fromResourceType string
|
fromResourceType string
|
||||||
listID string
|
listID string
|
||||||
accountIDs accountIDs
|
accountNames accountNames
|
||||||
}
|
}
|
||||||
|
|
||||||
func newRemoveCommand(name, summary string) *removeCommand {
|
func newRemoveCommand(name, summary string) *removeCommand {
|
||||||
|
@ -20,12 +20,12 @@ func newRemoveCommand(name, summary string) *removeCommand {
|
||||||
|
|
||||||
command := removeCommand{
|
command := removeCommand{
|
||||||
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
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.fromResourceType, removeFromFlag, "", "specify the type of resource to remove from")
|
||||||
command.StringVar(&command.listID, listIDFlag, "", "the ID of the list 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)
|
command.Usage = commandUsageFunc(name, summary, command.FlagSet)
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ func newRemoveCommand(name, summary string) *removeCommand {
|
||||||
|
|
||||||
func (c *removeCommand) Execute() error {
|
func (c *removeCommand) Execute() error {
|
||||||
if c.fromResourceType == "" {
|
if c.fromResourceType == "" {
|
||||||
return flagNotSetError{flagText: "remove-from"}
|
return flagNotSetError{flagText: removeFromFlag}
|
||||||
}
|
}
|
||||||
|
|
||||||
funcMap := map[string]func(*client.Client) error{
|
funcMap := map[string]func(*client.Client) error{
|
||||||
|
@ -59,11 +59,21 @@ func (c *removeCommand) removeAccountsFromList(gtsClient *client.Client) error {
|
||||||
return flagNotSetError{flagText: listIDFlag}
|
return flagNotSetError{flagText: listIDFlag}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(c.accountIDs) == 0 {
|
if len(c.accountNames) == 0 {
|
||||||
return noAccountIDsSpecifiedError{}
|
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)
|
return fmt.Errorf("unable to remove the accounts from the list; %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,8 +15,7 @@ type showCommand struct {
|
||||||
showAccountRelationship bool
|
showAccountRelationship bool
|
||||||
showUserPreferences bool
|
showUserPreferences bool
|
||||||
resourceType string
|
resourceType string
|
||||||
account string
|
accountName string
|
||||||
accountID string
|
|
||||||
statusID string
|
statusID string
|
||||||
timelineCategory string
|
timelineCategory string
|
||||||
listID 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.showAccountRelationship, showAccountRelationshipFlag, false, "show your relationship to the specified account")
|
||||||
command.BoolVar(&command.showUserPreferences, showUserPreferencesFlag, false, "show your preferences")
|
command.BoolVar(&command.showUserPreferences, showUserPreferencesFlag, false, "show your preferences")
|
||||||
command.StringVar(&command.resourceType, resourceTypeFlag, "", "specify the type of resource to display")
|
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.accountName, accountNameFlag, "", "specify the account name in full (username@domain)")
|
||||||
command.StringVar(&command.accountID, accountIDFlag, "", "specify the account ID")
|
|
||||||
command.StringVar(&command.statusID, statusIDFlag, "", "specify the ID of the status to display")
|
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.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")
|
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 {
|
if c.myAccount {
|
||||||
account, err = getMyAccount(gts)
|
account, err = getMyAccount(gts)
|
||||||
if err != nil {
|
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 {
|
} else {
|
||||||
if c.account == "" {
|
if c.accountName == "" {
|
||||||
return flagNotSetError{flagText: accountFlag}
|
return flagNotSetError{flagText: accountNameFlag}
|
||||||
}
|
}
|
||||||
|
|
||||||
accountURI := c.account
|
account, err = getAccount(gts, c.accountName)
|
||||||
|
|
||||||
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("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 {
|
func (c *showCommand) showFollowers(gts *client.Client) error {
|
||||||
var accountID string
|
accountID, err := getAccountID(gts, c.myAccount, c.accountName)
|
||||||
|
|
||||||
if c.myAccount {
|
|
||||||
account, err := getMyAccount(gts)
|
|
||||||
if err != nil {
|
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 ID; %w", err)
|
||||||
}
|
|
||||||
|
|
||||||
accountID = account.ID
|
|
||||||
} else {
|
|
||||||
if c.accountID == "" {
|
|
||||||
return flagNotSetError{flagText: accountIDFlag}
|
|
||||||
}
|
|
||||||
|
|
||||||
accountID = c.accountID
|
|
||||||
}
|
}
|
||||||
|
|
||||||
followers, err := gts.GetFollowers(accountID, c.limit)
|
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 {
|
func (c *showCommand) showFollowing(gts *client.Client) error {
|
||||||
var accountID string
|
accountID, err := getAccountID(gts, c.myAccount, c.accountName)
|
||||||
|
|
||||||
if c.myAccount {
|
|
||||||
account, err := getMyAccount(gts)
|
|
||||||
if err != nil {
|
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 ID; %w", err)
|
||||||
}
|
|
||||||
|
|
||||||
accountID = account.ID
|
|
||||||
} else {
|
|
||||||
if c.accountID == "" {
|
|
||||||
return flagNotSetError{flagText: accountIDFlag}
|
|
||||||
}
|
|
||||||
|
|
||||||
accountID = c.accountID
|
|
||||||
}
|
}
|
||||||
|
|
||||||
following, err := gts.GetFollowing(accountID, c.limit)
|
following, err := gts.GetFollowing(accountID, c.limit)
|
||||||
|
|
Loading…
Reference in a new issue