mirror of
https://codeberg.org/june64/mrvc.git
synced 2026-01-10 16:06:33 +01:00
introduce option for JSON output
Also rename printHomeserverMemberCount option accordingly to fit new output type.
This commit is contained in:
parent
1e9f23d7a9
commit
4964485d29
6 changed files with 67 additions and 20 deletions
|
|
@ -51,6 +51,7 @@ Version Support:
|
||||||
- Show member count broken down by maximum room version, Matrix server implementation and version and optionally homeserver name.
|
- Show member count broken down by maximum room version, Matrix server implementation and version and optionally homeserver name.
|
||||||
- Check multiple rooms at once by setting the `--room` flag multiple times.
|
- Check multiple rooms at once by setting the `--room` flag multiple times.
|
||||||
- Recursively check all the child rooms for the given rooms (spaces) by setting the `--recursive` flag.
|
- Recursively check all the child rooms for the given rooms (spaces) by setting the `--recursive` flag.
|
||||||
|
- Output the results as JSON by setting the `--json` flag.
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,8 @@ type configOptions struct {
|
||||||
recursive configOption[bool]
|
recursive configOption[bool]
|
||||||
recursionMaxDepth configOption[int]
|
recursionMaxDepth configOption[int]
|
||||||
recursionSuggestedOnly configOption[bool]
|
recursionSuggestedOnly configOption[bool]
|
||||||
printHomeserverMemberCount configOption[bool]
|
json configOption[bool]
|
||||||
|
outputHomeserverMemberCount configOption[bool]
|
||||||
homeserverVersionInfoTimeout configOption[time.Duration]
|
homeserverVersionInfoTimeout configOption[time.Duration]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -37,7 +38,8 @@ type Config struct {
|
||||||
Recursive bool
|
Recursive bool
|
||||||
RecursionMaxDepth *int
|
RecursionMaxDepth *int
|
||||||
RecursionSuggestedOnly bool
|
RecursionSuggestedOnly bool
|
||||||
PrintHomeserverMemberCount bool
|
JSON bool
|
||||||
|
OutputHomeserverMemberCount bool
|
||||||
HomeserverVersionInfoTimeout time.Duration
|
HomeserverVersionInfoTimeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -48,7 +50,8 @@ var configOpts = configOptions{
|
||||||
recursive: configOption[bool]{"recursive", false, "Recursively check the child rooms for the given rooms (spaces) as well.", "MRVC_RECURSIVE"},
|
recursive: configOption[bool]{"recursive", false, "Recursively check the child rooms for the given rooms (spaces) as well.", "MRVC_RECURSIVE"},
|
||||||
recursionMaxDepth: configOption[int]{"recursion-max-depth", 0, "The maximum depth when recursively getting child rooms. Defaults to 0, which gets interpreted as the servers default.", "MRVC_RECURSION_MAX_DEPTH"},
|
recursionMaxDepth: configOption[int]{"recursion-max-depth", 0, "The maximum depth when recursively getting child rooms. Defaults to 0, which gets interpreted as the servers default.", "MRVC_RECURSION_MAX_DEPTH"},
|
||||||
recursionSuggestedOnly: configOption[bool]{"recursion-suggested-only", false, "Only include the suggested children, when recursively getting child rooms.", "MRVC_RECURSION_SUGGESTED_ONLY"},
|
recursionSuggestedOnly: configOption[bool]{"recursion-suggested-only", false, "Only include the suggested children, when recursively getting child rooms.", "MRVC_RECURSION_SUGGESTED_ONLY"},
|
||||||
printHomeserverMemberCount: configOption[bool]{"print-homeserver-member-count", false, "Print the member count for each homeserver.", "MRVC_PRINT_HOMESERVER_MEMBER_COUNT"},
|
json: configOption[bool]{"json", false, "Output result as JSON.", "MRVC_JSON"},
|
||||||
|
outputHomeserverMemberCount: configOption[bool]{"output-homeserver-member-count", false, "Output the member count for each homeserver.", "MRVC_OUTPUT_HOMESERVER_MEMBER_COUNT"},
|
||||||
homeserverVersionInfoTimeout: configOption[time.Duration]{"homeserver-version-info-timeout", time.Second * 5, "Timeout for getting the homeservers version information per homeserver.", "MRVC_HOMESERVER_VERSION_INFO_TIMEOUT"},
|
homeserverVersionInfoTimeout: configOption[time.Duration]{"homeserver-version-info-timeout", time.Second * 5, "Timeout for getting the homeservers version information per homeserver.", "MRVC_HOMESERVER_VERSION_INFO_TIMEOUT"},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -97,7 +100,8 @@ var tokenFlag = flag.String(configOpts.token.getFlagArgs())
|
||||||
var recursiveFlag = flag.Bool(configOpts.recursive.getFlagArgs())
|
var recursiveFlag = flag.Bool(configOpts.recursive.getFlagArgs())
|
||||||
var recursionMaxDepthFlag = flag.Int(configOpts.recursionMaxDepth.getFlagArgs())
|
var recursionMaxDepthFlag = flag.Int(configOpts.recursionMaxDepth.getFlagArgs())
|
||||||
var recursionSuggestedOnlyFlag = flag.Bool(configOpts.recursionSuggestedOnly.getFlagArgs())
|
var recursionSuggestedOnlyFlag = flag.Bool(configOpts.recursionSuggestedOnly.getFlagArgs())
|
||||||
var printHomeserverMemberCountFlag = flag.Bool(configOpts.printHomeserverMemberCount.getFlagArgs())
|
var jsonFlag = flag.Bool(configOpts.json.getFlagArgs())
|
||||||
|
var outputHomeserverMemberCountFlag = flag.Bool(configOpts.outputHomeserverMemberCount.getFlagArgs())
|
||||||
var homeserverVersionInfoTimeoutFlag = flag.Duration(configOpts.homeserverVersionInfoTimeout.getFlagArgs())
|
var homeserverVersionInfoTimeoutFlag = flag.Duration(configOpts.homeserverVersionInfoTimeout.getFlagArgs())
|
||||||
|
|
||||||
var roomsFlag rooms
|
var roomsFlag rooms
|
||||||
|
|
@ -160,10 +164,18 @@ func Get() Config {
|
||||||
}
|
}
|
||||||
return parsedEnvVar
|
return parsedEnvVar
|
||||||
})
|
})
|
||||||
config.PrintHomeserverMemberCount = configOpts.printHomeserverMemberCount.getConfigValueWithDefault(printHomeserverMemberCountFlag, visitedFlags, func(envVar string) bool {
|
config.JSON = configOpts.json.getConfigValueWithDefault(jsonFlag, visitedFlags, func(envVar string) bool {
|
||||||
parsedEnvVar, err := strconv.ParseBool(envVar)
|
parsedEnvVar, err := strconv.ParseBool(envVar)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error parsing %s:\n", configOpts.printHomeserverMemberCount.envVarName)
|
log.Printf("Error parsing %s:\n", configOpts.json.envVarName)
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
return parsedEnvVar
|
||||||
|
})
|
||||||
|
config.OutputHomeserverMemberCount = configOpts.outputHomeserverMemberCount.getConfigValueWithDefault(outputHomeserverMemberCountFlag, visitedFlags, func(envVar string) bool {
|
||||||
|
parsedEnvVar, err := strconv.ParseBool(envVar)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error parsing %s:\n", configOpts.outputHomeserverMemberCount.envVarName)
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
return parsedEnvVar
|
return parsedEnvVar
|
||||||
|
|
|
||||||
6
main.go
6
main.go
|
|
@ -43,5 +43,9 @@ func main() {
|
||||||
|
|
||||||
roomInfoTree := roominfotree.Get(config.Rooms, config.Recursive, config.RecursionMaxDepth, config.RecursionSuggestedOnly, client, federationClient)
|
roomInfoTree := roominfotree.Get(config.Rooms, config.Recursive, config.RecursionMaxDepth, config.RecursionSuggestedOnly, client, federationClient)
|
||||||
|
|
||||||
output.Print(roomInfoTree, config.PrintHomeserverMemberCount)
|
if config.JSON {
|
||||||
|
output.PrintJSON(roomInfoTree, config.OutputHomeserverMemberCount)
|
||||||
|
} else {
|
||||||
|
output.Print(roomInfoTree, config.OutputHomeserverMemberCount)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
30
output/json.go
Normal file
30
output/json.go
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
package output
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"codeberg.org/june64/mrvc/roominfotree"
|
||||||
|
)
|
||||||
|
|
||||||
|
func PrintJSON(roomInfoTree roominfotree.RoomInfoTree, outputHomeserverMemberCount bool) {
|
||||||
|
if !outputHomeserverMemberCount {
|
||||||
|
for _, roomInfo := range roomInfoTree {
|
||||||
|
for _, maxRoomVersionInfo := range roomInfo.MaxRoomVersions {
|
||||||
|
for _, serverInfo := range maxRoomVersionInfo.Servers {
|
||||||
|
for _, versionInfo := range serverInfo.Versions {
|
||||||
|
versionInfo.Homeservers = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
json, err := json.Marshal(roomInfoTree)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
os.Stdout.Write(json)
|
||||||
|
}
|
||||||
|
|
@ -39,7 +39,7 @@ func compareVersionStrings(a, b string) int {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Print(roomInfoTree roominfotree.RoomInfoTree, printHomeserverMemberCount bool) {
|
func Print(roomInfoTree roominfotree.RoomInfoTree, outputHomeserverMemberCount bool) {
|
||||||
for roomID, roomInfo := range roomInfoTree {
|
for roomID, roomInfo := range roomInfoTree {
|
||||||
fmt.Println("Room:")
|
fmt.Println("Room:")
|
||||||
fmt.Printf(" %s -> %d\n", roomID, roomInfo.MemberCount)
|
fmt.Printf(" %s -> %d\n", roomID, roomInfo.MemberCount)
|
||||||
|
|
@ -90,7 +90,7 @@ func Print(roomInfoTree roominfotree.RoomInfoTree, printHomeserverMemberCount bo
|
||||||
|
|
||||||
fmt.Printf(" %s -> %d\n", versionKey, versionInfo.MemberCount)
|
fmt.Printf(" %s -> %d\n", versionKey, versionInfo.MemberCount)
|
||||||
|
|
||||||
if printHomeserverMemberCount {
|
if outputHomeserverMemberCount {
|
||||||
homeserverKeys := make([]string, 0, len(versionInfo.Homeservers))
|
homeserverKeys := make([]string, 0, len(versionInfo.Homeservers))
|
||||||
for key := range versionInfo.Homeservers {
|
for key := range versionInfo.Homeservers {
|
||||||
homeserverKeys = append(homeserverKeys, key)
|
homeserverKeys = append(homeserverKeys, key)
|
||||||
|
|
|
||||||
|
|
@ -20,29 +20,29 @@ type homeserverServerVersionInfo struct {
|
||||||
type RoomInfoTree map[id.RoomID](*RoomInfo)
|
type RoomInfoTree map[id.RoomID](*RoomInfo)
|
||||||
|
|
||||||
type RoomInfo struct {
|
type RoomInfo struct {
|
||||||
MemberCount uint
|
MemberCount uint `json:"memberCount"`
|
||||||
GivenAliases []string
|
GivenAliases []string `json:"givenAliases"`
|
||||||
Aliases []string
|
Aliases []string `json:"aliases"`
|
||||||
MaxRoomVersions map[string](*MaxRoomVersionInfo)
|
MaxRoomVersions map[string](*MaxRoomVersionInfo) `json:"maxRoomVersions"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type MaxRoomVersionInfo struct {
|
type MaxRoomVersionInfo struct {
|
||||||
MemberCount uint
|
MemberCount uint `json:"memberCount"`
|
||||||
Servers map[string](*ServerInfo)
|
Servers map[string](*ServerInfo) `json:"servers"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ServerInfo struct {
|
type ServerInfo struct {
|
||||||
MemberCount uint
|
MemberCount uint `json:"memberCount"`
|
||||||
Versions map[string](*VersionInfo)
|
Versions map[string](*VersionInfo) `json:"versions"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type VersionInfo struct {
|
type VersionInfo struct {
|
||||||
MemberCount uint
|
MemberCount uint `json:"memberCount"`
|
||||||
Homeservers map[string](*HomeserverInfo)
|
Homeservers map[string](*HomeserverInfo) `json:"homeservers,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type HomeserverInfo struct {
|
type HomeserverInfo struct {
|
||||||
MemberCount uint
|
MemberCount uint `json:"memberCount"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var unknownServerVersionInfo = fclient.Version{
|
var unknownServerVersionInfo = fclient.Version{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue