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.)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     1
// Copyright © 2017 Mikael Berthe <mikael@lilotux.net>
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     2
//
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     3
// Licensed under the MIT license.
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     4
// Please see the LICENSE file is this directory.
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     6
package cmd
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
import (
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
	"github.com/spf13/cobra"
20
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
    10
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
    11
	"github.com/McKael/madon"
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    14
var timelineOpts struct {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
	local bool
13
f862af8faf17 Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    16
	limit uint
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    19
// timelineCmd represents the timelines command
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    20
var timelineCmd = &cobra.Command{
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
	Use:     "timeline [home|public|:HASHTAG] [--local]",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
	Aliases: []string{"tl"},
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
	Short:   "Fetch a timeline",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
	Long: `
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
The timeline command fetches a timeline (home, local or federated).
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
It can also get a hashtag-based timeline if the keyword or prefixed with
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
':' or '#'.`,
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
	Example: `  madonctl timeline
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
  madonctl timeline public --local
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
  madonctl timeline :mastodon`,
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    31
	RunE:      timelineRunE,
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    32
	ValidArgs: []string{"home", "public"},
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    33
}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    35
func init() {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    36
	RootCmd.AddCommand(timelineCmd)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
	timelineCmd.Flags().BoolVar(&timelineOpts.local, "local", false, "Posts from the local instance")
13
f862af8faf17 Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    39
	timelineCmd.Flags().UintVarP(&timelineOpts.limit, "limit", "l", 0, "Limit number of results")
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    42
func timelineRunE(cmd *cobra.Command, args []string) error {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    43
	opt := timelineOpts
20
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
    44
	var limOpts *madon.LimitParams
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
    45
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
    46
	if opt.limit > 0 {
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
    47
		limOpts = new(madon.LimitParams)
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
    48
		limOpts.Limit = int(opt.limit)
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
    49
	}
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    50
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    51
	tl := "home"
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    52
	if len(args) > 0 {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    53
		tl = args[0]
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    54
	}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    55
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    56
	// The home timeline is the only one requiring to be logged in
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    57
	if err := madonInit(tl == "home"); err != nil {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    58
		return err
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    59
	}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    60
20
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
    61
	sl, err := gClient.GetTimelines(tl, opt.local, limOpts)
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    62
	if err != nil {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    63
		errPrint("Error: %s", err.Error())
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    64
		return nil
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    65
	}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    66
20
b0ccc09f07a2 Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
    67
	if opt.limit > 0 && len(sl) > int(opt.limit) {
13
f862af8faf17 Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    68
		sl = sl[:opt.limit]
f862af8faf17 Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    69
	}
f862af8faf17 Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    70
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    71
	p, err := getPrinter()
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    72
	if err != nil {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    73
		return err
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    74
	}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    75
	return p.PrintObj(sl, nil, "")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    76
}