Add "madonctl emoji list"
authorMikael Berthe <mikael@lilotux.net>
Mon, 19 Mar 2018 23:51:45 +0100
changeset 187 63d1e5751300
parent 186 180e636f231c
child 188 a4df685950ed
Add "madonctl emoji list"
cmd/emoji.go
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cmd/emoji.go	Mon Mar 19 23:51:45 2018 +0100
@@ -0,0 +1,97 @@
+// Copyright © 2018 Mikael Berthe <mikael@lilotux.net>
+//
+// Licensed under the MIT license.
+// Please see the LICENSE file is this directory.
+
+package cmd
+
+import (
+	"os"
+
+	"github.com/spf13/cobra"
+
+	"github.com/McKael/madon"
+)
+
+var emojiOpts struct {
+	// Used for several subcommands to limit the number of results
+	limit, keep uint
+	//sinceID, maxID int64
+	all bool
+}
+
+//emojiCmd represents the emoji command
+var emojiCmd = &cobra.Command{
+	Use:     "emoji",
+	Aliases: []string{"emojis"},
+	Short:   "Display server emojis",
+}
+
+func init() {
+	RootCmd.AddCommand(emojiCmd)
+
+	// Subcommands
+	emojiCmd.AddCommand(emojiSubcommands...)
+
+	emojiGetCustomSubcommand.Flags().UintVarP(&emojiOpts.limit, "limit", "l", 0, "Limit number of API results")
+	emojiGetCustomSubcommand.Flags().UintVarP(&emojiOpts.keep, "keep", "k", 0, "Limit number of results")
+	emojiGetCustomSubcommand.Flags().BoolVar(&emojiOpts.all, "all", false, "Fetch all results")
+}
+
+var emojiSubcommands = []*cobra.Command{
+	emojiGetCustomSubcommand,
+}
+
+var emojiGetCustomSubcommand = &cobra.Command{
+	Use:     "list",
+	Short:   "Display the custom emojis",
+	Long:    `Display the list of custom emojis of the instance.`,
+	Aliases: []string{"get", "display", "show"},
+	RunE:    emojiGetRunE,
+}
+
+func emojiGetRunE(cmd *cobra.Command, args []string) error {
+	opt := emojiOpts
+
+	// Set up LimitParams
+	var limOpts *madon.LimitParams
+	if opt.all || opt.limit > 0 {
+		limOpts = new(madon.LimitParams)
+		limOpts.All = opt.all
+	}
+	if opt.limit > 0 {
+		limOpts.Limit = int(opt.limit)
+	}
+
+	// We don't have to log in
+	if err := madonInit(false); err != nil {
+		return err
+	}
+
+	var obj interface{}
+	var err error
+
+	var emojiList []madon.Emoji
+	emojiList, err = gClient.GetCustomEmojis(limOpts)
+
+	if opt.keep > 0 && len(emojiList) > int(opt.keep) {
+		emojiList = emojiList[:opt.keep]
+	}
+
+	obj = emojiList
+
+	if err != nil {
+		errPrint("Error: %s", err.Error())
+		os.Exit(1)
+	}
+	if obj == nil {
+		return nil
+	}
+
+	p, err := getPrinter()
+	if err != nil {
+		errPrint("Error: %v", err)
+		os.Exit(1)
+	}
+	return p.printObj(obj)
+}