Skip to content

Commit

Permalink
add "sbx help <cmd>"; parse command args
Browse files Browse the repository at this point in the history
this adds `sbx help <cmd>` to get more info on what a
command does (for example, what flags it accepts).

this is a precursor to add a `duration` flag to `up`, as this sets us up
to add documented flags to the `up` command.

under the hood, this also adds the ability to parse additional args
for each command (right now, only `help` uses this).
  • Loading branch information
hspearman committed Dec 18, 2024
1 parent 210e588 commit 9fbf1bd
Show file tree
Hide file tree
Showing 24 changed files with 213 additions and 97 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ To cut a new release, [publish a new tag](https://github.com/reverbdotcom/sbx/re
Every command should be a go package. Commands are
configured in `commands/commands.go`.


`make <command>.run` will build and run the command.
This runs live.

Expand Down
50 changes: 4 additions & 46 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/reverbdotcom/sbx/dash"
"github.com/reverbdotcom/sbx/down"
"github.com/reverbdotcom/sbx/graphiql"
"github.com/reverbdotcom/sbx/help"
"github.com/reverbdotcom/sbx/logs"
"github.com/reverbdotcom/sbx/name"
"github.com/reverbdotcom/sbx/summary"
Expand All @@ -12,57 +13,14 @@ import (
"github.com/reverbdotcom/sbx/web"
)

type RunFn func() (string, error)

const Help = `NAME
sbx - orchestra cli
COMMANDS
sbx help
up
down
name
dash
logs
web
graphiql
version
info
progress
DESCRIPTION
command shorthand description
help h show the help message.
up u spin up an orchestra sandbox.
down tear down an orchestra sandbox.
name n show the sandbox name.
dash d open the dashboard in a browser.
logs l open the logs in a browser.
web w open the site in a browser.
graphiql g open graphql user interface in a browser.
version v show the version of the sbx cli.
info i show the summary of the sandbox.
progress p open deployment progress in a browser.
USAGE:
sbx up
sbx name
`

func help() (string, error) {
return Help, nil
}
type RunFn func(cmdArgs []string) (string, error)

func Commands() map[string]RunFn {
return map[string]RunFn{
"up": up.Run,
"u": up.Run,
"help": help,
"h": help,
"help": help.Run,
"h": help.Run,
"name": name.Run,
"n": name.Run,
"web": web.Open,
Expand Down
2 changes: 1 addition & 1 deletion dash/dash.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const template = "https://app.datadoghq.com/dashboard/9rm-fjs-8tx/orchestra?tpl_

var openURL = open.Open

func Run() (string, error) {
func Run(_ []string) (string, error) {
err := openURL(Url())

if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions dash/dash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestRun(t *testing.T) {
return nil
}

_, err := Run()
_, err := Run([]string{})

if err != nil {
t.Errorf("got %v, want nil", err)
Expand All @@ -47,7 +47,7 @@ func TestRun(t *testing.T) {
return errors.New("open error")
}

_, err := Run()
_, err := Run([]string{})

want := "open error"
if err.Error() != want {
Expand Down
2 changes: 1 addition & 1 deletion down/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
var nameFn = name.Name
var teardownSandboxFn = github.TeardownSandbox

func Run() (string, error) {
func Run(_ []string) (string, error) {
name, err := nameFn()

if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions down/down_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func TestRun(t *testing.T) {
nameFn = func() (string, error) { return "sandbox-blake-julian-kevin", nil }
teardownSandboxFn = func(_ string) error { return nil }

_, err := Run()
_, err := Run([]string{})

if err != nil {
t.Errorf("got %v", err)
Expand All @@ -23,7 +23,7 @@ func TestRun(t *testing.T) {
want := errors.New("name error")
nameFn = func() (string, error) { return "", want }

_, err := Run()
_, err := Run([]string{})

if err.Error() != want.Error() {
t.Errorf("got %v", err)
Expand All @@ -36,7 +36,7 @@ func TestRun(t *testing.T) {
want := errors.New("teardown error")
teardownSandboxFn = func(_ string) error { return want }

_, err := Run()
_, err := Run([]string{})

if err.Error() != want.Error() {
t.Errorf("got %v", err)
Expand Down
2 changes: 1 addition & 1 deletion graphiql/graphiql.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const template = "https://graphiql-%s.int.orchestra.rvb.ai/graphql"

var openURL = open.Open

func Run() (string, error) {
func Run(_ []string) (string, error) {
err := openURL(Url())

if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions graphiql/graphiql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestRun(t *testing.T) {
return nil
}

_, err := Run()
_, err := Run([]string{})

if err != nil {
t.Errorf("got %v, want nil", err)
Expand All @@ -47,7 +47,7 @@ func TestRun(t *testing.T) {
return errors.New("open error")
}

_, err := Run()
_, err := Run([]string{})

want := "open error"
if err.Error() != want {
Expand Down
73 changes: 73 additions & 0 deletions help/help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package help

import (
"fmt"

"github.com/reverbdotcom/sbx/up"
)

type HelpFn func() (string, error)

const Help = `NAME
sbx - orchestra cli
COMMANDS
sbx help
up
down
name
dash
logs
web
graphiql
version
info
progress
DESCRIPTION
command shorthand description
help h show the help message. use "sbx help <cmd>" for more info about that command.
up u spin up an orchestra sandbox.
down tear down an orchestra sandbox.
name n show the sandbox name.
dash d open the dashboard in a browser.
logs l open the logs in a browser.
web w open the site in a browser.
graphiql g open graphql user interface in a browser.
version v show the version of the sbx cli.
info i show the summary of the sandbox.
progress p open deployment progress in a browser.
USAGE:
sbx up
sbx name
`

func Run(cmdArgs []string) (string, error) {
var totalArgs = len(cmdArgs)
if totalArgs == 0 {
return Help, nil
}

if totalArgs > 1 {
return "", fmt.Errorf("too many arguments")
}

var cmd = cmdArgs[0]
var helpFn = helps()[cmd]
if helpFn == nil {
return fmt.Sprintf("no help info available for command %s", cmd), nil
}

return helpFn()
}

func helps() map[string]HelpFn {
return map[string]HelpFn{
"up": up.Help,
}
}
52 changes: 52 additions & 0 deletions help/help_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package help

import (
"strings"
"testing"

"github.com/reverbdotcom/sbx/up"
)

func TestRun(t *testing.T) {
t.Run("it returns generic help text with no args", func(t *testing.T) {
output, err := Run([]string{})

if err != nil {
t.Errorf("unexpected error %v", err)
}

if output != Help {
t.Errorf("did not return generic help text, got %v", output)
}
})

t.Run("it fails when there's too many args", func(t *testing.T) {
output, err := Run([]string{"up", "foo"})
if err == nil {
t.Errorf("help should have errored, but got %v", output)
}
})

t.Run("it returns help text for command", func(t *testing.T) {
output, err := Run([]string{"up"})
if err != nil {
t.Errorf("unexpected error %v", err)
}

text, _ := up.Help()
if output != text {
t.Errorf("did not return help text for up, got %v", output)
}
})

t.Run("it reports when command has no help info", func(t *testing.T) {
output, err := Run([]string{"foo"})
if err != nil {
t.Errorf("unexpected error %v", err)
}

if !strings.Contains(output, "no help info") {
t.Errorf("expected to report no help info for command, but got %v", output)
}
})
}
2 changes: 1 addition & 1 deletion logs/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const template = "https://app.datadoghq.com/logs?query=version:1.0.0-sha-%v%%20k

var openURL = open.Open

func Run() (string, error) {
func Run(_ []string) (string, error) {
err := openURL(Url())

if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions logs/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestRun(t *testing.T) {
return nil
}

_, err := Run()
_, err := Run([]string{})

if err != nil {
t.Errorf("got %v, want nil", err)
Expand All @@ -54,7 +54,7 @@ func TestRun(t *testing.T) {
return errors.New("open error")
}

_, err := Run()
_, err := Run([]string{})

want := "open error"
if err.Error() != want {
Expand Down
2 changes: 1 addition & 1 deletion name/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
const maxStep = 2
const sandbox = "sandbox-"

func Run() (string, error) {
func Run(_ []string) (string, error) {
return Name()
}

Expand Down
2 changes: 1 addition & 1 deletion name/name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestRun(t *testing.T) {
return "nn-sbx-1234", nil
}

got, err := Run()
got, err := Run([]string{})
want := "sandbox-blake-julian-kevin"

if err != nil {
Expand Down
27 changes: 19 additions & 8 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,38 @@ import (

"github.com/reverbdotcom/sbx/check"
"github.com/reverbdotcom/sbx/commands"
"github.com/reverbdotcom/sbx/help"
"golang.org/x/exp/slices"
)

func Parse(args []string) (*commands.RunFn, error) {
cmd, err := command(args)
func Parse(args []string) (*commands.RunFn, []string, error) {
cmd, cmdArgs, err := command(args)

if err != nil {
return nil, err
return nil, nil, err
}

return cmdfn(*cmd)
cmdFn, err := cmdfn(*cmd)
if err != nil {
return nil, nil, err
}

return cmdFn, cmdArgs, nil
}

func command(args []string) (command *string, err error) {
func command(args []string) (command *string, commandArgs []string, err error) {
if len(args) < 2 {
return nil, errr("command required")
return nil, nil, errr("command required")
}

cmd := args[1]

return &cmd, nil
cmdArgs := []string{}
if len(args) > 2 {
cmdArgs = args[2:]
}

return &cmd, cmdArgs, nil
}

var ensureOrchestra = check.EnsureOrchestra
Expand Down Expand Up @@ -55,5 +66,5 @@ func cmdfn(command string) (*commands.RunFn, error) {
}

func errr(message string) error {
return errors.New(fmt.Sprintf("%s\n\n\n%s", message, commands.Help))
return errors.New(fmt.Sprintf("%s\n\n\n%s", message, help.Help))
}
Loading

0 comments on commit 9fbf1bd

Please sign in to comment.