diff options
| author | Xe Iaso <me@xeiaso.net> | 2024-05-24 15:03:04 -0400 |
|---|---|---|
| committer | Xe Iaso <me@xeiaso.net> | 2024-05-24 15:32:00 -0400 |
| commit | 042eb1851d06597ca91a83a9cf93d4135efe1096 (patch) | |
| tree | 366e741e74aa66fa47ac13410e94c8f172a039cc /cmd/x/mi.go | |
| parent | 29d67647732f554e03a2a95164365a6ad8c825f7 (diff) | |
| download | x-042eb1851d06597ca91a83a9cf93d4135efe1096.tar.xz x-042eb1851d06597ca91a83a9cf93d4135efe1096.zip | |
cmd/x: add mi commands
Signed-off-by: Xe Iaso <me@xeiaso.net>
Diffstat (limited to 'cmd/x/mi.go')
| -rw-r--r-- | cmd/x/mi.go | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/cmd/x/mi.go b/cmd/x/mi.go new file mode 100644 index 0000000..08b5d8c --- /dev/null +++ b/cmd/x/mi.go @@ -0,0 +1,111 @@ +package main + +import ( + "context" + "flag" + "fmt" + "net/http" + + "github.com/google/subcommands" + "github.com/rodaine/table" + "google.golang.org/protobuf/types/known/emptypb" + "within.website/x/proto/mi" +) + +var ( + miURL = flag.String("mi-url", "http://mi.mi.svc.alrest.xeserv.us", "Base mi URL") +) + +type miWhoIsFront struct { + simple bool +} + +func (*miWhoIsFront) Name() string { return "who-is-front" } +func (*miWhoIsFront) Synopsis() string { return "Print who is front of the system." } +func (*miWhoIsFront) Usage() string { + return `who-is-front [--mi-url]: +Print who is front of the system. +` +} +func (wif *miWhoIsFront) SetFlags(f *flag.FlagSet) { + f.BoolVar(&wif.simple, "simple", false, "Print only the name of the front member.") +} +func (wif *miWhoIsFront) Execute(ctx context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { + cli := mi.NewSwitchTrackerProtobufClient(*miURL, http.DefaultClient) + + front, err := cli.WhoIsFront(ctx, &emptypb.Empty{}) + if err != nil { + fmt.Printf("error: %v\n", err) + return subcommands.ExitFailure + } + + if wif.simple { + fmt.Printf("%s\n", front.Member.Name) + return subcommands.ExitSuccess + } + + fmt.Printf("current front: %s\n", front.Member.Name) + + return subcommands.ExitSuccess +} + +type miSwitch struct { + member string +} + +func (*miSwitch) Name() string { return "switch" } +func (*miSwitch) Synopsis() string { return "Switch front to a different member." } +func (*miSwitch) Usage() string { + return `switch [--member]: +Switch front to a different member. +` +} +func (s *miSwitch) SetFlags(f *flag.FlagSet) { + f.StringVar(&s.member, "member", "", "Member to switch to.") +} +func (s *miSwitch) Execute(ctx context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { + cli := mi.NewSwitchTrackerProtobufClient(*miURL, http.DefaultClient) + + _, err := cli.Switch(ctx, &mi.SwitchReq{MemberName: s.member}) + if err != nil { + fmt.Printf("error: %v\n", err) + return subcommands.ExitFailure + } + + return subcommands.ExitSuccess +} + +type miListSwitches struct { + count int + page int +} + +func (*miListSwitches) Name() string { return "list-switches" } +func (*miListSwitches) Synopsis() string { return "List switches." } +func (*miListSwitches) Usage() string { + return `list-switches [--count] [--page]: +List a number of past switches. +` +} +func (ls *miListSwitches) SetFlags(f *flag.FlagSet) { + f.IntVar(&ls.count, "count", 10, "Number of switches to list.") + f.IntVar(&ls.page, "page", 0, "Page of switches to list.") +} +func (ls *miListSwitches) Execute(ctx context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { + cli := mi.NewSwitchTrackerProtobufClient(*miURL, http.DefaultClient) + + resp, err := cli.ListSwitches(ctx, &mi.ListSwitchesReq{Count: int32(ls.count), Page: int32(ls.page)}) + if err != nil { + fmt.Printf("error: %v\n", err) + return subcommands.ExitFailure + } + + tbl := table.New("Started at", "Ended at", "Member") + for _, sw := range resp.Switches { + tbl.AddRow(sw.GetSwitch().GetStartedAt(), sw.Switch.GetEndedAt(), sw.GetMember().GetName()) + } + + tbl.Print() + + return subcommands.ExitSuccess +} |
