cmd/accounts.go
changeset 102 84ad56b643c8
parent 81 b1671f83e91b
child 110 57843255fd1a
equal deleted inserted replaced
101:aeaae086db13 102:84ad56b643c8
   246 
   246 
   247 	if opt.accountUID != "" {
   247 	if opt.accountUID != "" {
   248 		if opt.accountID > 0 {
   248 		if opt.accountID > 0 {
   249 			return errors.New("cannot use both account ID and UID")
   249 			return errors.New("cannot use both account ID and UID")
   250 		}
   250 		}
   251 		// Remove leading '@'
       
   252 		opt.accountUID = strings.TrimLeft(opt.accountUID, "@")
       
   253 		// Sign in early to look the user id up
   251 		// Sign in early to look the user id up
   254 		if err := madonInit(true); err != nil {
   252 		var err error
       
   253 		if err = madonInit(true); err != nil {
   255 			return err
   254 			return err
   256 		}
   255 		}
   257 		accList, err := gClient.SearchAccounts(opt.accountUID, &madon.LimitParams{Limit: 2})
   256 		opt.accountID, err = accountLookupUser(opt.accountUID)
   258 		if err != nil || len(accList) < 1 {
   257 		if err != nil || opt.accountID < 1 {
   259 			errPrint("Cannot find user '%s': %v", opt.accountUID, err)
   258 			if err != nil {
       
   259 				errPrint("Cannot find user '%s': %v", opt.accountUID, err)
       
   260 			} else {
       
   261 				errPrint("Cannot find user '%s'", opt.accountUID)
       
   262 			}
   260 			os.Exit(1)
   263 			os.Exit(1)
   261 		}
       
   262 		for _, u := range accList {
       
   263 			if u.Acct == opt.accountUID {
       
   264 				opt.accountID = u.ID
       
   265 				break
       
   266 			}
       
   267 		}
       
   268 		if opt.accountID < 1 {
       
   269 			errPrint("Cannot find user '%s'", opt.accountUID)
       
   270 			os.Exit(1)
       
   271 		}
       
   272 		if verbose {
       
   273 			errPrint("User '%s' is account ID %d", opt.accountUID, opt.accountID)
       
   274 		}
   264 		}
   275 	}
   265 	}
   276 
   266 
   277 	switch subcmd {
   267 	switch subcmd {
   278 	case "show", "search", "update":
   268 	case "show", "search", "update":
   541 		errPrint("Error: %s", err.Error())
   531 		errPrint("Error: %s", err.Error())
   542 		os.Exit(1)
   532 		os.Exit(1)
   543 	}
   533 	}
   544 	return p.PrintObj(obj, nil, "")
   534 	return p.PrintObj(obj, nil, "")
   545 }
   535 }
       
   536 
       
   537 // accountLookupUser tries to find a (single) user matching 'user'
       
   538 // If the user is an HTTP URL, it will use the search API, else
       
   539 // it will use the accounts/search API.
       
   540 func accountLookupUser(user string) (int64, error) {
       
   541 	var accID int64
       
   542 
       
   543 	if strings.HasPrefix(user, "https://") || strings.HasPrefix(user, "http://") {
       
   544 		res, err := gClient.Search(user, true)
       
   545 		if err != nil {
       
   546 			return 0, err
       
   547 		}
       
   548 		if res != nil {
       
   549 			if len(res.Accounts) > 1 {
       
   550 				return 0, errors.New("several results")
       
   551 			}
       
   552 			if len(res.Accounts) == 1 {
       
   553 				accID = res.Accounts[0].ID
       
   554 			}
       
   555 		}
       
   556 	} else {
       
   557 		// Remove leading '@'
       
   558 		user = strings.TrimLeft(user, "@")
       
   559 
       
   560 		accList, err := gClient.SearchAccounts(user, &madon.LimitParams{Limit: 2})
       
   561 		if err != nil {
       
   562 			return 0, err
       
   563 		}
       
   564 		for _, u := range accList {
       
   565 			if u.Acct == user {
       
   566 				accID = u.ID
       
   567 				break
       
   568 			}
       
   569 		}
       
   570 	}
       
   571 
       
   572 	if accID < 1 {
       
   573 		return 0, errors.New("user not found")
       
   574 	}
       
   575 	if verbose {
       
   576 		errPrint("User '%s' is account ID %d", user, user)
       
   577 	}
       
   578 	return accID, nil
       
   579 }