bedrocktool/utils/realms.go

60 lines
1.4 KiB
Go
Raw Normal View History

2022-09-04 14:26:32 +00:00
package utils
2022-04-16 11:02:32 +00:00
import (
"context"
"flag"
2022-04-16 11:02:32 +00:00
"fmt"
"strings"
"github.com/bedrock-tool/bedrocktool/locale"
"github.com/google/subcommands"
2022-09-04 23:40:55 +00:00
"github.com/sirupsen/logrus"
2022-04-16 11:02:32 +00:00
)
2023-01-29 21:20:13 +00:00
func getRealm(ctx context.Context, realmName, id string) (name string, address string, err error) {
realms, err := GetRealmsAPI().Realms(ctx)
2022-04-16 11:02:32 +00:00
if err != nil {
return "", "", err
}
for _, realm := range realms {
2023-01-29 21:20:13 +00:00
if strings.HasPrefix(realm.Name, realmName) {
2022-08-15 00:29:01 +00:00
if id != "" && id != fmt.Sprint(id) {
continue
}
2022-08-22 10:56:02 +00:00
name = realm.Name
2022-08-23 19:06:08 +00:00
address, err = realm.Address(ctx)
2022-04-16 11:02:32 +00:00
if err != nil {
return "", "", err
}
2022-08-22 10:56:02 +00:00
return
2022-04-16 11:02:32 +00:00
}
}
return "", "", fmt.Errorf("realm not found")
}
2022-08-15 00:29:01 +00:00
type RealmListCMD struct{}
func (*RealmListCMD) Name() string { return "list-realms" }
func (*RealmListCMD) Synopsis() string { return locale.Loc("list_realms_synopsis", nil) }
2022-08-15 00:29:01 +00:00
func (c *RealmListCMD) SetFlags(f *flag.FlagSet) {}
func (c *RealmListCMD) Usage() string {
return c.Name() + ": " + c.Synopsis() + "\n"
}
2022-08-23 19:06:08 +00:00
func (c *RealmListCMD) Execute(ctx context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
2023-01-29 21:20:13 +00:00
realms, err := GetRealmsAPI().Realms(ctx)
2022-08-15 00:29:01 +00:00
if err != nil {
2022-09-04 23:40:55 +00:00
logrus.Error(err)
2022-08-15 00:29:01 +00:00
return 1
}
for _, realm := range realms {
fmt.Println(locale.Loc("realm_list_line", locale.Strmap{"Name": realm.Name, "Id": realm.ID}))
2022-08-15 00:29:01 +00:00
}
return 0
}
func init() {
2022-09-04 14:26:32 +00:00
RegisterCommand(&RealmListCMD{})
}