diff -r 9bc03db114c3 -r 891a3c46a62a cmd/instance.go --- a/cmd/instance.go Sun Apr 30 17:07:16 2017 +0200 +++ b/cmd/instance.go Sun Apr 30 17:58:06 2017 +0200 @@ -13,23 +13,32 @@ "github.com/m0t0k1ch1/gomif" "github.com/spf13/cobra" - - "github.com/McKael/madonctl/printer" ) var instanceOpts struct { - stats bool - server string + stats bool + server string + start, end int64 } // timelinesCmd represents the timelines command var instanceCmd = &cobra.Command{ Use: "instance", Short: "Display current instance information", - RunE: instanceRunE, + Long: `Display instance information + +This command display the instance information returned by the server. + +With '--stats', the instances.mastodon.xyz API is queried and instance +statistics will be returned (the instance server can be specified). +To get a range of statistics, both '--start' and '--end' should be provided +with EPOCH timestamps (e.g. "date +%s"). +`, + RunE: instanceRunE, Example: ` madonctl instance madonctl instance -i mastodon.social madonctl instance --stats + madonctl instance --stats --start 1493565000 --end 1493566000 madonctl instance --stats --server mastodon.social --template '{{.Users}}'`, } @@ -38,37 +47,15 @@ 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)") + instanceCmd.Flags().Int64Var(&instanceOpts.start, "start", 0, "Start timestamp (for --stats)") + instanceCmd.Flags().Int64Var(&instanceOpts.end, "end", 0, "End timestamp (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, "") + return instanceStats() } // Get current instance data through the API @@ -81,19 +68,61 @@ 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") +func instanceStats() error { + opt := instanceOpts + + // Get instance statistics using gomif + if opt.server == "" { + if err := madonInit(false); err != nil { + return err + } + opt.server = strings.TrimLeft(gClient.InstanceURL, "https://") + } + + if opt.server == "" { + return errors.New("no instance server name") } client := gomif.NewClient() + var obj interface{} + var err error - return client.FetchLastInstanceStatus( - context.Background(), - server, - 3600, // span (sec) - ) + if opt.start > 0 && opt.end > 0 { + var isl []*gomif.InstanceStatus + isl, err = client.FetchInstanceStatuses( + context.Background(), + opt.server, opt.start, opt.end, + ) + obj = isl + } else if opt.start > 0 || opt.end > 0 { + return errors.New("invalid parameters: missing timestamp") + } else { + var is *gomif.InstanceStatus + is, err = client.FetchLastInstanceStatus( + context.Background(), + opt.server, + 3600, // span (sec) + ) + obj = is + } + if err != nil { + errPrint("Error: %s", err.Error()) + os.Exit(1) + } + if obj == nil { + return nil + } + + p, err := getPrinter() + if err != nil { + return err + } + return p.PrintObj(obj, nil, "") }