chore(repo): reinitialize repository
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
type Handlers struct {
|
||||
PrintUsage func(io.Writer)
|
||||
RunServer func([]string) error
|
||||
RunAPI func([]string) error
|
||||
}
|
||||
|
||||
func (h Handlers) printUsage(w io.Writer) {
|
||||
if h.PrintUsage != nil {
|
||||
h.PrintUsage(w)
|
||||
}
|
||||
}
|
||||
|
||||
// Run executes the inbox CLI and returns a process exit code.
|
||||
func Run(args []string, stderr io.Writer, handlers Handlers) int {
|
||||
if len(args) < 2 {
|
||||
handlers.printUsage(stderr)
|
||||
return 1
|
||||
}
|
||||
|
||||
cmd := args[1]
|
||||
var err error
|
||||
|
||||
switch cmd {
|
||||
case "server":
|
||||
if handlers.RunServer == nil {
|
||||
err = fmt.Errorf("cli %s handler is not configured", cmd)
|
||||
break
|
||||
}
|
||||
err = handlers.RunServer(args[2:])
|
||||
case "api":
|
||||
if handlers.RunAPI == nil {
|
||||
err = fmt.Errorf("cli %s handler is not configured", cmd)
|
||||
break
|
||||
}
|
||||
err = handlers.RunAPI(args[2:])
|
||||
default:
|
||||
handlers.printUsage(stderr)
|
||||
return 1
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "error: %v\n", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package cli_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"inbox/internal/cli"
|
||||
)
|
||||
|
||||
func TestRunSupportsServerAndAPI(t *testing.T) {
|
||||
var stderr bytes.Buffer
|
||||
handlers := cli.Handlers{
|
||||
PrintUsage: func(w io.Writer) {
|
||||
_, _ = io.WriteString(w, "Usage: inbox <command> [flags]\n")
|
||||
},
|
||||
RunServer: func([]string) error {
|
||||
return assertErr("--workspaces-dir is required")
|
||||
},
|
||||
RunAPI: func([]string) error {
|
||||
return assertErr("api failed")
|
||||
},
|
||||
}
|
||||
|
||||
if code := cli.Run([]string{"inbox"}, &stderr, handlers); code != 1 {
|
||||
t.Fatalf("cli.Run missing command exit code = %d, want 1", code)
|
||||
}
|
||||
if !strings.Contains(stderr.String(), "Usage: inbox <command> [flags]") {
|
||||
t.Fatalf("missing command stderr = %q", stderr.String())
|
||||
}
|
||||
|
||||
stderr.Reset()
|
||||
if code := cli.Run([]string{"inbox", "web"}, &stderr, handlers); code != 1 {
|
||||
t.Fatalf("cli.Run legacy command exit code = %d, want 1", code)
|
||||
}
|
||||
if !strings.Contains(stderr.String(), "Usage: inbox <command> [flags]") {
|
||||
t.Fatalf("legacy command stderr = %q", stderr.String())
|
||||
}
|
||||
|
||||
stderr.Reset()
|
||||
if code := cli.Run([]string{"inbox", "server"}, &stderr, handlers); code != 1 {
|
||||
t.Fatalf("cli.Run server error exit code = %d, want 1", code)
|
||||
}
|
||||
if !strings.Contains(stderr.String(), "error: --workspaces-dir is required") {
|
||||
t.Fatalf("server error stderr = %q", stderr.String())
|
||||
}
|
||||
|
||||
stderr.Reset()
|
||||
if code := cli.Run([]string{"inbox", "api"}, &stderr, handlers); code != 1 {
|
||||
t.Fatalf("cli.Run api error exit code = %d, want 1", code)
|
||||
}
|
||||
if !strings.Contains(stderr.String(), "error: api failed") {
|
||||
t.Fatalf("api error stderr = %q", stderr.String())
|
||||
}
|
||||
}
|
||||
|
||||
type assertErr string
|
||||
|
||||
func (e assertErr) Error() string {
|
||||
return string(e)
|
||||
}
|
||||
Reference in New Issue
Block a user