From 67228114256a5e8d9a8d9457c022caa50674a345 Mon Sep 17 00:00:00 2001 From: James Hillyerd Date: Sat, 4 Feb 2017 18:07:25 -0800 Subject: [PATCH] Beginnings of a command line REST client --- .gitignore | 4 ++- cmd/client/list.go | 54 +++++++++++++++++++++++++++++++++++ cmd/client/main.go | 45 +++++++++++++++++++++++++++++ cmd/client/mbox.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 172 insertions(+), 1 deletion(-) create mode 100644 cmd/client/list.go create mode 100644 cmd/client/main.go create mode 100644 cmd/client/mbox.go diff --git a/.gitignore b/.gitignore index 45f7b09..0cd2d89 100644 --- a/.gitignore +++ b/.gitignore @@ -25,10 +25,12 @@ _testmain.go *.swp *.swo -# our binary +# our binaries /inbucket /inbucket.exe /target/** +/cmd/client/client +/cmd/client/client.exe # local goxc config .goxc.local.json diff --git a/cmd/client/list.go b/cmd/client/list.go new file mode 100644 index 0000000..eda64bb --- /dev/null +++ b/cmd/client/list.go @@ -0,0 +1,54 @@ +package main + +import ( + "context" + "flag" + "fmt" + + "github.com/google/subcommands" + "github.com/jhillyerd/inbucket/rest/client" +) + +type listCmd struct { + mailbox string +} + +func (*listCmd) Name() string { + return "list" +} + +func (*listCmd) Synopsis() string { + return "list contents of mailbox" +} + +func (*listCmd) Usage() string { + return `list : + list message IDs in mailbox +` +} + +func (l *listCmd) SetFlags(f *flag.FlagSet) { +} + +func (l *listCmd) Execute( + _ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { + mailbox := f.Arg(0) + if mailbox == "" { + return usage("mailbox required") + } + // Setup rest client + c, err := client.New(baseURL()) + if err != nil { + return fatal("Couldn't build client", err) + } + // Get list + headers, err := c.ListMailbox(mailbox) + if err != nil { + return fatal("REST call failed", err) + } + for _, h := range headers { + fmt.Println(h.ID) + } + + return subcommands.ExitSuccess +} diff --git a/cmd/client/main.go b/cmd/client/main.go new file mode 100644 index 0000000..b56b63b --- /dev/null +++ b/cmd/client/main.go @@ -0,0 +1,45 @@ +// Package main implements a command line client for the Inbucket REST API +package main + +import ( + "context" + "flag" + "fmt" + "os" + + "github.com/google/subcommands" +) + +var host = flag.String("host", "localhost", "host/IP of Inbucket server") +var port = flag.Uint("port", 9000, "HTTP port of Inbucket server") + +func main() { + // Important top-level flags + subcommands.ImportantFlag("host") + subcommands.ImportantFlag("port") + // Setup standard helpers + subcommands.Register(subcommands.HelpCommand(), "") + subcommands.Register(subcommands.FlagsCommand(), "") + subcommands.Register(subcommands.CommandsCommand(), "") + // Setup my commands + subcommands.Register(&listCmd{}, "") + subcommands.Register(&mboxCmd{}, "") + // Parse and execute + flag.Parse() + ctx := context.Background() + os.Exit(int(subcommands.Execute(ctx))) +} + +func baseURL() string { + return fmt.Sprintf("http://%s:%v", *host, *port) +} + +func fatal(msg string, err error) subcommands.ExitStatus { + fmt.Fprintf(os.Stderr, "%s: %v\n", msg, err) + return subcommands.ExitFailure +} + +func usage(msg string) subcommands.ExitStatus { + fmt.Fprintln(os.Stderr, msg) + return subcommands.ExitUsageError +} diff --git a/cmd/client/mbox.go b/cmd/client/mbox.go new file mode 100644 index 0000000..6943796 --- /dev/null +++ b/cmd/client/mbox.go @@ -0,0 +1,70 @@ +package main + +import ( + "context" + "flag" + "fmt" + "os" + + "github.com/google/subcommands" + "github.com/jhillyerd/inbucket/rest/client" +) + +type mboxCmd struct { + mailbox string + delete bool +} + +func (*mboxCmd) Name() string { + return "mbox" +} + +func (*mboxCmd) Synopsis() string { + return "output mailbox in mbox format" +} + +func (*mboxCmd) Usage() string { + return `mbox [options] : + output mailbox in mbox format +` +} + +func (m *mboxCmd) SetFlags(f *flag.FlagSet) { + f.BoolVar(&m.delete, "delete", false, "delete messages after output") +} + +func (m *mboxCmd) Execute( + _ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { + mailbox := f.Arg(0) + if mailbox == "" { + return usage("mailbox required") + } + // Setup rest client + c, err := client.New(baseURL()) + if err != nil { + return fatal("Couldn't build client", err) + } + // Get list + headers, err := c.ListMailbox(mailbox) + if err != nil { + return fatal("List REST call failed", err) + } + for _, h := range headers { + source, err := h.GetSource() + if err != nil { + return fatal("Source REST call failed", err) + } + fmt.Printf("From %s\n", h.From) + // TODO Escape "From " in message bodies with > + source.WriteTo(os.Stdout) + fmt.Println() + if m.delete { + err = h.Delete() + if err != nil { + return fatal("Delete REST call failed", err) + } + } + } + + return subcommands.ExitSuccess +}