cmd/instance.go
author Mikael Berthe <mikael@lilotux.net>
Tue, 09 May 2017 20:14:28 +0200
changeset 110 57843255fd1a
parent 81 b1671f83e91b
child 185 564d92b54b00
permissions -rw-r--r--
Refactor printers
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 (
37
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
     9
	"context"
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    10
	"os"
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    11
	"strings"
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    12
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    13
	"github.com/m0t0k1ch1/gomif"
45
b58a7ea1aeb2 Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents: 39
diff changeset
    14
	"github.com/pkg/errors"
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
	"github.com/spf13/cobra"
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
37
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    18
var instanceOpts struct {
38
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
    19
	stats      bool
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
    20
	server     string
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
    21
	start, end int64
37
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    22
}
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    23
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
// timelinesCmd represents the timelines command
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
var instanceCmd = &cobra.Command{
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
	Use:   "instance",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
	Short: "Display current instance information",
38
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
    28
	Long: `Display instance information
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
    29
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
    30
This command display the instance information returned by the server.
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
    31
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
    32
With '--stats', the instances.mastodon.xyz API is queried and instance
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
    33
statistics will be returned (the instance server can be specified).
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
    34
To get a range of statistics, both '--start' and '--end' should be provided
39
3fa876b70df7 Update instance command usage
Mikael Berthe <mikael@lilotux.net>
parents: 38
diff changeset
    35
with UNIX timestamps (e.g. "date +%s").
38
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
    36
`,
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
    37
	RunE: instanceRunE,
37
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    38
	Example: `  madonctl instance
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    39
  madonctl instance -i mastodon.social
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    40
  madonctl instance --stats
38
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
    41
  madonctl instance --stats --start 1493565000 --end 1493566000
37
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    42
  madonctl instance --stats --server mastodon.social --template '{{.Users}}'`,
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    43
}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    44
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    45
func init() {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    46
	RootCmd.AddCommand(instanceCmd)
37
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    47
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    48
	instanceCmd.Flags().BoolVar(&instanceOpts.stats, "stats", false, "Display server statistics (from instances.mastodon.xyz)")
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    49
	instanceCmd.Flags().StringVar(&instanceOpts.server, "server", "", "Display statistics for a specific server (for --stats)")
38
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
    50
	instanceCmd.Flags().Int64Var(&instanceOpts.start, "start", 0, "Start timestamp (for --stats)")
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
    51
	instanceCmd.Flags().Int64Var(&instanceOpts.end, "end", 0, "End timestamp (for --stats)")
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    52
}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    53
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    54
func instanceRunE(cmd *cobra.Command, args []string) error {
37
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    55
	opt := instanceOpts
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    56
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    57
	if opt.stats {
38
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
    58
		return instanceStats()
37
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    59
	}
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    60
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    61
	// Get current instance data through the API
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    62
	if err := madonInit(false); err != nil {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    63
		return err
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    64
	}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    65
	i, err := gClient.GetCurrentInstance()
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    66
	if err != nil {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    67
		errPrint("Error: %s", err.Error())
47
82d8b6074309 Set exit code to non-zero when API calls fail
Mikael Berthe <mikael@lilotux.net>
parents: 45
diff changeset
    68
		os.Exit(1)
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    69
	}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    70
38
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
    71
	p, err := getPrinter()
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
    72
	if err != nil {
81
b1671f83e91b Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents: 47
diff changeset
    73
		errPrint("Error: %s", err.Error())
b1671f83e91b Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents: 47
diff changeset
    74
		os.Exit(1)
38
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
    75
	}
110
57843255fd1a Refactor printers
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
    76
	return p.printObj(i)
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    77
}
37
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    78
38
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
    79
func instanceStats() error {
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
    80
	opt := instanceOpts
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
    81
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
    82
	// Get instance statistics using gomif
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
    83
	if opt.server == "" {
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
    84
		if err := madonInit(false); err != nil {
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
    85
			return err
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
    86
		}
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
    87
		opt.server = strings.TrimLeft(gClient.InstanceURL, "https://")
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
    88
	}
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
    89
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
    90
	if opt.server == "" {
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
    91
		return errors.New("no instance server name")
37
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    92
	}
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    93
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    94
	client := gomif.NewClient()
38
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
    95
	var obj interface{}
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
    96
	var err error
37
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    97
38
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
    98
	if opt.start > 0 && opt.end > 0 {
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
    99
		var isl []*gomif.InstanceStatus
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
   100
		isl, err = client.FetchInstanceStatuses(
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
   101
			context.Background(),
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
   102
			opt.server, opt.start, opt.end,
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
   103
		)
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
   104
		obj = isl
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
   105
	} else if opt.start > 0 || opt.end > 0 {
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
   106
		return errors.New("invalid parameters: missing timestamp")
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
   107
	} else {
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
   108
		var is *gomif.InstanceStatus
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
   109
		is, err = client.FetchLastInstanceStatus(
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
   110
			context.Background(),
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
   111
			opt.server,
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
   112
			3600, // span (sec)
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
   113
		)
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
   114
		obj = is
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
   115
	}
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
   116
	if err != nil {
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
   117
		errPrint("Error: %s", err.Error())
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
   118
		os.Exit(1)
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
   119
	}
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
   120
	if obj == nil {
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
   121
		return nil
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
   122
	}
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
   123
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
   124
	p, err := getPrinter()
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
   125
	if err != nil {
81
b1671f83e91b Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents: 47
diff changeset
   126
		errPrint("Error: %s", err.Error())
b1671f83e91b Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents: 47
diff changeset
   127
		os.Exit(1)
38
891a3c46a62a Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents: 37
diff changeset
   128
	}
110
57843255fd1a Refactor printers
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   129
	return p.printObj(obj)
37
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   130
}