cmd/lists.go
changeset 188 a4df685950ed
child 189 280fc76acf39
equal deleted inserted replaced
187:63d1e5751300 188:a4df685950ed
       
     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 listsOpts struct {
       
    18 	listID     int64
       
    19 	accountID  int64
       
    20 	accountIDs string
       
    21 	title      string
       
    22 
       
    23 	// Used for several subcommands to limit the number of results
       
    24 	limit, keep uint
       
    25 	all         bool
       
    26 }
       
    27 
       
    28 //listsCmd represents the lists command
       
    29 var listsCmd = &cobra.Command{
       
    30 	Use:     "lists",
       
    31 	Aliases: []string{"list"},
       
    32 	Short:   "Manage lists",
       
    33 	Example: `  madonctl lists create --title "Friends"
       
    34   madonctl lists show
       
    35   madonctl lists show --list-id 3
       
    36   madonctl lists update --list-id 3 --title "Family"
       
    37   madonctl lists delete --list-id 3
       
    38   madonctl lists accounts --list-id 2
       
    39   madonctl lists add-accounts --list-id 2 --account-ids 123,456
       
    40   madonctl lists remove-accounts --list-id 2 --account-ids 456`,
       
    41 }
       
    42 
       
    43 func init() {
       
    44 	RootCmd.AddCommand(listsCmd)
       
    45 
       
    46 	// Subcommands
       
    47 	listsCmd.AddCommand(listsSubcommands...)
       
    48 
       
    49 	listsCmd.PersistentFlags().UintVarP(&listsOpts.limit, "limit", "l", 0, "Limit number of API results")
       
    50 	listsCmd.PersistentFlags().UintVarP(&listsOpts.keep, "keep", "k", 0, "Limit number of results")
       
    51 	listsCmd.PersistentFlags().BoolVar(&listsOpts.all, "all", false, "Fetch all results")
       
    52 
       
    53 	listsCmd.PersistentFlags().Int64VarP(&listsOpts.listID, "list-id", "G", 0, "List ID")
       
    54 
       
    55 	listsGetSubcommand.Flags().Int64VarP(&listsOpts.accountID, "account-id", "a", 0, "Account ID number")
       
    56 	// XXX accountUID?
       
    57 
       
    58 	listsGetAccountsSubcommand.Flags().Int64VarP(&listsOpts.listID, "list-id", "G", 0, "List ID")
       
    59 
       
    60 	listsCreateSubcommand.Flags().StringVar(&listsOpts.title, "title", "", "List title")
       
    61 	listsUpdateSubcommand.Flags().StringVar(&listsOpts.title, "title", "", "List title")
       
    62 
       
    63 	listsAddAccountsSubcommand.Flags().StringVar(&listsOpts.accountIDs, "account-ids", "", "Comma-separated list of account IDs")
       
    64 	listsAddAccountsSubcommand.Flags().Int64VarP(&listsOpts.accountID, "account-id", "a", 0, "Account ID number")
       
    65 	listsRemoveAccountsSubcommand.Flags().StringVar(&listsOpts.accountIDs, "account-ids", "", "Comma-separated list of account IDs")
       
    66 	listsRemoveAccountsSubcommand.Flags().Int64VarP(&listsOpts.accountID, "account-id", "a", 0, "Account ID number")
       
    67 }
       
    68 
       
    69 var listsSubcommands = []*cobra.Command{
       
    70 	listsGetSubcommand,
       
    71 	listsCreateSubcommand,
       
    72 	listsUpdateSubcommand,
       
    73 	listsDeleteSubcommand,
       
    74 	listsGetAccountsSubcommand,
       
    75 	listsAddAccountsSubcommand,
       
    76 	listsRemoveAccountsSubcommand,
       
    77 }
       
    78 
       
    79 var listsGetSubcommand = &cobra.Command{
       
    80 	Use:   "show",
       
    81 	Short: "Display one or several lists",
       
    82 	// TODO Long: ``,
       
    83 	Aliases: []string{"get", "display", "ls"},
       
    84 	RunE:    listsGetRunE,
       
    85 }
       
    86 
       
    87 var listsGetAccountsSubcommand = &cobra.Command{
       
    88 	Use:   "accounts --list-id N",
       
    89 	Short: "Display a list's accounts",
       
    90 	RunE:  listsGetAccountsRunE,
       
    91 }
       
    92 
       
    93 var listsCreateSubcommand = &cobra.Command{
       
    94 	Use:   "create --title TITLE",
       
    95 	Short: "Create a list",
       
    96 	RunE:  listsSetDeleteRunE,
       
    97 }
       
    98 
       
    99 var listsUpdateSubcommand = &cobra.Command{
       
   100 	Use:   "update --list-id N --title TITLE",
       
   101 	Short: "Update a list",
       
   102 	RunE:  listsSetDeleteRunE,
       
   103 }
       
   104 
       
   105 var listsDeleteSubcommand = &cobra.Command{
       
   106 	Use:     "delete --list-id N",
       
   107 	Short:   "Delete a list",
       
   108 	Aliases: []string{"rm", "del"},
       
   109 	RunE:    listsSetDeleteRunE,
       
   110 }
       
   111 
       
   112 var listsAddAccountsSubcommand = &cobra.Command{
       
   113 	Use:     "add-accounts --list-id N --account-ids ACC1,ACC2...",
       
   114 	Short:   "Add one or several accounts to a list",
       
   115 	Aliases: []string{"add-account"},
       
   116 	RunE:    listsAddRemoveAccountsRunE,
       
   117 }
       
   118 
       
   119 var listsRemoveAccountsSubcommand = &cobra.Command{
       
   120 	Use:     "remove-accounts --list-id N --account-ids ACC1,ACC2...",
       
   121 	Short:   "Remove one or several accounts from a list",
       
   122 	Aliases: []string{"remove-account"},
       
   123 	RunE:    listsAddRemoveAccountsRunE,
       
   124 }
       
   125 
       
   126 func listsGetRunE(cmd *cobra.Command, args []string) error {
       
   127 	opt := listsOpts
       
   128 
       
   129 	// Log in
       
   130 	if err := madonInit(true); err != nil {
       
   131 		return err
       
   132 	}
       
   133 
       
   134 	// Set up LimitParams
       
   135 	var limOpts *madon.LimitParams
       
   136 	if opt.all || opt.limit > 0 {
       
   137 		limOpts = new(madon.LimitParams)
       
   138 		limOpts.All = opt.all
       
   139 	}
       
   140 	if opt.limit > 0 {
       
   141 		limOpts.Limit = int(opt.limit)
       
   142 	}
       
   143 
       
   144 	var obj interface{}
       
   145 	var err error
       
   146 
       
   147 	if opt.listID > 0 {
       
   148 		var list *madon.List
       
   149 		list, err = gClient.GetList(opt.listID)
       
   150 		obj = list
       
   151 	} else {
       
   152 		var lists []madon.List
       
   153 		lists, err = gClient.GetLists(opt.accountID, limOpts)
       
   154 
       
   155 		if opt.keep > 0 && len(lists) > int(opt.keep) {
       
   156 			lists = lists[:opt.keep]
       
   157 		}
       
   158 		obj = lists
       
   159 	}
       
   160 
       
   161 	if err != nil {
       
   162 		errPrint("Error: %s", err.Error())
       
   163 		os.Exit(1)
       
   164 	}
       
   165 	if obj == nil {
       
   166 		return nil
       
   167 	}
       
   168 
       
   169 	p, err := getPrinter()
       
   170 	if err != nil {
       
   171 		errPrint("Error: %v", err)
       
   172 		os.Exit(1)
       
   173 	}
       
   174 	return p.printObj(obj)
       
   175 }
       
   176 
       
   177 func listsGetAccountsRunE(cmd *cobra.Command, args []string) error {
       
   178 	opt := listsOpts
       
   179 
       
   180 	if opt.listID <= 0 {
       
   181 		return errors.New("missing list ID")
       
   182 	}
       
   183 
       
   184 	// Log in
       
   185 	if err := madonInit(true); err != nil {
       
   186 		return err
       
   187 	}
       
   188 
       
   189 	// Set up LimitParams
       
   190 	var limOpts *madon.LimitParams
       
   191 	if opt.all || opt.limit > 0 {
       
   192 		limOpts = new(madon.LimitParams)
       
   193 		limOpts.All = opt.all
       
   194 	}
       
   195 	if opt.limit > 0 {
       
   196 		limOpts.Limit = int(opt.limit)
       
   197 	}
       
   198 
       
   199 	var obj interface{}
       
   200 	var err error
       
   201 
       
   202 	var accounts []madon.Account
       
   203 	accounts, err = gClient.GetListAccounts(opt.listID, limOpts)
       
   204 
       
   205 	if opt.keep > 0 && len(accounts) > int(opt.keep) {
       
   206 		accounts = accounts[:opt.keep]
       
   207 	}
       
   208 	obj = accounts
       
   209 
       
   210 	if err != nil {
       
   211 		errPrint("Error: %s", err.Error())
       
   212 		os.Exit(1)
       
   213 	}
       
   214 	if obj == nil {
       
   215 		return nil
       
   216 	}
       
   217 
       
   218 	p, err := getPrinter()
       
   219 	if err != nil {
       
   220 		errPrint("Error: %v", err)
       
   221 		os.Exit(1)
       
   222 	}
       
   223 	return p.printObj(obj)
       
   224 }
       
   225 
       
   226 func listsSetDeleteRunE(cmd *cobra.Command, args []string) error {
       
   227 	const (
       
   228 		actionUnknown = iota
       
   229 		actionCreate
       
   230 		actionUpdate
       
   231 		actionDelete
       
   232 	)
       
   233 
       
   234 	var action int
       
   235 	opt := listsOpts
       
   236 
       
   237 	switch cmd.Name() {
       
   238 	case "create":
       
   239 		if opt.listID > 0 {
       
   240 			return errors.New("list ID should not be provided with create")
       
   241 		}
       
   242 		action = actionCreate
       
   243 	case "update":
       
   244 		if opt.listID <= 0 {
       
   245 			return errors.New("list ID is required")
       
   246 		}
       
   247 		action = actionUpdate
       
   248 	case "delete", "rm", "del":
       
   249 		action = actionDelete
       
   250 	}
       
   251 
       
   252 	// Additionnal checks
       
   253 	if action == actionUnknown {
       
   254 		// Shouldn't happen.  If it does, might be an unrecognized alias.
       
   255 		return errors.New("listsSetDeleteRunE: internal error")
       
   256 	}
       
   257 
       
   258 	if action != actionDelete && opt.title == "" {
       
   259 		return errors.New("the list title is required")
       
   260 	}
       
   261 
       
   262 	// Log in
       
   263 	if err := madonInit(true); err != nil {
       
   264 		return err
       
   265 	}
       
   266 
       
   267 	var obj interface{}
       
   268 	var err error
       
   269 	var list *madon.List
       
   270 
       
   271 	switch action {
       
   272 	case actionCreate:
       
   273 		list, err = gClient.CreateList(opt.title)
       
   274 		obj = list
       
   275 	case actionUpdate:
       
   276 		list, err = gClient.UpdateList(opt.listID, opt.title)
       
   277 		obj = list
       
   278 	case actionDelete:
       
   279 		err = gClient.DeleteList(opt.listID)
       
   280 		obj = nil
       
   281 	}
       
   282 
       
   283 	if err != nil {
       
   284 		errPrint("Error: %s", err.Error())
       
   285 		os.Exit(1)
       
   286 	}
       
   287 	if obj == nil {
       
   288 		return nil
       
   289 	}
       
   290 
       
   291 	p, err := getPrinter()
       
   292 	if err != nil {
       
   293 		errPrint("Error: %v", err)
       
   294 		os.Exit(1)
       
   295 	}
       
   296 	return p.printObj(obj)
       
   297 }
       
   298 
       
   299 func listsAddRemoveAccountsRunE(cmd *cobra.Command, args []string) error {
       
   300 	opt := listsOpts
       
   301 
       
   302 	if opt.listID <= 0 {
       
   303 		return errors.New("missing list ID")
       
   304 	}
       
   305 
       
   306 	var ids []int64
       
   307 	var err error
       
   308 	ids, err = splitIDs(opt.accountIDs)
       
   309 	if err != nil {
       
   310 		return errors.New("cannot parse account IDs")
       
   311 	}
       
   312 
       
   313 	if opt.accountID > 0 { // Allow --account-id
       
   314 		ids = []int64{opt.accountID}
       
   315 	}
       
   316 	if len(ids) < 1 {
       
   317 		return errors.New("missing account IDs")
       
   318 	}
       
   319 
       
   320 	// Log in
       
   321 	if err := madonInit(true); err != nil {
       
   322 		return err
       
   323 	}
       
   324 
       
   325 	switch cmd.Name() {
       
   326 	case "add-account", "add-accounts":
       
   327 		err = gClient.AddListAccounts(opt.listID, ids)
       
   328 	case "remove-account", "remove-accounts":
       
   329 		err = gClient.RemoveListAccounts(opt.listID, ids)
       
   330 	default:
       
   331 		// Shouldn't happen.  If it does, might be an unrecognized alias.
       
   332 		return errors.New("listsAddRemoveAccountsRunE: internal error")
       
   333 	}
       
   334 
       
   335 	if err != nil {
       
   336 		errPrint("Error: %s", err.Error())
       
   337 		os.Exit(1)
       
   338 	}
       
   339 
       
   340 	return nil
       
   341 }