author | Mikael Berthe <mikael@lilotux.net> |
Fri, 07 Sep 2018 21:47:36 +0200 | |
changeset 235 | 747f108d5821 |
parent 190 | e058a8a15e22 |
permissions | -rw-r--r-- |
185 | 1 |
// Copyright © 2017-2018 Mikael Berthe <mikael@lilotux.net> |
0 | 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 |
"os" |
9bc03db114c3
Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
10 |
|
0 | 11 |
"github.com/spf13/cobra" |
12 |
) |
|
13 |
||
14 |
// timelinesCmd represents the timelines command |
|
15 |
var instanceCmd = &cobra.Command{ |
|
16 |
Use: "instance", |
|
17 |
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
|
18 |
Long: `Display instance information |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
19 |
|
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
20 |
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
|
21 |
`, |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
22 |
RunE: instanceRunE, |
0 | 23 |
} |
24 |
||
25 |
func init() { |
|
26 |
RootCmd.AddCommand(instanceCmd) |
|
190
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
27 |
|
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
28 |
instanceCmd.AddCommand(instancePeersSubcommand) |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
29 |
instanceCmd.AddCommand(instanceActivitySubcommand) |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
30 |
} |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
31 |
|
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
32 |
var instancePeersSubcommand = &cobra.Command{ |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
33 |
Use: "peers", |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
34 |
Short: "Display the instance peers", |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
35 |
RunE: instanceStatsRunE, |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
36 |
} |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
37 |
|
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
38 |
var instanceActivitySubcommand = &cobra.Command{ |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
39 |
Use: "activity", |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
40 |
Short: "Display the instance activity", |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
41 |
RunE: instanceStatsRunE, |
0 | 42 |
} |
43 |
||
44 |
func instanceRunE(cmd *cobra.Command, args []string) error { |
|
186
180e636f231c
Remove extended statistics using the instances.mastodon.xyz API
Mikael Berthe <mikael@lilotux.net>
parents:
185
diff
changeset
|
45 |
if err := madonInit(false); err != nil { |
180e636f231c
Remove extended statistics using the instances.mastodon.xyz API
Mikael Berthe <mikael@lilotux.net>
parents:
185
diff
changeset
|
46 |
return err |
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 |
|
9bc03db114c3
Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
49 |
// Get current instance data through the API |
0 | 50 |
i, err := gClient.GetCurrentInstance() |
51 |
if err != nil { |
|
52 |
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
|
53 |
os.Exit(1) |
0 | 54 |
} |
55 |
||
38
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
56 |
p, err := getPrinter() |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
57 |
if err != nil { |
81
b1671f83e91b
Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents:
47
diff
changeset
|
58 |
errPrint("Error: %s", err.Error()) |
b1671f83e91b
Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents:
47
diff
changeset
|
59 |
os.Exit(1) |
38
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
60 |
} |
110 | 61 |
return p.printObj(i) |
0 | 62 |
} |
190
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
63 |
|
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
64 |
func instanceStatsRunE(cmd *cobra.Command, args []string) error { |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
65 |
if err := madonInit(false); err != nil { |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
66 |
return err |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
67 |
} |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
68 |
|
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
69 |
var obj interface{} |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
70 |
var err error |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
71 |
|
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
72 |
switch cmd.Name() { |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
73 |
case "peers": |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
74 |
// Get current instance peers |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
75 |
peers, err := gClient.GetInstancePeers() |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
76 |
if err != nil { |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
77 |
errPrint("Error: %s", err.Error()) |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
78 |
os.Exit(1) |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
79 |
} |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
80 |
obj = peers |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
81 |
case "activity": |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
82 |
// Get current instance activity |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
83 |
activity, err := gClient.GetInstanceActivity() |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
84 |
if err != nil { |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
85 |
errPrint("Error: %s", err.Error()) |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
86 |
os.Exit(1) |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
87 |
} |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
88 |
obj = activity |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
89 |
} |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
90 |
|
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
91 |
p, err := getPrinter() |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
92 |
if err != nil { |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
93 |
errPrint("Error: %s", err.Error()) |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
94 |
os.Exit(1) |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
95 |
} |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
96 |
return p.printObj(obj) |
e058a8a15e22
Add instance peers and instance activity
Mikael Berthe <mikael@lilotux.net>
parents:
186
diff
changeset
|
97 |
} |