cmd/accounts.go
changeset 6 5228e517c455
parent 0 5abace724584
child 12 e94c9ed9b1c8
equal deleted inserted replaced
5:d70f8e8f3d2e 6:5228e517c455
     8 import (
     8 import (
     9 	"errors"
     9 	"errors"
    10 	"strings"
    10 	"strings"
    11 
    11 
    12 	"github.com/spf13/cobra"
    12 	"github.com/spf13/cobra"
       
    13 	flag "github.com/spf13/pflag"
    13 
    14 
    14 	"github.com/McKael/madon"
    15 	"github.com/McKael/madon"
    15 )
    16 )
    16 
    17 
    17 var accountsOpts struct {
    18 var accountsOpts struct {
    24 	list                      bool   // For account follow_requests/reports
    25 	list                      bool   // For account follow_requests/reports
    25 	accountIDs                string // For account relationships
    26 	accountIDs                string // For account relationships
    26 	statusIDs                 string // For account reports
    27 	statusIDs                 string // For account reports
    27 	comment                   string // For account reports
    28 	comment                   string // For account reports
    28 	show                      bool   // For account reports
    29 	show                      bool   // For account reports
    29 }
    30 	displayName, note         string // For account update
       
    31 	avatar, header            string // For account update
       
    32 }
       
    33 
       
    34 var updateFlags *flag.FlagSet
    30 
    35 
    31 func init() {
    36 func init() {
    32 	RootCmd.AddCommand(accountsCmd)
    37 	RootCmd.AddCommand(accountsCmd)
    33 
    38 
    34 	// Subcommands
    39 	// Subcommands
    54 	accountRelationshipsSubcommand.Flags().StringVar(&accountsOpts.accountIDs, "account-ids", "", "Comma-separated list of account IDs")
    59 	accountRelationshipsSubcommand.Flags().StringVar(&accountsOpts.accountIDs, "account-ids", "", "Comma-separated list of account IDs")
    55 
    60 
    56 	accountReportsSubcommand.Flags().StringVar(&accountsOpts.statusIDs, "status-ids", "", "Comma-separated list of status IDs")
    61 	accountReportsSubcommand.Flags().StringVar(&accountsOpts.statusIDs, "status-ids", "", "Comma-separated list of status IDs")
    57 	accountReportsSubcommand.Flags().StringVar(&accountsOpts.comment, "comment", "", "Report comment")
    62 	accountReportsSubcommand.Flags().StringVar(&accountsOpts.comment, "comment", "", "Report comment")
    58 	accountReportsSubcommand.Flags().BoolVar(&accountsOpts.list, "list", false, "List current user reports")
    63 	accountReportsSubcommand.Flags().BoolVar(&accountsOpts.list, "list", false, "List current user reports")
       
    64 
       
    65 	accountUpdateSubcommand.Flags().StringVar(&accountsOpts.displayName, "display-name", "", "User display name")
       
    66 	accountUpdateSubcommand.Flags().StringVar(&accountsOpts.note, "note", "", "User note (a.k.a. bio)")
       
    67 	accountUpdateSubcommand.Flags().StringVar(&accountsOpts.avatar, "avatar", "", "User avatar image")
       
    68 	accountUpdateSubcommand.Flags().StringVar(&accountsOpts.header, "header", "", "User header image")
       
    69 
       
    70 	// This one will be used to check if the options were explicitely set or not
       
    71 	updateFlags = accountUpdateSubcommand.Flags()
    59 }
    72 }
    60 
    73 
    61 // accountsCmd represents the accounts command
    74 // accountsCmd represents the accounts command
    62 // This command does nothing without a subcommand
    75 // This command does nothing without a subcommand
    63 var accountsCmd = &cobra.Command{
    76 var accountsCmd = &cobra.Command{
   133 	accountFollowSubcommand,
   146 	accountFollowSubcommand,
   134 	accountBlockSubcommand,
   147 	accountBlockSubcommand,
   135 	accountMuteSubcommand,
   148 	accountMuteSubcommand,
   136 	accountRelationshipsSubcommand,
   149 	accountRelationshipsSubcommand,
   137 	accountReportsSubcommand,
   150 	accountReportsSubcommand,
       
   151 	accountUpdateSubcommand,
   138 }
   152 }
   139 
   153 
   140 var accountStatusesSubcommand = &cobra.Command{
   154 var accountStatusesSubcommand = &cobra.Command{
   141 	Use:     "statuses",
   155 	Use:     "statuses",
   142 	Aliases: []string{"st"},
   156 	Aliases: []string{"st"},
   197 	RunE: func(cmd *cobra.Command, args []string) error {
   211 	RunE: func(cmd *cobra.Command, args []string) error {
   198 		return accountSubcommandsRunE("reports", args)
   212 		return accountSubcommandsRunE("reports", args)
   199 	},
   213 	},
   200 }
   214 }
   201 
   215 
       
   216 var accountUpdateSubcommand = &cobra.Command{
       
   217 	Use:   "update",
       
   218 	Short: "Update connected user account",
       
   219 	Long: `Update connected user account
       
   220 
       
   221 All flags are optional (set to an empty string if you want to delete a field).
       
   222 The flags --avatar and --header can be paths to image files or base64-encoded
       
   223 images (see Mastodon API specifications for the details).
       
   224 
       
   225 Please note the avatar and header images cannot be removed, they can only be
       
   226 replaced.`,
       
   227 	Example: `  madonctl accounts update --display-name "Mr President"
       
   228   madonctl accounts update --note "I like madonctl"
       
   229   madonctl accounts update --avatar happyface.png`,
       
   230 	RunE: func(cmd *cobra.Command, args []string) error {
       
   231 		return accountSubcommandsRunE(cmd.Name(), args)
       
   232 	},
       
   233 }
       
   234 
   202 // accountSubcommandsRunE is a generic function for status subcommands
   235 // accountSubcommandsRunE is a generic function for status subcommands
   203 func accountSubcommandsRunE(subcmd string, args []string) error {
   236 func accountSubcommandsRunE(subcmd string, args []string) error {
   204 	opt := accountsOpts
   237 	opt := accountsOpts
   205 
   238 
   206 	switch subcmd {
   239 	switch subcmd {
   207 	case "show", "search":
   240 	case "show", "search", "update":
   208 		// These subcommands do not require an account ID
   241 		// These subcommands do not require an account ID
   209 	case "favourites", "blocks", "mutes":
   242 	case "favourites", "blocks", "mutes":
   210 		// Those subcommands can not use an account ID
   243 		// Those subcommands can not use an account ID
   211 		if opt.accountID > 0 {
   244 		if opt.accountID > 0 {
   212 			return errors.New("useless account ID")
   245 			return errors.New("useless account ID")
   369 			return errors.New("missing status IDs")
   402 			return errors.New("missing status IDs")
   370 		}
   403 		}
   371 		var report *madon.Report
   404 		var report *madon.Report
   372 		report, err = gClient.ReportUser(opt.accountID, ids, opt.comment)
   405 		report, err = gClient.ReportUser(opt.accountID, ids, opt.comment)
   373 		obj = report
   406 		obj = report
       
   407 	case "update":
       
   408 		var dn, note, avatar, header *string
       
   409 		change := false
       
   410 		if updateFlags.Lookup("display-name").Changed {
       
   411 			dn = &opt.displayName
       
   412 			change = true
       
   413 		}
       
   414 		if updateFlags.Lookup("note").Changed {
       
   415 			note = &opt.note
       
   416 			change = true
       
   417 		}
       
   418 		if updateFlags.Lookup("avatar").Changed {
       
   419 			avatar = &opt.avatar
       
   420 			change = true
       
   421 		}
       
   422 		if updateFlags.Lookup("header").Changed {
       
   423 			header = &opt.header
       
   424 			change = true
       
   425 		}
       
   426 
       
   427 		if !change { // We want at least one update
       
   428 			return errors.New("missing parameters")
       
   429 		}
       
   430 
       
   431 		var account *madon.Account
       
   432 		account, err = gClient.UpdateAccount(dn, note, avatar, header)
       
   433 		obj = account
   374 	default:
   434 	default:
   375 		return errors.New("accountSubcommand: internal error")
   435 		return errors.New("accountSubcommand: internal error")
   376 	}
   436 	}
   377 
   437 
   378 	if err != nil {
   438 	if err != nil {