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