cmd/emojis.go
changeset 191 ae9b3c28fab3
parent 187 63d1e5751300
child 239 605a00e9d1ab
equal deleted inserted replaced
190:e058a8a15e22 191:ae9b3c28fab3
       
     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/spf13/cobra"
       
    12 
       
    13 	"github.com/McKael/madon"
       
    14 )
       
    15 
       
    16 var emojiOpts struct {
       
    17 	// Used for several subcommands to limit the number of results
       
    18 	limit, keep uint
       
    19 	//sinceID, maxID int64
       
    20 	all bool
       
    21 }
       
    22 
       
    23 //emojiCmd represents the emoji command
       
    24 var emojiCmd = &cobra.Command{
       
    25 	Use:     "emojis",
       
    26 	Aliases: []string{"emoji"},
       
    27 	Short:   "Display server emojis",
       
    28 	RunE:    emojiGetRunE, // Defaults to list
       
    29 }
       
    30 
       
    31 func init() {
       
    32 	RootCmd.AddCommand(emojiCmd)
       
    33 
       
    34 	// Subcommands
       
    35 	emojiCmd.AddCommand(emojiSubcommands...)
       
    36 
       
    37 	emojiGetCustomSubcommand.Flags().UintVarP(&emojiOpts.limit, "limit", "l", 0, "Limit number of API results")
       
    38 	emojiGetCustomSubcommand.Flags().UintVarP(&emojiOpts.keep, "keep", "k", 0, "Limit number of results")
       
    39 	emojiGetCustomSubcommand.Flags().BoolVar(&emojiOpts.all, "all", false, "Fetch all results")
       
    40 }
       
    41 
       
    42 var emojiSubcommands = []*cobra.Command{
       
    43 	emojiGetCustomSubcommand,
       
    44 }
       
    45 
       
    46 var emojiGetCustomSubcommand = &cobra.Command{
       
    47 	Use:     "list",
       
    48 	Short:   "Display the custom emojis (default subcommand)",
       
    49 	Long:    `Display the list of custom emojis of the instance.`,
       
    50 	Aliases: []string{"get", "display", "show"},
       
    51 	RunE:    emojiGetRunE,
       
    52 }
       
    53 
       
    54 func emojiGetRunE(cmd *cobra.Command, args []string) error {
       
    55 	opt := emojiOpts
       
    56 
       
    57 	// Set up LimitParams
       
    58 	var limOpts *madon.LimitParams
       
    59 	if opt.all || opt.limit > 0 {
       
    60 		limOpts = new(madon.LimitParams)
       
    61 		limOpts.All = opt.all
       
    62 	}
       
    63 	if opt.limit > 0 {
       
    64 		limOpts.Limit = int(opt.limit)
       
    65 	}
       
    66 
       
    67 	// We don't have to log in
       
    68 	if err := madonInit(false); err != nil {
       
    69 		return err
       
    70 	}
       
    71 
       
    72 	var obj interface{}
       
    73 	var err error
       
    74 
       
    75 	var emojiList []madon.Emoji
       
    76 	emojiList, err = gClient.GetCustomEmojis(limOpts)
       
    77 
       
    78 	if opt.keep > 0 && len(emojiList) > int(opt.keep) {
       
    79 		emojiList = emojiList[:opt.keep]
       
    80 	}
       
    81 
       
    82 	obj = emojiList
       
    83 
       
    84 	if err != nil {
       
    85 		errPrint("Error: %s", err.Error())
       
    86 		os.Exit(1)
       
    87 	}
       
    88 	if obj == nil {
       
    89 		return nil
       
    90 	}
       
    91 
       
    92 	p, err := getPrinter()
       
    93 	if err != nil {
       
    94 		errPrint("Error: %v", err)
       
    95 		os.Exit(1)
       
    96 	}
       
    97 	return p.printObj(obj)
       
    98 }