Allow HTTP URLs in --user-id
authorMikael Berthe <mikael@lilotux.net>
Mon, 08 May 2017 12:17:59 +0200
changeset 102 84ad56b643c8
parent 101 aeaae086db13
child 103 7a4604bb12d9
Allow HTTP URLs in --user-id E.g. madonctl accounts --user-id https://server/@username show
README.md
cmd/accounts.go
--- a/README.md	Mon May 08 10:56:17 2017 +0200
+++ b/README.md	Mon May 08 12:17:59 2017 +0200
@@ -115,7 +115,8 @@
 You can specify the (instance-specific) account ID number (--account-id) or
 the user ID (--user-id).  In the later case, madonctl will search for the
 user so it must match exactly the ID known to your instance (without the
-@domain suffix if the user is on the same instance).
+@domain suffix if the user is on the same instance).  With madonctl v0.6.1+,
+the --user-id flag can also contain an HTTP account URL.
 
 Display accounts you're following or your followers:
 ``` sh
@@ -184,7 +185,7 @@
 % madonctl accounts --account-id 1 followers --template '{{.acct}}{{"\n"}}'
 ```
 
-Number of users on current instance (madonctl 0.5+) (statistics from instances.mastodon.xyz API):
+Number of users on current instance (statistics from instances.mastodon.xyz API):
 ```
 madonctl instance --stats --template '{{printf "%v\n" .users}}'
 ```
--- a/cmd/accounts.go	Mon May 08 10:56:17 2017 +0200
+++ b/cmd/accounts.go	Mon May 08 12:17:59 2017 +0200
@@ -248,30 +248,20 @@
 		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 {
+		var err error
+		if err = madonInit(true); err != nil {
 			return err
 		}
-		accList, err := gClient.SearchAccounts(opt.accountUID, &madon.LimitParams{Limit: 2})
-		if err != nil || len(accList) < 1 {
-			errPrint("Cannot find user '%s': %v", opt.accountUID, err)
+		opt.accountID, err = accountLookupUser(opt.accountUID)
+		if err != nil || opt.accountID < 1 {
+			if err != nil {
+				errPrint("Cannot find user '%s': %v", opt.accountUID, err)
+			} else {
+				errPrint("Cannot find user '%s'", opt.accountUID)
+			}
 			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 {
@@ -543,3 +533,47 @@
 	}
 	return p.PrintObj(obj, nil, "")
 }
+
+// accountLookupUser tries to find a (single) user matching 'user'
+// If the user is an HTTP URL, it will use the search API, else
+// it will use the accounts/search API.
+func accountLookupUser(user string) (int64, error) {
+	var accID int64
+
+	if strings.HasPrefix(user, "https://") || strings.HasPrefix(user, "http://") {
+		res, err := gClient.Search(user, true)
+		if err != nil {
+			return 0, err
+		}
+		if res != nil {
+			if len(res.Accounts) > 1 {
+				return 0, errors.New("several results")
+			}
+			if len(res.Accounts) == 1 {
+				accID = res.Accounts[0].ID
+			}
+		}
+	} else {
+		// Remove leading '@'
+		user = strings.TrimLeft(user, "@")
+
+		accList, err := gClient.SearchAccounts(user, &madon.LimitParams{Limit: 2})
+		if err != nil {
+			return 0, err
+		}
+		for _, u := range accList {
+			if u.Acct == user {
+				accID = u.ID
+				break
+			}
+		}
+	}
+
+	if accID < 1 {
+		return 0, errors.New("user not found")
+	}
+	if verbose {
+		errPrint("User '%s' is account ID %d", user, user)
+	}
+	return accID, nil
+}