author | Mikael Berthe <mikael@lilotux.net> |
Mon, 08 May 2017 21:10:28 +0200 | |
changeset 104 | ce2b92d6d72c |
parent 81 | b1671f83e91b |
child 110 | 57843255fd1a |
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 |
"os" |
9bc03db114c3
Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
11 |
"strings" |
9bc03db114c3
Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
12 |
|
9bc03db114c3
Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
13 |
"github.com/m0t0k1ch1/gomif" |
45
b58a7ea1aeb2
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
39
diff
changeset
|
14 |
"github.com/pkg/errors" |
0 | 15 |
"github.com/spf13/cobra" |
16 |
) |
|
17 |
||
37
9bc03db114c3
Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
18 |
var instanceOpts struct { |
38
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
19 |
stats bool |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
20 |
server string |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
21 |
start, end int64 |
37
9bc03db114c3
Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
22 |
} |
9bc03db114c3
Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
23 |
|
0 | 24 |
// timelinesCmd represents the timelines command |
25 |
var instanceCmd = &cobra.Command{ |
|
26 |
Use: "instance", |
|
27 |
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
|
28 |
Long: `Display instance information |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
29 |
|
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
30 |
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
|
31 |
|
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
32 |
With '--stats', the instances.mastodon.xyz API is queried and instance |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
33 |
statistics will be returned (the instance server can be specified). |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
34 |
To get a range of statistics, both '--start' and '--end' should be provided |
39
3fa876b70df7
Update instance command usage
Mikael Berthe <mikael@lilotux.net>
parents:
38
diff
changeset
|
35 |
with UNIX timestamps (e.g. "date +%s"). |
38
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
36 |
`, |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
37 |
RunE: instanceRunE, |
37
9bc03db114c3
Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
38 |
Example: ` madonctl instance |
9bc03db114c3
Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
39 |
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
|
40 |
madonctl instance --stats |
38
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
41 |
madonctl instance --stats --start 1493565000 --end 1493566000 |
37
9bc03db114c3
Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
42 |
madonctl instance --stats --server mastodon.social --template '{{.Users}}'`, |
0 | 43 |
} |
44 |
||
45 |
func init() { |
|
46 |
RootCmd.AddCommand(instanceCmd) |
|
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 |
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
|
49 |
instanceCmd.Flags().StringVar(&instanceOpts.server, "server", "", "Display statistics for a specific server (for --stats)") |
38
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
50 |
instanceCmd.Flags().Int64Var(&instanceOpts.start, "start", 0, "Start timestamp (for --stats)") |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
51 |
instanceCmd.Flags().Int64Var(&instanceOpts.end, "end", 0, "End timestamp (for --stats)") |
0 | 52 |
} |
53 |
||
54 |
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
|
55 |
opt := instanceOpts |
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 |
if opt.stats { |
38
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
58 |
return instanceStats() |
37
9bc03db114c3
Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
59 |
} |
9bc03db114c3
Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
60 |
|
9bc03db114c3
Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
61 |
// Get current instance data through the API |
0 | 62 |
if err := madonInit(false); err != nil { |
63 |
return err |
|
64 |
} |
|
65 |
i, err := gClient.GetCurrentInstance() |
|
66 |
if err != nil { |
|
67 |
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
|
68 |
os.Exit(1) |
0 | 69 |
} |
70 |
||
38
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
71 |
p, err := getPrinter() |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
72 |
if err != nil { |
81
b1671f83e91b
Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents:
47
diff
changeset
|
73 |
errPrint("Error: %s", err.Error()) |
b1671f83e91b
Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents:
47
diff
changeset
|
74 |
os.Exit(1) |
38
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
75 |
} |
0 | 76 |
return p.PrintObj(i, nil, "") |
77 |
} |
|
37
9bc03db114c3
Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
78 |
|
38
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
79 |
func instanceStats() error { |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
80 |
opt := instanceOpts |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
81 |
|
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
82 |
// Get instance statistics using gomif |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
83 |
if opt.server == "" { |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
84 |
if err := madonInit(false); err != nil { |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
85 |
return err |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
86 |
} |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
87 |
opt.server = strings.TrimLeft(gClient.InstanceURL, "https://") |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
88 |
} |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
89 |
|
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
90 |
if opt.server == "" { |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
91 |
return errors.New("no instance server name") |
37
9bc03db114c3
Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
92 |
} |
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 |
client := gomif.NewClient() |
38
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
95 |
var obj interface{} |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
96 |
var err error |
37
9bc03db114c3
Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
97 |
|
38
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
98 |
if opt.start > 0 && opt.end > 0 { |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
99 |
var isl []*gomif.InstanceStatus |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
100 |
isl, err = client.FetchInstanceStatuses( |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
101 |
context.Background(), |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
102 |
opt.server, opt.start, opt.end, |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
103 |
) |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
104 |
obj = isl |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
105 |
} else if opt.start > 0 || opt.end > 0 { |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
106 |
return errors.New("invalid parameters: missing timestamp") |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
107 |
} else { |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
108 |
var is *gomif.InstanceStatus |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
109 |
is, err = client.FetchLastInstanceStatus( |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
110 |
context.Background(), |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
111 |
opt.server, |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
112 |
3600, // span (sec) |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
113 |
) |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
114 |
obj = is |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
115 |
} |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
116 |
if err != nil { |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
117 |
errPrint("Error: %s", err.Error()) |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
118 |
os.Exit(1) |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
119 |
} |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
120 |
if obj == nil { |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
121 |
return nil |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
122 |
} |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
123 |
|
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
124 |
p, err := getPrinter() |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
125 |
if err != nil { |
81
b1671f83e91b
Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents:
47
diff
changeset
|
126 |
errPrint("Error: %s", err.Error()) |
b1671f83e91b
Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents:
47
diff
changeset
|
127 |
os.Exit(1) |
38
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
128 |
} |
891a3c46a62a
Instance stats: sync with gomif update & add time range support
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
129 |
return p.PrintObj(obj, nil, "") |
37
9bc03db114c3
Add server statistics with gomif (using instances.mastodon.xyz API)
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
130 |
} |