Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add account command #15

Merged
merged 3 commits into from
Jul 26, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add ability to look up another user to account command
Add an optional argument to the `account` command which allows it to look up
other users by their account ID.
  • Loading branch information
waits committed May 30, 2016
commit 2fcae7b7f82ee006fa9f3364dec407a04af827c6
67 changes: 52 additions & 15 deletions cmd/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,66 @@
package cmd

import (
"errors"
"fmt"
"os"
"text/tabwriter"

"github.com/dropbox/dropbox-sdk-go-unofficial/users"
"github.com/spf13/cobra"
)

func account(cmd *cobra.Command, args []string) error {
res, err := dbx.GetCurrentAccount()
if err != nil {
return err
// printFullAccount prints the account details returned by GetCurrentAccount
func printFullAccount(w *tabwriter.Writer, fa *users.FullAccount) {
fmt.Fprintf(w, "Logged in as %s <%s>\n\n", fa.Name.DisplayName, fa.Email)
fmt.Fprintf(w, "Account Id:\t%s\n", fa.AccountId)
fmt.Fprintf(w, "Account Type:\t%s\n", fa.AccountType.Tag)
fmt.Fprintf(w, "Locale:\t%s\n", fa.Locale)
fmt.Fprintf(w, "Referral Link:\t%s\n", fa.ReferralLink)
fmt.Fprintf(w, "Profile Photo Url:\t%s\n", fa.ProfilePhotoUrl)
fmt.Fprintf(w, "Paired Account:\t%t\n", fa.IsPaired)
if fa.Team != nil {
fmt.Fprintf(w, "Team:\n Name:\t%s\n Id:\t%s\n Member Id:\t%s\n", fa.Team.Name, fa.Team.Id, fa.TeamMemberId)
}
}

// printBasicAccount prints the account details returned by GetAccount
func printBasicAccount(w *tabwriter.Writer, ba *users.BasicAccount) {
fmt.Fprintf(w, "Name:\t%s\n", ba.Name.DisplayName)
if !ba.EmailVerified {
ba.Email += " (unverified)"
}
fmt.Fprintf(w, "Email:\t%s\n", ba.Email)
fmt.Fprintf(w, "Is Teammate:\t%t\n", ba.IsTeammate)
if ba.TeamMemberId != "" {
fmt.Fprintf(w, "Team Member Id:\t%s\n", ba.TeamMemberId)
}
fmt.Fprintf(w, "Profile Photo URL:\t%s\n", ba.ProfilePhotoUrl)
}

func account(cmd *cobra.Command, args []string) error {
w := new(tabwriter.Writer)
w.Init(os.Stdout, 4, 8, 1, ' ', 0)

fmt.Fprintf(w, "Logged in as %s <%s>\n\n", res.Name.DisplayName, res.Email)
fmt.Fprintf(w, "Account Id:\t%s\n", res.AccountId)
fmt.Fprintf(w, "Account Type:\t%s\n", res.AccountType.Tag)
fmt.Fprintf(w, "Locale:\t%s\n", res.Locale)
fmt.Fprintf(w, "Referral Link:\t%s\n", res.ReferralLink)
fmt.Fprintf(w, "Paired Account:\t%t\n", res.IsPaired)
if res.Team != nil {
fmt.Fprintf(w, "Team:\n Name:\t%s\n Id:\t%s\n Member Id:\t%s\n", res.Team.Name, res.Team.Id, res.TeamMemberId)
switch len(args) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you add the len validations for this as well, you could simplify this to

if len(args) == 0 {
...
} else {
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, that's cleaner. Added b75d1e3.

case 0:
// If no arguments are provided get the current user's account
res, err := dbx.GetCurrentAccount()
if err != nil {
return err
}
printFullAccount(w, res)
case 1:
// Otherwise look up an account with the provided ID
if len(args[0]) != 40 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, let me see if there's a better way to validate this. String length check seems hackish. We may need to validate the prefix dbxid as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that seems hackish and might break if the API changes. I didn't see a spec on the ID string in the docs, I just got the value 40 from the API's error messages.

Better idea: drop the validation and leave the errors up to the API. It worked fine that way before I added the length check. It'd be less affected by future changes to the ID string, anyways.

Here's what it does with the validation removed:

$ dbxcli account too_short_id
Error: Error in call to API function "users/get_account": request body: account_id: 'too_short_id' must be at least 40 characters, got 12

Or I could drop the second commit if it's not needed.

return errors.New("account ID must be 40 characters")
}
arg := users.NewGetAccountArg(args[0])
res, err := dbx.GetAccount(arg)
if err != nil {
return err
}
printBasicAccount(w, res)
}

w.Flush()
Expand All @@ -47,9 +83,10 @@ func account(cmd *cobra.Command, args []string) error {
}

var accountCmd = &cobra.Command{
Use: "account",
Short: "Display information about the current account",
RunE: account,
Use: "account [flags] [<account-id>]",
Short: "Display account information",
Example: " dbxcli account\n dbxcli account dbid:xxxx",
RunE: account,
}

func init() {
Expand Down