mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-17 09:37:02 +00:00
Beginnings of a command line REST client
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -25,10 +25,12 @@ _testmain.go
|
|||||||
*.swp
|
*.swp
|
||||||
*.swo
|
*.swo
|
||||||
|
|
||||||
# our binary
|
# our binaries
|
||||||
/inbucket
|
/inbucket
|
||||||
/inbucket.exe
|
/inbucket.exe
|
||||||
/target/**
|
/target/**
|
||||||
|
/cmd/client/client
|
||||||
|
/cmd/client/client.exe
|
||||||
|
|
||||||
# local goxc config
|
# local goxc config
|
||||||
.goxc.local.json
|
.goxc.local.json
|
||||||
|
|||||||
54
cmd/client/list.go
Normal file
54
cmd/client/list.go
Normal file
@@ -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 <mailbox>:
|
||||||
|
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
|
||||||
|
}
|
||||||
45
cmd/client/main.go
Normal file
45
cmd/client/main.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
70
cmd/client/mbox.go
Normal file
70
cmd/client/mbox.go
Normal file
@@ -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] <mailbox>:
|
||||||
|
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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user