cmd/timelines.go
author Mikael Berthe <mikael@lilotux.net>
Fri, 28 Apr 2017 15:39:32 +0200
changeset 20 b0ccc09f07a2
parent 13 f862af8faf17
child 22 5778b09bc6fe
permissions -rw-r--r--
Sync with Madon library update; use limit API parameter The --limit argument will be used in the API query. Note that the Mastodon server does not have the same maximum "limit" value for for the different end points. (We do not check the user value in madonctl.)

// Copyright © 2017 Mikael Berthe <mikael@lilotux.net>
//
// Licensed under the MIT license.
// Please see the LICENSE file is this directory.

package cmd

import (
	"github.com/spf13/cobra"

	"github.com/McKael/madon"
)

var timelineOpts struct {
	local bool
	limit uint
}

// timelineCmd represents the timelines command
var timelineCmd = &cobra.Command{
	Use:     "timeline [home|public|:HASHTAG] [--local]",
	Aliases: []string{"tl"},
	Short:   "Fetch a timeline",
	Long: `
The timeline command fetches a timeline (home, local or federated).
It can also get a hashtag-based timeline if the keyword or prefixed with
':' or '#'.`,
	Example: `  madonctl timeline
  madonctl timeline public --local
  madonctl timeline :mastodon`,
	RunE:      timelineRunE,
	ValidArgs: []string{"home", "public"},
}

func init() {
	RootCmd.AddCommand(timelineCmd)

	timelineCmd.Flags().BoolVar(&timelineOpts.local, "local", false, "Posts from the local instance")
	timelineCmd.Flags().UintVarP(&timelineOpts.limit, "limit", "l", 0, "Limit number of results")
}

func timelineRunE(cmd *cobra.Command, args []string) error {
	opt := timelineOpts
	var limOpts *madon.LimitParams

	if opt.limit > 0 {
		limOpts = new(madon.LimitParams)
		limOpts.Limit = int(opt.limit)
	}

	tl := "home"
	if len(args) > 0 {
		tl = args[0]
	}

	// The home timeline is the only one requiring to be logged in
	if err := madonInit(tl == "home"); err != nil {
		return err
	}

	sl, err := gClient.GetTimelines(tl, opt.local, limOpts)
	if err != nil {
		errPrint("Error: %s", err.Error())
		return nil
	}

	if opt.limit > 0 && len(sl) > int(opt.limit) {
		sl = sl[:opt.limit]
	}

	p, err := getPrinter()
	if err != nil {
		return err
	}
	return p.PrintObj(sl, nil, "")
}