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-- |
0 | 1 |
// Copyright © 2017 Mikael Berthe <mikael@lilotux.net> |
2 |
// |
|
3 |
// Licensed under the MIT license. |
|
4 |
// Please see the LICENSE file is this directory. |
|
5 |
||
6 |
package cmd |
|
7 |
||
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 | 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 | 18 |
) |
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 | 25 |
// timelinesCmd represents the timelines command |
26 |
var instanceCmd = &cobra.Command{ |
|
27 |
Use: "instance", |
|
28 |
Short: "Display current instance information", |
|
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 | 34 |
} |
35 |
||
36 |
func init() { |
|
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 | 41 |
} |
42 |
||
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 | 75 |
if err := madonInit(false); err != nil { |
76 |
return err |
|
77 |
} |
|
78 |
i, err := gClient.GetCurrentInstance() |
|
79 |
if err != nil { |
|
80 |
errPrint("Error: %s", err.Error()) |
|
81 |
return nil |
|
82 |
} |
|
83 |
||
84 |
return p.PrintObj(i, nil, "") |
|
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 |
} |