cmd/instance.go
changeset 37 9bc03db114c3
parent 0 5abace724584
child 38 891a3c46a62a
--- a/cmd/instance.go	Sun Apr 30 16:12:55 2017 +0200
+++ b/cmd/instance.go	Sun Apr 30 17:07:16 2017 +0200
@@ -6,34 +6,94 @@
 package cmd
 
 import (
+	"context"
+	"errors"
+	"os"
+	"strings"
+
+	"github.com/m0t0k1ch1/gomif"
 	"github.com/spf13/cobra"
+
+	"github.com/McKael/madonctl/printer"
 )
 
+var instanceOpts struct {
+	stats  bool
+	server string
+}
+
 // timelinesCmd represents the timelines command
 var instanceCmd = &cobra.Command{
 	Use:   "instance",
 	Short: "Display current instance information",
 	RunE:  instanceRunE,
+	Example: `  madonctl instance
+  madonctl instance -i mastodon.social
+  madonctl instance --stats
+  madonctl instance --stats --server mastodon.social --template '{{.Users}}'`,
 }
 
 func init() {
 	RootCmd.AddCommand(instanceCmd)
+
+	instanceCmd.Flags().BoolVar(&instanceOpts.stats, "stats", false, "Display server statistics (from instances.mastodon.xyz)")
+	instanceCmd.Flags().StringVar(&instanceOpts.server, "server", "", "Display statistics for a specific server (for --stats)")
 }
 
 func instanceRunE(cmd *cobra.Command, args []string) error {
+	opt := instanceOpts
+
+	p, err := getPrinter()
+	if err != nil {
+		return err
+	}
+
+	if opt.stats {
+		// Get instance statistics using gomif
+		if opt.server == "" {
+			if err := madonInit(false); err != nil {
+				return err
+			}
+			opt.server = strings.TrimLeft(gClient.InstanceURL, "https://")
+		}
+		is, err := instanceFetchStatus(opt.server)
+		if err != nil {
+			errPrint("Error: %s", err.Error())
+			os.Exit(1)
+		}
+		if is == nil {
+			return nil
+		}
+		istats := &printer.InstanceStatistics{
+			InstanceName:   opt.server,
+			InstanceStatus: *is,
+		}
+		return p.PrintObj(istats, nil, "")
+	}
+
+	// Get current instance data through the API
 	if err := madonInit(false); err != nil {
 		return err
 	}
-
 	i, err := gClient.GetCurrentInstance()
 	if err != nil {
 		errPrint("Error: %s", err.Error())
 		return nil
 	}
 
-	p, err := getPrinter()
-	if err != nil {
-		return err
-	}
 	return p.PrintObj(i, nil, "")
 }
+
+func instanceFetchStatus(server string) (*gomif.InstanceStatus, error) {
+	if server == "" {
+		return nil, errors.New("no instance server name")
+	}
+
+	client := gomif.NewClient()
+
+	return client.FetchLastInstanceStatus(
+		context.Background(),
+		server,
+		3600, // span (sec)
+	)
+}