4 // Please see the LICENSE file is this directory. |
4 // Please see the LICENSE file is this directory. |
5 |
5 |
6 package cmd |
6 package cmd |
7 |
7 |
8 import ( |
8 import ( |
9 "context" |
|
10 "os" |
9 "os" |
11 "strings" |
|
12 |
10 |
13 "github.com/m0t0k1ch1/gomif" |
|
14 "github.com/pkg/errors" |
|
15 "github.com/spf13/cobra" |
11 "github.com/spf13/cobra" |
16 ) |
12 ) |
17 |
|
18 var instanceOpts struct { |
|
19 stats bool |
|
20 server string |
|
21 start, end int64 |
|
22 } |
|
23 |
13 |
24 // timelinesCmd represents the timelines command |
14 // timelinesCmd represents the timelines command |
25 var instanceCmd = &cobra.Command{ |
15 var instanceCmd = &cobra.Command{ |
26 Use: "instance", |
16 Use: "instance", |
27 Short: "Display current instance information", |
17 Short: "Display current instance information", |
28 Long: `Display instance information |
18 Long: `Display instance information |
29 |
19 |
30 This command display the instance information returned by the server. |
20 This command display the instance information returned by the server. |
31 |
|
32 With '--stats', the instances.mastodon.xyz API is queried and instance |
|
33 statistics will be returned (the instance server can be specified). |
|
34 To get a range of statistics, both '--start' and '--end' should be provided |
|
35 with UNIX timestamps (e.g. "date +%s"). |
|
36 `, |
21 `, |
37 RunE: instanceRunE, |
22 RunE: instanceRunE, |
38 Example: ` madonctl instance |
|
39 madonctl instance -i mastodon.social |
|
40 madonctl instance --stats |
|
41 madonctl instance --stats --start 1493565000 --end 1493566000 |
|
42 madonctl instance --stats --server mastodon.social --template '{{.Users}}'`, |
|
43 } |
23 } |
44 |
24 |
45 func init() { |
25 func init() { |
46 RootCmd.AddCommand(instanceCmd) |
26 RootCmd.AddCommand(instanceCmd) |
47 |
|
48 instanceCmd.Flags().BoolVar(&instanceOpts.stats, "stats", false, "Display server statistics (from instances.mastodon.xyz)") |
|
49 instanceCmd.Flags().StringVar(&instanceOpts.server, "server", "", "Display statistics for a specific server (for --stats)") |
|
50 instanceCmd.Flags().Int64Var(&instanceOpts.start, "start", 0, "Start timestamp (for --stats)") |
|
51 instanceCmd.Flags().Int64Var(&instanceOpts.end, "end", 0, "End timestamp (for --stats)") |
|
52 } |
27 } |
53 |
28 |
54 func instanceRunE(cmd *cobra.Command, args []string) error { |
29 func instanceRunE(cmd *cobra.Command, args []string) error { |
55 opt := instanceOpts |
30 if err := madonInit(false); err != nil { |
56 |
31 return err |
57 if opt.stats { |
|
58 return instanceStats() |
|
59 } |
32 } |
60 |
33 |
61 // Get current instance data through the API |
34 // Get current instance data through the API |
62 if err := madonInit(false); err != nil { |
|
63 return err |
|
64 } |
|
65 i, err := gClient.GetCurrentInstance() |
35 i, err := gClient.GetCurrentInstance() |
66 if err != nil { |
36 if err != nil { |
67 errPrint("Error: %s", err.Error()) |
37 errPrint("Error: %s", err.Error()) |
68 os.Exit(1) |
38 os.Exit(1) |
69 } |
39 } |
73 errPrint("Error: %s", err.Error()) |
43 errPrint("Error: %s", err.Error()) |
74 os.Exit(1) |
44 os.Exit(1) |
75 } |
45 } |
76 return p.printObj(i) |
46 return p.printObj(i) |
77 } |
47 } |
78 |
|
79 func instanceStats() error { |
|
80 opt := instanceOpts |
|
81 |
|
82 // Get instance statistics using gomif |
|
83 if opt.server == "" { |
|
84 if err := madonInit(false); err != nil { |
|
85 return err |
|
86 } |
|
87 opt.server = strings.TrimLeft(gClient.InstanceURL, "https://") |
|
88 } |
|
89 |
|
90 if opt.server == "" { |
|
91 return errors.New("no instance server name") |
|
92 } |
|
93 |
|
94 client := gomif.NewClient() |
|
95 var obj interface{} |
|
96 var err error |
|
97 |
|
98 if opt.start > 0 && opt.end > 0 { |
|
99 var isl []*gomif.InstanceStatus |
|
100 isl, err = client.FetchInstanceStatuses( |
|
101 context.Background(), |
|
102 opt.server, opt.start, opt.end, |
|
103 ) |
|
104 obj = isl |
|
105 } else if opt.start > 0 || opt.end > 0 { |
|
106 return errors.New("invalid parameters: missing timestamp") |
|
107 } else { |
|
108 var is *gomif.InstanceStatus |
|
109 is, err = client.FetchLastInstanceStatus( |
|
110 context.Background(), |
|
111 opt.server, |
|
112 3600, // span (sec) |
|
113 ) |
|
114 obj = is |
|
115 } |
|
116 if err != nil { |
|
117 errPrint("Error: %s", err.Error()) |
|
118 os.Exit(1) |
|
119 } |
|
120 if obj == nil { |
|
121 return nil |
|
122 } |
|
123 |
|
124 p, err := getPrinter() |
|
125 if err != nil { |
|
126 errPrint("Error: %s", err.Error()) |
|
127 os.Exit(1) |
|
128 } |
|
129 return p.printObj(obj) |
|
130 } |
|