mirror of
https://codeberg.org/june64/mrvc.git
synced 2026-01-10 16:06:33 +01:00
introduce support for checking multiple rooms in one go
This commit is contained in:
parent
cbba786b5c
commit
c42777e741
2 changed files with 142 additions and 89 deletions
|
|
@ -10,6 +10,8 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
type rooms []string
|
||||
|
||||
type configOption[T any] struct {
|
||||
flagName string
|
||||
flagDefaultValue T
|
||||
|
|
@ -20,7 +22,7 @@ type configOption[T any] struct {
|
|||
type configOptions struct {
|
||||
userID configOption[string]
|
||||
token configOption[string]
|
||||
room configOption[string]
|
||||
rooms configOption[rooms]
|
||||
printHomeserverMemberCount configOption[bool]
|
||||
homeserverVersionInfoTimeout configOption[time.Duration]
|
||||
}
|
||||
|
|
@ -28,7 +30,7 @@ type configOptions struct {
|
|||
type Config struct {
|
||||
UserID string
|
||||
Token string
|
||||
Room string
|
||||
Rooms rooms
|
||||
PrintHomeserverMemberCount bool
|
||||
HomeserverVersionInfoTimeout time.Duration
|
||||
}
|
||||
|
|
@ -36,11 +38,20 @@ type Config struct {
|
|||
var configOpts = configOptions{
|
||||
userID: configOption[string]{"user-id", "", "The Matrix user ID to use.", "MRVC_USER_ID"},
|
||||
token: configOption[string]{"token", "", "The Matrix access token to use.", "MRVC_TOKEN"},
|
||||
room: configOption[string]{"room", "", "The Matrix room to check.", "MRVC_ROOM"},
|
||||
rooms: configOption[rooms]{"room", []string{}, "The Matrix room to check. The flag can be set multiple times to check multiple rooms.", "MRVC_ROOM"},
|
||||
printHomeserverMemberCount: configOption[bool]{"print-homeserver-member-count", false, "Print the member count for each homeserver.", "MRVC_PRINT_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"},
|
||||
}
|
||||
|
||||
func (r *rooms) String() string {
|
||||
return fmt.Sprint(*r)
|
||||
}
|
||||
|
||||
func (r *rooms) Set(value string) error {
|
||||
*r = append(*r, value)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (configOpt configOption[T]) getFlagHelp() string {
|
||||
return fmt.Sprintf("%s (EnvVar: %s)", configOpt.flagHelpBase, configOpt.envVarName)
|
||||
}
|
||||
|
|
@ -74,10 +85,15 @@ func (configOpt configOption[T]) getConfigValueWithError(configFlag *T, visitedF
|
|||
|
||||
var userIdFlag = flag.String(configOpts.userID.getFlagArgs())
|
||||
var tokenFlag = flag.String(configOpts.token.getFlagArgs())
|
||||
var roomFlag = flag.String(configOpts.room.getFlagArgs())
|
||||
var printHomeserverMemberCountFlag = flag.Bool(configOpts.printHomeserverMemberCount.getFlagArgs())
|
||||
var homeserverVersionInfoTimeoutFlag = flag.Duration(configOpts.homeserverVersionInfoTimeout.getFlagArgs())
|
||||
|
||||
var roomsFlag rooms
|
||||
|
||||
func init() {
|
||||
flag.Var(&roomsFlag, configOpts.rooms.flagName, configOpts.rooms.getFlagHelp())
|
||||
}
|
||||
|
||||
func Get() Config {
|
||||
flag.Parse()
|
||||
|
||||
|
|
@ -98,7 +114,7 @@ func Get() Config {
|
|||
if err != nil {
|
||||
log.Fatal("A Matrix access token must be provided.")
|
||||
}
|
||||
config.Room, err = configOpts.room.getConfigValueWithError(roomFlag, visitedFlags, func(envVar string) string { return envVar })
|
||||
config.Rooms, err = configOpts.rooms.getConfigValueWithError(&roomsFlag, visitedFlags, func(envVar string) rooms { return []string{envVar} })
|
||||
if err != nil {
|
||||
log.Fatal("A Matrix room must be provided.")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue