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 "errors" |
|
11 "os" |
|
12 "strings" |
|
13 |
|
14 "github.com/m0t0k1ch1/gomif" |
9 "github.com/spf13/cobra" |
15 "github.com/spf13/cobra" |
|
16 |
|
17 "github.com/McKael/madonctl/printer" |
10 ) |
18 ) |
|
19 |
|
20 var instanceOpts struct { |
|
21 stats bool |
|
22 server string |
|
23 } |
11 |
24 |
12 // timelinesCmd represents the timelines command |
25 // timelinesCmd represents the timelines command |
13 var instanceCmd = &cobra.Command{ |
26 var instanceCmd = &cobra.Command{ |
14 Use: "instance", |
27 Use: "instance", |
15 Short: "Display current instance information", |
28 Short: "Display current instance information", |
16 RunE: instanceRunE, |
29 RunE: instanceRunE, |
|
30 Example: ` madonctl instance |
|
31 madonctl instance -i mastodon.social |
|
32 madonctl instance --stats |
|
33 madonctl instance --stats --server mastodon.social --template '{{.Users}}'`, |
17 } |
34 } |
18 |
35 |
19 func init() { |
36 func init() { |
20 RootCmd.AddCommand(instanceCmd) |
37 RootCmd.AddCommand(instanceCmd) |
|
38 |
|
39 instanceCmd.Flags().BoolVar(&instanceOpts.stats, "stats", false, "Display server statistics (from instances.mastodon.xyz)") |
|
40 instanceCmd.Flags().StringVar(&instanceOpts.server, "server", "", "Display statistics for a specific server (for --stats)") |
21 } |
41 } |
22 |
42 |
23 func instanceRunE(cmd *cobra.Command, args []string) error { |
43 func instanceRunE(cmd *cobra.Command, args []string) error { |
|
44 opt := instanceOpts |
|
45 |
|
46 p, err := getPrinter() |
|
47 if err != nil { |
|
48 return err |
|
49 } |
|
50 |
|
51 if opt.stats { |
|
52 // Get instance statistics using gomif |
|
53 if opt.server == "" { |
|
54 if err := madonInit(false); err != nil { |
|
55 return err |
|
56 } |
|
57 opt.server = strings.TrimLeft(gClient.InstanceURL, "https://") |
|
58 } |
|
59 is, err := instanceFetchStatus(opt.server) |
|
60 if err != nil { |
|
61 errPrint("Error: %s", err.Error()) |
|
62 os.Exit(1) |
|
63 } |
|
64 if is == nil { |
|
65 return nil |
|
66 } |
|
67 istats := &printer.InstanceStatistics{ |
|
68 InstanceName: opt.server, |
|
69 InstanceStatus: *is, |
|
70 } |
|
71 return p.PrintObj(istats, nil, "") |
|
72 } |
|
73 |
|
74 // Get current instance data through the API |
24 if err := madonInit(false); err != nil { |
75 if err := madonInit(false); err != nil { |
25 return err |
76 return err |
26 } |
77 } |
27 |
|
28 i, err := gClient.GetCurrentInstance() |
78 i, err := gClient.GetCurrentInstance() |
29 if err != nil { |
79 if err != nil { |
30 errPrint("Error: %s", err.Error()) |
80 errPrint("Error: %s", err.Error()) |
31 return nil |
81 return nil |
32 } |
82 } |
33 |
83 |
34 p, err := getPrinter() |
|
35 if err != nil { |
|
36 return err |
|
37 } |
|
38 return p.PrintObj(i, nil, "") |
84 return p.PrintObj(i, nil, "") |
39 } |
85 } |
|
86 |
|
87 func instanceFetchStatus(server string) (*gomif.InstanceStatus, error) { |
|
88 if server == "" { |
|
89 return nil, errors.New("no instance server name") |
|
90 } |
|
91 |
|
92 client := gomif.NewClient() |
|
93 |
|
94 return client.FetchLastInstanceStatus( |
|
95 context.Background(), |
|
96 server, |
|
97 3600, // span (sec) |
|
98 ) |
|
99 } |