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