accounts: add --user-id
authorMikael Berthe <mikael@lilotux.net>
Tue, 25 Apr 2017 00:05:21 +0200
changeset 16 8939959991b3
parent 15 8ac069eaa817
child 17 ab1aa9652791
accounts: add --user-id Look up the requested user ID and use it in place of --account-id. Currently the UID should match exactly the account's Acct field in order to avoid false positives.
cmd/accounts.go
--- a/cmd/accounts.go	Mon Apr 24 23:05:27 2017 +0200
+++ b/cmd/accounts.go	Tue Apr 25 00:05:21 2017 +0200
@@ -7,6 +7,7 @@
 
 import (
 	"errors"
+	"os"
 	"strings"
 
 	"github.com/spf13/cobra"
@@ -17,6 +18,7 @@
 
 var accountsOpts struct {
 	accountID                 int
+	accountUID                string
 	unset                     bool   // TODO remove eventually?
 	limit                     uint   // Limit the number of results
 	onlyMedia, excludeReplies bool   // For acccount statuses
@@ -41,6 +43,7 @@
 
 	// Global flags
 	accountsCmd.PersistentFlags().IntVarP(&accountsOpts.accountID, "account-id", "a", 0, "Account ID number")
+	accountsCmd.PersistentFlags().StringVarP(&accountsOpts.accountUID, "user-id", "u", "", "Account user ID")
 	accountsCmd.PersistentFlags().UintVarP(&accountsOpts.limit, "limit", "l", 0, "Limit number of results")
 
 	// Subcommand flags
@@ -236,6 +239,36 @@
 func accountSubcommandsRunE(subcmd string, args []string) error {
 	opt := accountsOpts
 
+	if opt.accountUID != "" {
+		if opt.accountID > 0 {
+			return errors.New("cannot use both account ID and UID")
+		}
+		// Remove leading '@'
+		opt.accountUID = strings.TrimLeft(opt.accountUID, "@")
+		// Sign in early to look the user id up
+		if err := madonInit(true); err != nil {
+			return err
+		}
+		accList, err := gClient.SearchAccounts(opt.accountUID, 2)
+		if err != nil || len(accList) < 1 {
+			errPrint("Cannot find user '%s': %v", opt.accountUID, err)
+			os.Exit(1)
+		}
+		for _, u := range accList {
+			if u.Acct == opt.accountUID {
+				opt.accountID = u.ID
+				break
+			}
+		}
+		if opt.accountID < 1 {
+			errPrint("Cannot find user '%s'", opt.accountUID)
+			os.Exit(1)
+		}
+		if verbose {
+			errPrint("User '%s' is account ID %d", opt.accountUID, opt.accountID)
+		}
+	}
+
 	switch subcmd {
 	case "show", "search", "update":
 		// These subcommands do not require an account ID