cmd/accounts.go
changeset 208 7a830fed2ba3
parent 205 60f4c2acedfd
child 209 3772cc6b3d0a
equal deleted inserted replaced
207:56afbe03a7d4 208:7a830fed2ba3
     5 
     5 
     6 package cmd
     6 package cmd
     7 
     7 
     8 import (
     8 import (
     9 	"os"
     9 	"os"
       
    10 	"strconv"
    10 	"strings"
    11 	"strings"
    11 
    12 
    12 	"github.com/pkg/errors"
    13 	"github.com/pkg/errors"
    13 	"github.com/spf13/cobra"
    14 	"github.com/spf13/cobra"
    14 	flag "github.com/spf13/pflag"
    15 	flag "github.com/spf13/pflag"
   109 		Use: "show",
   110 		Use: "show",
   110 		Long: `Displays the details about the requested account.
   111 		Long: `Displays the details about the requested account.
   111 If no account ID is specified, the current user account is used.`,
   112 If no account ID is specified, the current user account is used.`,
   112 		Aliases: []string{"display"},
   113 		Aliases: []string{"display"},
   113 		Short:   "Display the account",
   114 		Short:   "Display the account",
       
   115 		Example: `  madonctl account show   # Display your own account
       
   116 
       
   117   madonctl accounts show --account-id 1234
       
   118   madonctl accounts show --user-id Gargron@mastodon.social
       
   119   madonctl accounts show --user-id https://mastodon.social/@Gargron
       
   120 
       
   121   madonctl accounts show 1234
       
   122   madonctl accounts show Gargron@mastodon.social
       
   123   madonctl accounts show https://mastodon.social/@Gargron
       
   124 `,
   114 		RunE: func(cmd *cobra.Command, args []string) error {
   125 		RunE: func(cmd *cobra.Command, args []string) error {
   115 			return accountSubcommandsRunE(cmd.Name(), args)
   126 			return accountSubcommandsRunE(cmd.Name(), args)
   116 		},
   127 		},
   117 	},
   128 	},
   118 	&cobra.Command{
   129 	&cobra.Command{
   178 
   189 
   179 var accountStatusesSubcommand = &cobra.Command{
   190 var accountStatusesSubcommand = &cobra.Command{
   180 	Use:     "statuses",
   191 	Use:     "statuses",
   181 	Aliases: []string{"st"},
   192 	Aliases: []string{"st"},
   182 	Short:   "Display the account statuses",
   193 	Short:   "Display the account statuses",
       
   194 	Example: `  madonctl accounts statuses
       
   195   madonctl accounts statuses 404                         # local account numeric ID
       
   196   madonctl accounts statuses @McKael                     # local account
       
   197   madonctl accounts statuses Gargron@mastodon.social     # remote (known account)
       
   198   madonctl accounts statuses https://mastodon.social/@Gargron  # any account URL
       
   199 `,
   183 	RunE: func(cmd *cobra.Command, args []string) error {
   200 	RunE: func(cmd *cobra.Command, args []string) error {
   184 		return accountSubcommandsRunE(cmd.Name(), args)
   201 		return accountSubcommandsRunE(cmd.Name(), args)
   185 	},
   202 	},
   186 }
   203 }
   187 
   204 
   197 	},
   214 	},
   198 }
   215 }
   199 var accountFollowSubcommand = &cobra.Command{
   216 var accountFollowSubcommand = &cobra.Command{
   200 	Use:   "follow",
   217 	Use:   "follow",
   201 	Short: "Follow or unfollow the account",
   218 	Short: "Follow or unfollow the account",
       
   219 	Example: `# Argument type can be set explicitly:
       
   220   madonctl accounts follow --account-id 1234
       
   221   madonctl accounts follow --remote Gargron@mastodon.social
       
   222 
       
   223 # Or argument type can be guessed:
       
   224   madonctl accounts follow 4800
       
   225   madonctl accounts follow Gargron@mastodon.social --show-reblogs=false
       
   226   madonctl accounts follow https://mastodon.social/@Gargron
       
   227 `,
   202 	RunE: func(cmd *cobra.Command, args []string) error {
   228 	RunE: func(cmd *cobra.Command, args []string) error {
   203 		return accountSubcommandsRunE(cmd.Name(), args)
   229 		return accountSubcommandsRunE(cmd.Name(), args)
   204 	},
   230 	},
   205 }
   231 }
   206 
   232 
   258 }
   284 }
   259 
   285 
   260 // accountSubcommandsRunE is a generic function for status subcommands
   286 // accountSubcommandsRunE is a generic function for status subcommands
   261 func accountSubcommandsRunE(subcmd string, args []string) error {
   287 func accountSubcommandsRunE(subcmd string, args []string) error {
   262 	opt := accountsOpts
   288 	opt := accountsOpts
       
   289 
       
   290 	if len(args) > 1 {
       
   291 		return errors.New("too many arguments")
       
   292 	}
       
   293 
       
   294 	userInArg := false
       
   295 
       
   296 	if len(args) == 1 {
       
   297 		if len(args[0]) > 0 {
       
   298 			userInArg = true
       
   299 		} else {
       
   300 			return errors.New("invalid argument (empty)")
       
   301 		}
       
   302 	}
       
   303 
       
   304 	// Check account is provided in only one way
       
   305 	aCounter := 0
       
   306 	if opt.accountID > 0 {
       
   307 		aCounter++
       
   308 	}
       
   309 	if opt.accountUID != "" {
       
   310 		aCounter++
       
   311 	}
       
   312 	if opt.remoteUID != "" {
       
   313 		aCounter++
       
   314 	}
       
   315 	if userInArg {
       
   316 		aCounter++
       
   317 	}
       
   318 
       
   319 	if aCounter > 1 {
       
   320 		return errors.New("too many account identifiers provided")
       
   321 	}
       
   322 
       
   323 	if userInArg {
       
   324 		// Is the argument an account ID?
       
   325 		if n, err := strconv.ParseInt(args[0], 10, 64); err == nil {
       
   326 			opt.accountID = n
       
   327 		} else if strings.HasPrefix(args[0], "https://") || strings.HasPrefix(args[0], "http://") {
       
   328 			// That is not a remote UID scheme
       
   329 			opt.accountUID = args[0]
       
   330 		} else if subcmd == "follow" {
       
   331 			// For the follow API, got to be a remote UID...
       
   332 			opt.remoteUID = args[0]
       
   333 			// ... unless it's local (i.e. no '@' in the identifier)...
       
   334 			fid := strings.TrimLeft(args[0], "@")
       
   335 			if !strings.ContainsRune(fid, '@') {
       
   336 				opt.accountUID = args[0]
       
   337 				opt.remoteUID = ""
       
   338 			}
       
   339 		} else {
       
   340 			// Fall back to account UID
       
   341 			opt.accountUID = args[0]
       
   342 		}
       
   343 	}
   263 
   344 
   264 	if opt.accountUID != "" {
   345 	if opt.accountUID != "" {
   265 		if opt.accountID > 0 {
   346 		if opt.accountID > 0 {
   266 			return errors.New("cannot use both account ID and UID")
   347 			return errors.New("cannot use both account ID and UID")
   267 		}
   348 		}
   288 		// Those subcommands can not use an account ID
   369 		// Those subcommands can not use an account ID
   289 		if opt.accountID > 0 {
   370 		if opt.accountID > 0 {
   290 			return errors.New("useless account ID")
   371 			return errors.New("useless account ID")
   291 		}
   372 		}
   292 	case "follow":
   373 	case "follow":
       
   374 		// We need an account ID or a remote UID
   293 		if opt.accountID < 1 && opt.remoteUID == "" {
   375 		if opt.accountID < 1 && opt.remoteUID == "" {
   294 			return errors.New("missing account ID or URI")
   376 			return errors.New("missing account ID or URI")
   295 		}
   377 		}
   296 		if opt.accountID > 0 && opt.remoteUID != "" {
   378 		if opt.accountID > 0 && opt.remoteUID != "" {
   297 			return errors.New("cannot use both account ID and URI")
   379 			return errors.New("cannot use both account ID and URI")
   414 	case "follow":
   496 	case "follow":
   415 		var relationship *madon.Relationship
   497 		var relationship *madon.Relationship
   416 		if opt.unset {
   498 		if opt.unset {
   417 			relationship, err = gClient.UnfollowAccount(opt.accountID)
   499 			relationship, err = gClient.UnfollowAccount(opt.accountID)
   418 			obj = relationship
   500 			obj = relationship
   419 		} else {
   501 			break
   420 			if opt.accountID <= 0 {
   502 		}
       
   503 		if opt.accountID <= 0 {
       
   504 			if opt.remoteUID != "" {
   421 				// Remote account
   505 				// Remote account
   422 				var account *madon.Account
   506 				var account *madon.Account
   423 				account, err = gClient.FollowRemoteAccount(opt.remoteUID)
   507 				account, err = gClient.FollowRemoteAccount(opt.remoteUID)
   424 				obj = account
   508 				obj = account
   425 				break
   509 				break
   426 			}
   510 			}
   427 
   511 			return errors.New("error: no usable parameter")
   428 			// Locally-known account
   512 		}
   429 			var followReblogs *bool
   513 
   430 			if accountFollowFlags.Lookup("show-reblogs").Changed {
   514 		// Locally-known account
   431 				// Set followReblogs as it's been explicitly requested
   515 		var followReblogs *bool
   432 				followReblogs = &opt.reblogs
   516 		if accountFollowFlags.Lookup("show-reblogs").Changed {
   433 			}
   517 			// Set followReblogs as it's been explicitly requested
   434 			relationship, err = gClient.FollowAccount(opt.accountID, followReblogs)
   518 			followReblogs = &opt.reblogs
   435 			obj = relationship
   519 		}
   436 		}
   520 		relationship, err = gClient.FollowAccount(opt.accountID, followReblogs)
       
   521 		obj = relationship
   437 	case "follow-requests":
   522 	case "follow-requests":
   438 		if opt.list {
   523 		if opt.list {
   439 			var followRequests []madon.Account
   524 			var followRequests []madon.Account
   440 			followRequests, err = gClient.GetAccountFollowRequests(limOpts)
   525 			followRequests, err = gClient.GetAccountFollowRequests(limOpts)
   441 			if opt.accountID > 0 { // Display a specific request
   526 			if opt.accountID > 0 { // Display a specific request