cmd/instance.go
author rjp <zimpenfish@gmail.com>
Mon, 23 Jan 2023 16:39:02 +0000
changeset 267 5b91a65ba95a
parent 190 e058a8a15e22
permissions -rw-r--r--
Update to handle non-int64 IDs Pleroma/Akkoma and GotoSocial use opaque IDs rather than `int64`s like Mastodon which means that `madon` can't talk to either of those. This commit updates everything that can be an ID to `madon.ActivityID` which is an alias for `string` - can't create a specific type for it since there's more than a few places where they're concatenated directly to strings for URLs, etc. Which means it could just as easily be a direct `string` type itself but I find that having distinct types can often make the code more readable and understandable. One extra bit is that `statusOpts` has grown a `_hasReplyTo` boolean to indicate whether the `--in-reply-to` flag was given or not because we can't distinguish because "empty because default" or "empty because given and empty". Another way around this would be to set the default to some theoretically impossible or unlikely string but you never know when someone might spin up an instance where, e.g., admin posts have negative integer IDs.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
185
564d92b54b00 Update copyrights
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
     1
// Copyright © 2017-2018 Mikael Berthe <mikael@lilotux.net>
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     2
//
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     3
// Licensed under the MIT license.
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     4
// Please see the LICENSE file is this directory.
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     6
package cmd
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     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
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
	"github.com/spf13/cobra"
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    14
// timelinesCmd represents the timelines command
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
var instanceCmd = &cobra.Command{
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
	Use:   "instance",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    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
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
func init() {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    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
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    42
}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    43
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    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
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    50
	i, err := gClient.GetCurrentInstance()
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    51
	if err != nil {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    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
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    54
	}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    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
57843255fd1a Refactor printers
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
    61
	return p.printObj(i)
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    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
}