cmd/suggestions.go
changeset 225 f90d1e6d4f15
child 239 605a00e9d1ab
equal deleted inserted replaced
224:d33a2c4fdfbf 225:f90d1e6d4f15
       
     1 // Copyright © 2018 Mikael Berthe <mikael@lilotux.net>
       
     2 //
       
     3 // Licensed under the MIT license.
       
     4 // Please see the LICENSE file is this directory.
       
     5 
       
     6 package cmd
       
     7 
       
     8 import (
       
     9 	"os"
       
    10 
       
    11 	"github.com/pkg/errors"
       
    12 	"github.com/spf13/cobra"
       
    13 
       
    14 	"github.com/McKael/madon"
       
    15 )
       
    16 
       
    17 var suggestionsOpts struct {
       
    18 	accountID  int64
       
    19 	accountIDs string
       
    20 
       
    21 	//limit uint
       
    22 	keep uint
       
    23 	//all bool
       
    24 }
       
    25 
       
    26 //suggestionsCmd represents the suggestions command
       
    27 var suggestionsCmd = &cobra.Command{
       
    28 	Use:     "suggestions",
       
    29 	Aliases: []string{"suggestion"},
       
    30 	Short:   "Display and remove the follow suggestions",
       
    31 	RunE:    suggestionsGetRunE, // Defaults to list
       
    32 }
       
    33 
       
    34 func init() {
       
    35 	RootCmd.AddCommand(suggestionsCmd)
       
    36 
       
    37 	// Subcommands
       
    38 	suggestionsCmd.AddCommand(suggestionsSubcommands...)
       
    39 
       
    40 	//suggestionsGetSubcommand.Flags().UintVarP(&suggestionsOpts.limit, "limit", "l", 0, "Limit number of API results")
       
    41 	suggestionsGetSubcommand.Flags().UintVarP(&suggestionsOpts.keep, "keep", "k", 0, "Limit number of results")
       
    42 	//suggestionsGetSubcommand.Flags().BoolVar(&suggestionsOpts.all, "all", false, "Fetch all results")
       
    43 
       
    44 	suggestionsDeleteSubcommand.Flags().Int64VarP(&suggestionsOpts.accountID, "account-id", "a", 0, "Account ID number")
       
    45 	suggestionsDeleteSubcommand.Flags().StringVar(&suggestionsOpts.accountIDs, "account-ids", "", "Comma-separated list of account IDs")
       
    46 }
       
    47 
       
    48 var suggestionsSubcommands = []*cobra.Command{
       
    49 	suggestionsGetSubcommand,
       
    50 	suggestionsDeleteSubcommand,
       
    51 }
       
    52 
       
    53 var suggestionsGetSubcommand = &cobra.Command{
       
    54 	Use:     "list",
       
    55 	Short:   "Display the suggestions (default subcommand)",
       
    56 	Long:    `Display the list of account suggestions.`,
       
    57 	Aliases: []string{"ls", "get", "display", "show"},
       
    58 	RunE:    suggestionsGetRunE,
       
    59 }
       
    60 
       
    61 var suggestionsDeleteSubcommand = &cobra.Command{
       
    62 	Use:     "delete",
       
    63 	Short:   "Remove an account from the suggestion list",
       
    64 	Aliases: []string{"remove", "del", "rm"},
       
    65 	RunE:    suggestionsDeleteRunE,
       
    66 }
       
    67 
       
    68 func suggestionsGetRunE(cmd *cobra.Command, args []string) error {
       
    69 	opt := suggestionsOpts
       
    70 
       
    71 	/*
       
    72 		// Note: The API currently does not support pagination
       
    73 		// Set up LimitParams
       
    74 		var limOpts *madon.LimitParams
       
    75 		if opt.all || opt.limit > 0 {
       
    76 			limOpts = new(madon.LimitParams)
       
    77 			limOpts.All = opt.all
       
    78 		}
       
    79 		if opt.limit > 0 {
       
    80 			limOpts.Limit = int(opt.limit)
       
    81 		}
       
    82 	*/
       
    83 
       
    84 	// We need to be logged in
       
    85 	if err := madonInit(true); err != nil {
       
    86 		return err
       
    87 	}
       
    88 
       
    89 	var obj interface{}
       
    90 	var err error
       
    91 
       
    92 	var accountList []madon.Account
       
    93 	accountList, err = gClient.GetSuggestions(nil)
       
    94 
       
    95 	if opt.keep > 0 && len(accountList) > int(opt.keep) {
       
    96 		accountList = accountList[:opt.keep]
       
    97 	}
       
    98 
       
    99 	obj = accountList
       
   100 
       
   101 	if err != nil {
       
   102 		errPrint("Error: %s", err.Error())
       
   103 		os.Exit(1)
       
   104 	}
       
   105 	if obj == nil {
       
   106 		return nil
       
   107 	}
       
   108 
       
   109 	p, err := getPrinter()
       
   110 	if err != nil {
       
   111 		errPrint("Error: %v", err)
       
   112 		os.Exit(1)
       
   113 	}
       
   114 	return p.printObj(obj)
       
   115 }
       
   116 
       
   117 func suggestionsDeleteRunE(cmd *cobra.Command, args []string) error {
       
   118 	opt := suggestionsOpts
       
   119 	var ids []int64
       
   120 	var err error
       
   121 
       
   122 	if opt.accountID < 1 && len(opt.accountIDs) == 0 {
       
   123 		return errors.New("missing account IDs")
       
   124 	}
       
   125 	if opt.accountID > 0 && len(opt.accountIDs) > 0 {
       
   126 		return errors.New("incompatible options")
       
   127 	}
       
   128 
       
   129 	ids, err = splitIDs(opt.accountIDs)
       
   130 	if err != nil {
       
   131 		return errors.New("cannot parse account IDs")
       
   132 	}
       
   133 	if opt.accountID > 0 { // Allow --account-id
       
   134 		ids = []int64{opt.accountID}
       
   135 	}
       
   136 	if len(ids) < 1 {
       
   137 		return errors.New("missing account IDs")
       
   138 	}
       
   139 
       
   140 	// We need to be logged in
       
   141 	if err := madonInit(true); err != nil {
       
   142 		return err
       
   143 	}
       
   144 
       
   145 	for _, id := range ids {
       
   146 		if e := gClient.DeleteSuggestion(id); err != nil {
       
   147 			errPrint("Cannot remove account %d: %s", id, e)
       
   148 			err = e
       
   149 		}
       
   150 	}
       
   151 
       
   152 	if err != nil {
       
   153 		os.Exit(1)
       
   154 	}
       
   155 	return nil
       
   156 }