cmd/instance.go
author Mikael Berthe <mikael@lilotux.net>
Sun, 30 Apr 2017 17:07:16 +0200
changeset 37 9bc03db114c3
parent 0 5abace724584
child 38 891a3c46a62a
permissions -rw-r--r--
Add server statistics with gomif (using instances.mastodon.xyz API)
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
	"errors"
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    11
	"os"
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    12
	"strings"
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    13
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    14
	"github.com/m0t0k1ch1/gomif"
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
	"github.com/spf13/cobra"
37
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    16
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    17
	"github.com/McKael/madonctl/printer"
0
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
37
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    20
var instanceOpts struct {
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    21
	stats  bool
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    22
	server string
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    23
}
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    24
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
// timelinesCmd represents the timelines command
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
var instanceCmd = &cobra.Command{
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
	Use:   "instance",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
	Short: "Display current instance information",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
	RunE:  instanceRunE,
37
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    30
	Example: `  madonctl instance
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    31
  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
    32
  madonctl instance --stats
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    33
  madonctl instance --stats --server mastodon.social --template '{{.Users}}'`,
0
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
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    36
func init() {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
	RootCmd.AddCommand(instanceCmd)
37
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    38
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    39
	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
    40
	instanceCmd.Flags().StringVar(&instanceOpts.server, "server", "", "Display statistics for a specific server (for --stats)")
0
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
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    43
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
    44
	opt := instanceOpts
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    45
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    46
	p, err := getPrinter()
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    47
	if err != nil {
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    48
		return err
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    49
	}
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    50
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    51
	if opt.stats {
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    52
		// Get instance statistics using gomif
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    53
		if opt.server == "" {
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    54
			if err := madonInit(false); err != nil {
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    55
				return err
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
			opt.server = strings.TrimLeft(gClient.InstanceURL, "https://")
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    58
		}
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    59
		is, err := instanceFetchStatus(opt.server)
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    60
		if err != nil {
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    61
			errPrint("Error: %s", err.Error())
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    62
			os.Exit(1)
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    63
		}
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    64
		if is == nil {
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    65
			return nil
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    66
		}
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    67
		istats := &printer.InstanceStatistics{
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    68
			InstanceName:   opt.server,
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    69
			InstanceStatus: *is,
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    70
		}
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    71
		return p.PrintObj(istats, nil, "")
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    72
	}
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    73
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    74
	// Get current instance data through the API
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    75
	if err := madonInit(false); err != nil {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    76
		return err
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    77
	}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    78
	i, err := gClient.GetCurrentInstance()
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    79
	if err != nil {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    80
		errPrint("Error: %s", err.Error())
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    81
		return nil
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    82
	}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    83
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    84
	return p.PrintObj(i, nil, "")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    85
}
37
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    86
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    87
func instanceFetchStatus(server string) (*gomif.InstanceStatus, error) {
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    88
	if server == "" {
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    89
		return nil, errors.New("no instance server name")
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    90
	}
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    91
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    92
	client := gomif.NewClient()
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
	return client.FetchLastInstanceStatus(
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    95
		context.Background(),
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    96
		server,
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    97
		3600, // span (sec)
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    98
	)
9bc03db114c3 Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    99
}