cmd/accounts.go
author Mikael Berthe <mikael@lilotux.net>
Tue, 09 May 2017 20:14:28 +0200
changeset 110 57843255fd1a
parent 102 84ad56b643c8
child 113 2e411da68fd3
permissions -rw-r--r--
Refactor printers
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     1
// Copyright © 2017 Mikael Berthe <mikael@lilotux.net>
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     2
//
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     3
// Licensed under the MIT license.
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     4
// Please see the LICENSE file is this directory.
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     6
package cmd
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
import (
16
8939959991b3 accounts: add --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
     9
	"os"
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
	"strings"
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
45
b58a7ea1aeb2 Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents: 44
diff changeset
    12
	"github.com/pkg/errors"
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
	"github.com/spf13/cobra"
6
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    14
	flag "github.com/spf13/pflag"
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
	"github.com/McKael/madon"
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    19
var accountsOpts struct {
44
6da40ca4534c Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents: 30
diff changeset
    20
	accountID                 int64
16
8939959991b3 accounts: add --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
    21
	accountUID                string
13
f862af8faf17 Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents: 12
diff changeset
    22
	unset                     bool   // TODO remove eventually?
44
6da40ca4534c Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents: 30
diff changeset
    23
	limit                     uint   // Limit the results
6da40ca4534c Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents: 30
diff changeset
    24
	sinceID, maxID            int64  // Query boundaries
30
14561d44211b Add option --all to fetch all available results
Mikael Berthe <mikael@lilotux.net>
parents: 29
diff changeset
    25
	all                       bool   // Try to fetch all results
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
	onlyMedia, excludeReplies bool   // For acccount statuses
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
	remoteUID                 string // For account follow
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
	acceptFR, rejectFR        bool   // For account follow_requests
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
	list                      bool   // For account follow_requests/reports
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
	accountIDs                string // For account relationships
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    31
	statusIDs                 string // For account reports
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    32
	comment                   string // For account reports
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    33
	show                      bool   // For account reports
6
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    34
	displayName, note         string // For account update
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    35
	avatar, header            string // For account update
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    36
}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
6
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    38
var updateFlags *flag.FlagSet
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    39
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
func init() {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
	RootCmd.AddCommand(accountsCmd)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    42
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    43
	// Subcommands
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    44
	accountsCmd.AddCommand(accountSubcommands...)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    45
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    46
	// Global flags
44
6da40ca4534c Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents: 30
diff changeset
    47
	accountsCmd.PersistentFlags().Int64VarP(&accountsOpts.accountID, "account-id", "a", 0, "Account ID number")
16
8939959991b3 accounts: add --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
    48
	accountsCmd.PersistentFlags().StringVarP(&accountsOpts.accountUID, "user-id", "u", "", "Account user ID")
13
f862af8faf17 Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents: 12
diff changeset
    49
	accountsCmd.PersistentFlags().UintVarP(&accountsOpts.limit, "limit", "l", 0, "Limit number of results")
44
6da40ca4534c Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents: 30
diff changeset
    50
	accountsCmd.PersistentFlags().Int64Var(&accountsOpts.sinceID, "since-id", 0, "Request IDs greater than a value")
6da40ca4534c Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents: 30
diff changeset
    51
	accountsCmd.PersistentFlags().Int64Var(&accountsOpts.maxID, "max-id", 0, "Request IDs less (or equal) than a value")
30
14561d44211b Add option --all to fetch all available results
Mikael Berthe <mikael@lilotux.net>
parents: 29
diff changeset
    52
	accountsCmd.PersistentFlags().BoolVar(&accountsOpts.all, "all", false, "Fetch all results")
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    53
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    54
	// Subcommand flags
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    55
	accountStatusesSubcommand.Flags().BoolVar(&accountsOpts.onlyMedia, "only-media", false, "Only statuses with media attachments")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    56
	accountStatusesSubcommand.Flags().BoolVar(&accountsOpts.excludeReplies, "exclude-replies", false, "Exclude replies to other statuses")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    57
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    58
	accountFollowRequestsSubcommand.Flags().BoolVar(&accountsOpts.list, "list", false, "List pending follow requests")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    59
	accountFollowRequestsSubcommand.Flags().BoolVar(&accountsOpts.acceptFR, "accept", false, "Accept the follow request from the account ID")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    60
	accountFollowRequestsSubcommand.Flags().BoolVar(&accountsOpts.rejectFR, "reject", false, "Reject the follow request from the account ID")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    61
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    62
	accountBlockSubcommand.Flags().BoolVarP(&accountsOpts.unset, "unset", "", false, "Unblock the account")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    63
	accountMuteSubcommand.Flags().BoolVarP(&accountsOpts.unset, "unset", "", false, "Unmute the account")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    64
	accountFollowSubcommand.Flags().BoolVarP(&accountsOpts.unset, "unset", "", false, "Unfollow the account")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    65
	accountFollowSubcommand.Flags().StringVarP(&accountsOpts.remoteUID, "remote", "r", "", "Follow remote account (user@domain)")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    66
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    67
	accountRelationshipsSubcommand.Flags().StringVar(&accountsOpts.accountIDs, "account-ids", "", "Comma-separated list of account IDs")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    68
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    69
	accountReportsSubcommand.Flags().StringVar(&accountsOpts.statusIDs, "status-ids", "", "Comma-separated list of status IDs")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    70
	accountReportsSubcommand.Flags().StringVar(&accountsOpts.comment, "comment", "", "Report comment")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    71
	accountReportsSubcommand.Flags().BoolVar(&accountsOpts.list, "list", false, "List current user reports")
6
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    72
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    73
	accountUpdateSubcommand.Flags().StringVar(&accountsOpts.displayName, "display-name", "", "User display name")
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    74
	accountUpdateSubcommand.Flags().StringVar(&accountsOpts.note, "note", "", "User note (a.k.a. bio)")
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    75
	accountUpdateSubcommand.Flags().StringVar(&accountsOpts.avatar, "avatar", "", "User avatar image")
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    76
	accountUpdateSubcommand.Flags().StringVar(&accountsOpts.header, "header", "", "User header image")
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    77
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    78
	// This one will be used to check if the options were explicitely set or not
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    79
	updateFlags = accountUpdateSubcommand.Flags()
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    80
}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    81
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    82
// accountsCmd represents the accounts command
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    83
// This command does nothing without a subcommand
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    84
var accountsCmd = &cobra.Command{
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    85
	Use:     "accounts [--account-id ID] subcommand",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    86
	Aliases: []string{"account"},
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    87
	Short:   "Account-related functions",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    88
	//Long:    `TBW...`, // TODO
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    89
	//PersistentPreRunE: func(cmd *cobra.Command, args []string) error {},
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    90
}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    91
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    92
// Note: Some account subcommands are not defined in this file.
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    93
var accountSubcommands = []*cobra.Command{
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    94
	&cobra.Command{
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    95
		Use: "show",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    96
		Long: `Displays the details about the requested account.
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    97
If no account ID is specified, the current user account is used.`,
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    98
		Aliases: []string{"display"},
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    99
		Short:   "Display the account",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   100
		RunE: func(cmd *cobra.Command, args []string) error {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   101
			return accountSubcommandsRunE(cmd.Name(), args)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   102
		},
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   103
	},
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   104
	&cobra.Command{
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   105
		Use:   "followers",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   106
		Short: "Display the accounts following the specified account",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   107
		RunE: func(cmd *cobra.Command, args []string) error {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   108
			return accountSubcommandsRunE(cmd.Name(), args)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   109
		},
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   110
	},
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   111
	&cobra.Command{
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   112
		Use:   "following",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   113
		Short: "Display the accounts followed by the specified account",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   114
		RunE: func(cmd *cobra.Command, args []string) error {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   115
			return accountSubcommandsRunE(cmd.Name(), args)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   116
		},
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   117
	},
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   118
	&cobra.Command{
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   119
		Use:     "favourites",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   120
		Aliases: []string{"favorites", "favourited", "favorited"},
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   121
		Short:   "Display the user's favourites",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   122
		RunE: func(cmd *cobra.Command, args []string) error {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   123
			return accountSubcommandsRunE(cmd.Name(), args)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   124
		},
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   125
	},
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   126
	&cobra.Command{
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   127
		Use:     "blocks",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   128
		Aliases: []string{"blocked"},
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   129
		Short:   "Display the user's blocked accounts",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   130
		RunE: func(cmd *cobra.Command, args []string) error {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   131
			return accountSubcommandsRunE(cmd.Name(), args)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   132
		},
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   133
	},
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   134
	&cobra.Command{
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   135
		Use:     "mutes",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   136
		Aliases: []string{"muted"},
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   137
		Short:   "Display the user's muted accounts",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   138
		RunE: func(cmd *cobra.Command, args []string) error {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   139
			return accountSubcommandsRunE(cmd.Name(), args)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   140
		},
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   141
	},
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   142
	&cobra.Command{
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   143
		Use:   "search TEXT",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   144
		Short: "Search for user accounts",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   145
		Long: `Search for user accounts.
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   146
The server will lookup an account remotely if the search term is in the
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   147
username@domain format and not yet in the database.`,
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   148
		RunE: func(cmd *cobra.Command, args []string) error {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   149
			return accountSubcommandsRunE(cmd.Name(), args)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   150
		},
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   151
	},
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   152
	accountStatusesSubcommand,
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   153
	accountFollowRequestsSubcommand,
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   154
	accountFollowSubcommand,
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   155
	accountBlockSubcommand,
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   156
	accountMuteSubcommand,
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   157
	accountRelationshipsSubcommand,
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   158
	accountReportsSubcommand,
6
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   159
	accountUpdateSubcommand,
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   160
}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   161
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   162
var accountStatusesSubcommand = &cobra.Command{
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   163
	Use:     "statuses",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   164
	Aliases: []string{"st"},
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   165
	Short:   "Display the account statuses",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   166
	RunE: func(cmd *cobra.Command, args []string) error {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   167
		return accountSubcommandsRunE(cmd.Name(), args)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   168
	},
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   169
}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   170
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   171
var accountFollowRequestsSubcommand = &cobra.Command{
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   172
	Use:     "follow-requests",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   173
	Aliases: []string{"follow-request", "fr"},
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   174
	Example: `  madonctl accounts follow-requests --list
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   175
  madonctl accounts follow-requests --account-id X --accept
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   176
  madonctl accounts follow-requests --account-id Y --reject`,
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   177
	Short: "List, accept or deny a follow request",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   178
	RunE: func(cmd *cobra.Command, args []string) error {
12
e94c9ed9b1c8 Use cmd.Name() in cobra commands
Mikael Berthe <mikael@lilotux.net>
parents: 6
diff changeset
   179
		return accountSubcommandsRunE(cmd.Name(), args)
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   180
	},
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   181
}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   182
var accountFollowSubcommand = &cobra.Command{
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   183
	Use:   "follow",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   184
	Short: "Follow or unfollow the account",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   185
	RunE: func(cmd *cobra.Command, args []string) error {
12
e94c9ed9b1c8 Use cmd.Name() in cobra commands
Mikael Berthe <mikael@lilotux.net>
parents: 6
diff changeset
   186
		return accountSubcommandsRunE(cmd.Name(), args)
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   187
	},
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   188
}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   189
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   190
var accountBlockSubcommand = &cobra.Command{
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   191
	Use:   "block",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   192
	Short: "Block or unblock the account",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   193
	RunE: func(cmd *cobra.Command, args []string) error {
12
e94c9ed9b1c8 Use cmd.Name() in cobra commands
Mikael Berthe <mikael@lilotux.net>
parents: 6
diff changeset
   194
		return accountSubcommandsRunE(cmd.Name(), args)
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   195
	},
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   196
}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   197
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   198
var accountMuteSubcommand = &cobra.Command{
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   199
	Use:   "mute",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   200
	Short: "Mute or unmute the account",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   201
	RunE: func(cmd *cobra.Command, args []string) error {
12
e94c9ed9b1c8 Use cmd.Name() in cobra commands
Mikael Berthe <mikael@lilotux.net>
parents: 6
diff changeset
   202
		return accountSubcommandsRunE(cmd.Name(), args)
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   203
	},
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   204
}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   205
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   206
var accountRelationshipsSubcommand = &cobra.Command{
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   207
	Use:   "relationships --account-ids ACC1,ACC2...",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   208
	Short: "List relationships with the accounts",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   209
	RunE: func(cmd *cobra.Command, args []string) error {
12
e94c9ed9b1c8 Use cmd.Name() in cobra commands
Mikael Berthe <mikael@lilotux.net>
parents: 6
diff changeset
   210
		return accountSubcommandsRunE(cmd.Name(), args)
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   211
	},
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   212
}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   213
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   214
var accountReportsSubcommand = &cobra.Command{
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   215
	Use:   "reports",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   216
	Short: "List reports or report a user account",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   217
	Example: `  madonctl accounts reports --list
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   218
  madonctl accounts reports --account-id ACCOUNT --status-ids ID... --comment TEXT`,
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   219
	RunE: func(cmd *cobra.Command, args []string) error {
12
e94c9ed9b1c8 Use cmd.Name() in cobra commands
Mikael Berthe <mikael@lilotux.net>
parents: 6
diff changeset
   220
		return accountSubcommandsRunE(cmd.Name(), args)
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   221
	},
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   222
}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   223
6
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   224
var accountUpdateSubcommand = &cobra.Command{
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   225
	Use:   "update",
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   226
	Short: "Update connected user account",
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   227
	Long: `Update connected user account
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   228
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   229
All flags are optional (set to an empty string if you want to delete a field).
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   230
The flags --avatar and --header can be paths to image files or base64-encoded
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   231
images (see Mastodon API specifications for the details).
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   232
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   233
Please note the avatar and header images cannot be removed, they can only be
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   234
replaced.`,
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   235
	Example: `  madonctl accounts update --display-name "Mr President"
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   236
  madonctl accounts update --note "I like madonctl"
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   237
  madonctl accounts update --avatar happyface.png`,
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   238
	RunE: func(cmd *cobra.Command, args []string) error {
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   239
		return accountSubcommandsRunE(cmd.Name(), args)
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   240
	},
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   241
}
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   242
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   243
// accountSubcommandsRunE is a generic function for status subcommands
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   244
func accountSubcommandsRunE(subcmd string, args []string) error {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   245
	opt := accountsOpts
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   246
16
8939959991b3 accounts: add --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   247
	if opt.accountUID != "" {
8939959991b3 accounts: add --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   248
		if opt.accountID > 0 {
8939959991b3 accounts: add --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   249
			return errors.New("cannot use both account ID and UID")
8939959991b3 accounts: add --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   250
		}
8939959991b3 accounts: add --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   251
		// Sign in early to look the user id up
102
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   252
		var err error
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   253
		if err = madonInit(true); err != nil {
16
8939959991b3 accounts: add --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   254
			return err
8939959991b3 accounts: add --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   255
		}
102
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   256
		opt.accountID, err = accountLookupUser(opt.accountUID)
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   257
		if err != nil || opt.accountID < 1 {
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   258
			if err != nil {
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   259
				errPrint("Cannot find user '%s': %v", opt.accountUID, err)
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   260
			} else {
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   261
				errPrint("Cannot find user '%s'", opt.accountUID)
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   262
			}
16
8939959991b3 accounts: add --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   263
			os.Exit(1)
8939959991b3 accounts: add --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   264
		}
8939959991b3 accounts: add --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   265
	}
8939959991b3 accounts: add --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   266
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   267
	switch subcmd {
6
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   268
	case "show", "search", "update":
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   269
		// These subcommands do not require an account ID
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   270
	case "favourites", "blocks", "mutes":
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   271
		// Those subcommands can not use an account ID
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   272
		if opt.accountID > 0 {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   273
			return errors.New("useless account ID")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   274
		}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   275
	case "follow":
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   276
		if opt.accountID < 1 && opt.remoteUID == "" {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   277
			return errors.New("missing account ID or URI")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   278
		}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   279
		if opt.accountID > 0 && opt.remoteUID != "" {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   280
			return errors.New("cannot use both account ID and URI")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   281
		}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   282
		if opt.unset && opt.accountID < 1 {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   283
			return errors.New("unfollowing requires an account ID")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   284
		}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   285
	case "follow-requests":
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   286
		if opt.list {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   287
			if opt.acceptFR || opt.rejectFR {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   288
				return errors.New("incompatible options")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   289
			}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   290
		} else {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   291
			if !opt.acceptFR && !opt.rejectFR { // No flag
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   292
				return errors.New("missing parameter (--list, --accept or --reject)")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   293
			}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   294
			// This is a FR reply
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   295
			if opt.acceptFR && opt.rejectFR {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   296
				return errors.New("incompatible options")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   297
			}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   298
			if opt.accountID < 1 {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   299
				return errors.New("missing account ID")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   300
			}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   301
		}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   302
	case "relationships":
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   303
		if opt.accountID < 1 && len(opt.accountIDs) == 0 {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   304
			return errors.New("missing account IDs")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   305
		}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   306
		if opt.accountID > 0 && len(opt.accountIDs) > 0 {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   307
			return errors.New("incompatible options")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   308
		}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   309
	case "reports":
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   310
		if opt.list {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   311
			break // No argument needed
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   312
		}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   313
		if opt.accountID < 1 || len(opt.statusIDs) == 0 || opt.comment == "" {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   314
			return errors.New("missing parameter")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   315
		}
28
79aa812c0dd2 Make account ID optional for accounts followers|following|statuses
Mikael Berthe <mikael@lilotux.net>
parents: 22
diff changeset
   316
	case "followers", "following", "statuses":
79aa812c0dd2 Make account ID optional for accounts followers|following|statuses
Mikael Berthe <mikael@lilotux.net>
parents: 22
diff changeset
   317
		// If the user's account ID is missing, get it
79aa812c0dd2 Make account ID optional for accounts followers|following|statuses
Mikael Berthe <mikael@lilotux.net>
parents: 22
diff changeset
   318
		if opt.accountID < 1 {
79aa812c0dd2 Make account ID optional for accounts followers|following|statuses
Mikael Berthe <mikael@lilotux.net>
parents: 22
diff changeset
   319
			// Sign in now to look the user id up
79aa812c0dd2 Make account ID optional for accounts followers|following|statuses
Mikael Berthe <mikael@lilotux.net>
parents: 22
diff changeset
   320
			if err := madonInit(true); err != nil {
79aa812c0dd2 Make account ID optional for accounts followers|following|statuses
Mikael Berthe <mikael@lilotux.net>
parents: 22
diff changeset
   321
				return err
79aa812c0dd2 Make account ID optional for accounts followers|following|statuses
Mikael Berthe <mikael@lilotux.net>
parents: 22
diff changeset
   322
			}
79aa812c0dd2 Make account ID optional for accounts followers|following|statuses
Mikael Berthe <mikael@lilotux.net>
parents: 22
diff changeset
   323
			account, err := gClient.GetCurrentAccount()
79aa812c0dd2 Make account ID optional for accounts followers|following|statuses
Mikael Berthe <mikael@lilotux.net>
parents: 22
diff changeset
   324
			if err != nil {
79aa812c0dd2 Make account ID optional for accounts followers|following|statuses
Mikael Berthe <mikael@lilotux.net>
parents: 22
diff changeset
   325
				return err
79aa812c0dd2 Make account ID optional for accounts followers|following|statuses
Mikael Berthe <mikael@lilotux.net>
parents: 22
diff changeset
   326
			}
79aa812c0dd2 Make account ID optional for accounts followers|following|statuses
Mikael Berthe <mikael@lilotux.net>
parents: 22
diff changeset
   327
			opt.accountID = account.ID
29
4f12e5d4ef75 Display account ID in verbose mode when it is looked up
Mikael Berthe <mikael@lilotux.net>
parents: 28
diff changeset
   328
			if verbose {
4f12e5d4ef75 Display account ID in verbose mode when it is looked up
Mikael Berthe <mikael@lilotux.net>
parents: 28
diff changeset
   329
				errPrint("User account ID: %d", opt.accountID)
4f12e5d4ef75 Display account ID in verbose mode when it is looked up
Mikael Berthe <mikael@lilotux.net>
parents: 28
diff changeset
   330
			}
28
79aa812c0dd2 Make account ID optional for accounts followers|following|statuses
Mikael Berthe <mikael@lilotux.net>
parents: 22
diff changeset
   331
		}
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   332
	default:
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   333
		// The other subcommands here require an account ID
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   334
		if opt.accountID < 1 {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   335
			return errors.New("missing account ID")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   336
		}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   337
	}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   338
22
5778b09bc6fe Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
   339
	var limOpts *madon.LimitParams
30
14561d44211b Add option --all to fetch all available results
Mikael Berthe <mikael@lilotux.net>
parents: 29
diff changeset
   340
	if opt.all || opt.limit > 0 || opt.sinceID > 0 || opt.maxID > 0 {
22
5778b09bc6fe Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
   341
		limOpts = new(madon.LimitParams)
30
14561d44211b Add option --all to fetch all available results
Mikael Berthe <mikael@lilotux.net>
parents: 29
diff changeset
   342
		limOpts.All = opt.all
22
5778b09bc6fe Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
   343
	}
5778b09bc6fe Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
   344
20
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
   345
	if opt.limit > 0 {
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
   346
		limOpts.Limit = int(opt.limit)
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
   347
	}
22
5778b09bc6fe Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
   348
	if opt.maxID > 0 {
44
6da40ca4534c Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents: 30
diff changeset
   349
		limOpts.MaxID = opt.maxID
22
5778b09bc6fe Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
   350
	}
5778b09bc6fe Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
   351
	if opt.sinceID > 0 {
44
6da40ca4534c Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents: 30
diff changeset
   352
		limOpts.SinceID = opt.sinceID
22
5778b09bc6fe Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
   353
	}
20
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
   354
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   355
	// All account subcommands need to have signed in
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   356
	if err := madonInit(true); err != nil {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   357
		return err
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   358
	}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   359
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   360
	var obj interface{}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   361
	var err error
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   362
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   363
	switch subcmd {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   364
	case "show":
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   365
		var account *madon.Account
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   366
		if opt.accountID > 0 {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   367
			account, err = gClient.GetAccount(opt.accountID)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   368
		} else {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   369
			account, err = gClient.GetCurrentAccount()
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   370
		}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   371
		obj = account
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   372
	case "search":
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   373
		var accountList []madon.Account
20
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
   374
		accountList, err = gClient.SearchAccounts(strings.Join(args, " "), limOpts)
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   375
		obj = accountList
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   376
	case "followers":
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   377
		var accountList []madon.Account
20
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
   378
		accountList, err = gClient.GetAccountFollowers(opt.accountID, limOpts)
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
   379
		if opt.limit > 0 && len(accountList) > int(opt.limit) {
13
f862af8faf17 Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents: 12
diff changeset
   380
			accountList = accountList[:opt.limit]
f862af8faf17 Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents: 12
diff changeset
   381
		}
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   382
		obj = accountList
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   383
	case "following":
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   384
		var accountList []madon.Account
20
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
   385
		accountList, err = gClient.GetAccountFollowing(opt.accountID, limOpts)
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
   386
		if opt.limit > 0 && len(accountList) > int(opt.limit) {
13
f862af8faf17 Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents: 12
diff changeset
   387
			accountList = accountList[:opt.limit]
f862af8faf17 Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents: 12
diff changeset
   388
		}
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   389
		obj = accountList
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   390
	case "statuses":
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   391
		var statusList []madon.Status
20
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
   392
		statusList, err = gClient.GetAccountStatuses(opt.accountID, opt.onlyMedia, opt.excludeReplies, limOpts)
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
   393
		if opt.limit > 0 && len(statusList) > int(opt.limit) {
13
f862af8faf17 Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents: 12
diff changeset
   394
			statusList = statusList[:opt.limit]
f862af8faf17 Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents: 12
diff changeset
   395
		}
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   396
		obj = statusList
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   397
	case "follow":
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   398
		if opt.unset {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   399
			err = gClient.UnfollowAccount(opt.accountID)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   400
		} else {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   401
			if opt.accountID > 0 {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   402
				err = gClient.FollowAccount(opt.accountID)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   403
			} else {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   404
				var account *madon.Account
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   405
				account, err = gClient.FollowRemoteAccount(opt.remoteUID)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   406
				obj = account
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   407
			}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   408
		}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   409
	case "follow-requests":
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   410
		if opt.list {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   411
			var followRequests []madon.Account
20
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
   412
			followRequests, err = gClient.GetAccountFollowRequests(limOpts)
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
   413
			if opt.limit > 0 && len(followRequests) > int(opt.limit) {
13
f862af8faf17 Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents: 12
diff changeset
   414
				followRequests = followRequests[:opt.limit]
f862af8faf17 Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents: 12
diff changeset
   415
			}
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   416
			obj = followRequests
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   417
		} else {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   418
			err = gClient.FollowRequestAuthorize(opt.accountID, !opt.rejectFR)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   419
		}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   420
	case "block":
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   421
		if opt.unset {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   422
			err = gClient.UnblockAccount(opt.accountID)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   423
		} else {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   424
			err = gClient.BlockAccount(opt.accountID)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   425
		}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   426
	case "mute":
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   427
		if opt.unset {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   428
			err = gClient.UnmuteAccount(opt.accountID)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   429
		} else {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   430
			err = gClient.MuteAccount(opt.accountID)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   431
		}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   432
	case "favourites":
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   433
		var statusList []madon.Status
20
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
   434
		statusList, err = gClient.GetFavourites(limOpts)
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
   435
		if opt.limit > 0 && len(statusList) > int(opt.limit) {
13
f862af8faf17 Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents: 12
diff changeset
   436
			statusList = statusList[:opt.limit]
f862af8faf17 Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents: 12
diff changeset
   437
		}
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   438
		obj = statusList
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   439
	case "blocks":
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   440
		var accountList []madon.Account
20
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
   441
		accountList, err = gClient.GetBlockedAccounts(limOpts)
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
   442
		if opt.limit > 0 && len(accountList) > int(opt.limit) {
13
f862af8faf17 Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents: 12
diff changeset
   443
			accountList = accountList[:opt.limit]
f862af8faf17 Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents: 12
diff changeset
   444
		}
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   445
		obj = accountList
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   446
	case "mutes":
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   447
		var accountList []madon.Account
20
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
   448
		accountList, err = gClient.GetMutedAccounts(limOpts)
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
   449
		if opt.limit > 0 && len(accountList) > int(opt.limit) {
13
f862af8faf17 Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents: 12
diff changeset
   450
			accountList = accountList[:opt.limit]
f862af8faf17 Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents: 12
diff changeset
   451
		}
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   452
		obj = accountList
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   453
	case "relationships":
44
6da40ca4534c Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents: 30
diff changeset
   454
		var ids []int64
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   455
		ids, err = splitIDs(opt.accountIDs)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   456
		if err != nil {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   457
			return errors.New("cannot parse account IDs")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   458
		}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   459
		if opt.accountID > 0 { // Allow --account-id
44
6da40ca4534c Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents: 30
diff changeset
   460
			ids = []int64{opt.accountID}
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   461
		}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   462
		if len(ids) < 1 {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   463
			return errors.New("missing account IDs")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   464
		}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   465
		var relationships []madon.Relationship
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   466
		relationships, err = gClient.GetAccountRelationships(ids)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   467
		obj = relationships
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   468
	case "reports":
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   469
		if opt.list {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   470
			var reports []madon.Report
20
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
   471
			reports, err = gClient.GetReports(limOpts)
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
   472
			if opt.limit > 0 && len(reports) > int(opt.limit) {
13
f862af8faf17 Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents: 12
diff changeset
   473
				reports = reports[:opt.limit]
f862af8faf17 Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents: 12
diff changeset
   474
			}
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   475
			obj = reports
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   476
			break
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   477
		}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   478
		// Send a report
44
6da40ca4534c Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents: 30
diff changeset
   479
		var ids []int64
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   480
		ids, err = splitIDs(opt.statusIDs)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   481
		if err != nil {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   482
			return errors.New("cannot parse status IDs")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   483
		}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   484
		if len(ids) < 1 {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   485
			return errors.New("missing status IDs")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   486
		}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   487
		var report *madon.Report
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   488
		report, err = gClient.ReportUser(opt.accountID, ids, opt.comment)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   489
		obj = report
6
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   490
	case "update":
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   491
		var dn, note, avatar, header *string
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   492
		change := false
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   493
		if updateFlags.Lookup("display-name").Changed {
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   494
			dn = &opt.displayName
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   495
			change = true
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   496
		}
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   497
		if updateFlags.Lookup("note").Changed {
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   498
			note = &opt.note
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   499
			change = true
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   500
		}
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   501
		if updateFlags.Lookup("avatar").Changed {
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   502
			avatar = &opt.avatar
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   503
			change = true
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   504
		}
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   505
		if updateFlags.Lookup("header").Changed {
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   506
			header = &opt.header
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   507
			change = true
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   508
		}
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   509
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   510
		if !change { // We want at least one update
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   511
			return errors.New("missing parameters")
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   512
		}
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   513
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   514
		var account *madon.Account
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   515
		account, err = gClient.UpdateAccount(dn, note, avatar, header)
5228e517c455 Add account update
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   516
		obj = account
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   517
	default:
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   518
		return errors.New("accountSubcommand: internal error")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   519
	}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   520
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   521
	if err != nil {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   522
		errPrint("Error: %s", err.Error())
47
82d8b6074309 Set exit code to non-zero when API calls fail
Mikael Berthe <mikael@lilotux.net>
parents: 45
diff changeset
   523
		os.Exit(1)
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   524
	}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   525
	if obj == nil {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   526
		return nil
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   527
	}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   528
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   529
	p, err := getPrinter()
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   530
	if err != nil {
81
b1671f83e91b Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents: 47
diff changeset
   531
		errPrint("Error: %s", err.Error())
b1671f83e91b Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents: 47
diff changeset
   532
		os.Exit(1)
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   533
	}
110
57843255fd1a Refactor printers
Mikael Berthe <mikael@lilotux.net>
parents: 102
diff changeset
   534
	return p.printObj(obj)
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   535
}
102
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   536
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   537
// accountLookupUser tries to find a (single) user matching 'user'
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   538
// If the user is an HTTP URL, it will use the search API, else
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   539
// it will use the accounts/search API.
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   540
func accountLookupUser(user string) (int64, error) {
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   541
	var accID int64
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   542
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   543
	if strings.HasPrefix(user, "https://") || strings.HasPrefix(user, "http://") {
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   544
		res, err := gClient.Search(user, true)
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   545
		if err != nil {
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   546
			return 0, err
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   547
		}
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   548
		if res != nil {
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   549
			if len(res.Accounts) > 1 {
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   550
				return 0, errors.New("several results")
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   551
			}
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   552
			if len(res.Accounts) == 1 {
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   553
				accID = res.Accounts[0].ID
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   554
			}
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   555
		}
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   556
	} else {
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   557
		// Remove leading '@'
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   558
		user = strings.TrimLeft(user, "@")
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   559
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   560
		accList, err := gClient.SearchAccounts(user, &madon.LimitParams{Limit: 2})
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   561
		if err != nil {
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   562
			return 0, err
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   563
		}
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   564
		for _, u := range accList {
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   565
			if u.Acct == user {
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   566
				accID = u.ID
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   567
				break
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   568
			}
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   569
		}
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   570
	}
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   571
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   572
	if accID < 1 {
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   573
		return 0, errors.New("user not found")
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   574
	}
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   575
	if verbose {
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   576
		errPrint("User '%s' is account ID %d", user, user)
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   577
	}
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   578
	return accID, nil
84ad56b643c8 Allow HTTP URLs in --user-id
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   579
}