Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): Group commands by openapi tag #131

Merged
merged 1 commit into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cli/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ func setupRootFromAPI(root *cobra.Command, api *API) {
}

for _, op := range api.Operations {
if op.Group != "" && !root.ContainsGroup(op.Group) {
groupName := fmt.Sprintf("%s Commands:", strings.Title(op.Group))
group := &cobra.Group{ID: op.Group, Title: groupName}
root.AddGroup(group)
}
root.AddCommand(op.command())
}
}
Expand Down
10 changes: 6 additions & 4 deletions cli/apiconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,9 @@ func initAPIConfig() {

// Register api init sub-command to register the API.
apiCommand = &cobra.Command{
Use: "api",
Short: "API management commands",
GroupID: "generic",
Use: "api",
Short: "API management commands",
}
Root.AddCommand(apiCommand)

Expand Down Expand Up @@ -176,8 +177,9 @@ func initAPIConfig() {

n := apiName
cmd := &cobra.Command{
Use: n,
Short: config.Base,
GroupID: "api",
Use: n,
Short: config.Base,
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
Expand Down
54 changes: 18 additions & 36 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,38 +50,6 @@ var Stdout io.Writer = os.Stdout
// otherwise it defaults to `os.Stderr`.
var Stderr io.Writer = os.Stderr

// Ugh, see https://github.com/spf13/cobra/issues/836
var usageTemplate = `Usage:{{if .Runnable}}
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}

Aliases:
{{.NameAndAliases}}{{end}}{{if .HasExample}}

Examples:
{{.Example}}{{end}}{{if (not .Parent)}}{{if (gt (len .Commands) 9)}}

Available API Commands:{{range .Commands}}{{if (not (or (eq .Name "help") (eq .Name "get") (eq .Name "put") (eq .Name "post") (eq .Name "patch") (eq .Name "delete") (eq .Name "head") (eq .Name "options") (eq .Name "cert") (eq .Name "api") (eq .Name "links") (eq .Name "edit") (eq .Name "completion") (eq .Name "auth-header")))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}

Generic Commands:{{range .Commands}}{{if (or (eq .Name "help") (eq .Name "get") (eq .Name "put") (eq .Name "post") (eq .Name "patch") (eq .Name "delete") (eq .Name "head") (eq .Name "options") (eq .Name "cert") (eq .Name "api") (eq .Name "links") (eq .Name "edit") (eq .Name "completion") (eq .Name "auth-header"))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{else}}{{if .HasAvailableSubCommands}}

Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}

Flags:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}

Global Flags:
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}

Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}

Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
`

var tty bool
var au aurora.Aurora

Expand Down Expand Up @@ -278,12 +246,16 @@ func Init(name string, version string) {
generic(http.MethodGet, args[0], args[1:])
},
}
Root.SetUsageTemplate(usageTemplate)
Root.AddGroup(
&cobra.Group{ID: "api", Title: "Available API Commands:"},
&cobra.Group{ID: "generic", Title: "Generic Commands:"},
)
Root.SetHelpTemplate(`{{with (or .Long .Short)}}{{. | trimTrailingWhitespaces | highlight}}

{{end}}{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`)

head := &cobra.Command{
GroupID: "generic",
Use: "head uri",
Short: "Head a URI",
Long: "Perform an HTTP HEAD on the given URI",
Expand All @@ -296,6 +268,7 @@ func Init(name string, version string) {
Root.AddCommand(head)

options := &cobra.Command{
GroupID: "generic",
Use: "options uri",
Short: "Options a URI",
Long: "Perform an HTTP OPTIONS on the given URI",
Expand All @@ -308,6 +281,7 @@ func Init(name string, version string) {
Root.AddCommand(options)

get := &cobra.Command{
GroupID: "generic",
Use: "get uri",
Short: "Get a URI",
Long: "Perform an HTTP GET on the given URI",
Expand All @@ -320,6 +294,7 @@ func Init(name string, version string) {
Root.AddCommand(get)

post := &cobra.Command{
GroupID: "generic",
Use: "post uri [body...]",
Short: "Post a URI",
Long: "Perform an HTTP POST on the given URI",
Expand All @@ -332,6 +307,7 @@ func Init(name string, version string) {
Root.AddCommand(post)

put := &cobra.Command{
GroupID: "generic",
Use: "put uri [body...]",
Short: "Put a URI",
Long: "Perform an HTTP PUT on the given URI",
Expand All @@ -344,6 +320,7 @@ func Init(name string, version string) {
Root.AddCommand(put)

patch := &cobra.Command{
GroupID: "generic",
Use: "patch uri [body...]",
Short: "Patch a URI",
Long: "Perform an HTTP PATCH on the given URI",
Expand All @@ -356,6 +333,7 @@ func Init(name string, version string) {
Root.AddCommand(patch)

delete := &cobra.Command{
GroupID: "generic",
Use: "delete uri [body...]",
Short: "Delete a URI",
Long: "Perform an HTTP DELETE on the given URI",
Expand All @@ -371,6 +349,7 @@ func Init(name string, version string) {
var noPrompt *bool
var editFormat *string
edit := &cobra.Command{
GroupID: "generic",
Use: "edit uri [-i] [body...]",
Short: "Edit a resource by URI",
Long: "Convenience function which combines a GET, edit, and PUT operation into one command",
Expand All @@ -393,9 +372,10 @@ func Init(name string, version string) {
Root.AddCommand(edit)

authHeader := &cobra.Command{
Use: "auth-header uri",
Short: "Get an auth header for a given API",
Long: "Get an OAuth2 bearer token in an Authorization header capable of being passed to other commands. Uses a cached token when possible, renewing as needed if it has expired.",
GroupID: "generic",
Use: "auth-header uri",
Short: "Get an auth header for a given API",
Long: "Get an OAuth2 bearer token in an Authorization header capable of being passed to other commands. Uses a cached token when possible, renewing as needed if it has expired.",
Example: fmt.Sprintf(` # Using API short name
$ %s auth-header my-api

Expand Down Expand Up @@ -437,6 +417,7 @@ func Init(name string, version string) {
Root.AddCommand(authHeader)

cert := &cobra.Command{
GroupID: "generic",
Use: "cert uri",
Short: "Get cert info",
Long: "Get TLS certificate information including expiration date",
Expand Down Expand Up @@ -489,6 +470,7 @@ Not after (expires): %s (%s)
Root.AddCommand(cert)

linkCmd := &cobra.Command{
GroupID: "generic",
Use: "links uri [rel1 rel2...]",
Short: "Get link relations from the given URI, with optional filtering",
Long: "Returns a list of resolved references to the link relations after making an HTTP GET request to the given URI. Additional arguments filter down the set of returned relationship names.",
Expand Down
2 changes: 2 additions & 0 deletions cli/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
// Operation represents an API action, e.g. list-things or create-user
type Operation struct {
Name string `json:"name"`
Group string `json:"group,omitempty"`
Aliases []string `json:"aliases,omitempty"`
Short string `json:"short,omitempty"`
Long string `json:"long,omitempty"`
Expand Down Expand Up @@ -52,6 +53,7 @@ func (o Operation) command() *cobra.Command {

sub := &cobra.Command{
Use: use,
GroupID: o.Group,
Aliases: o.Aliases,
Short: o.Short,
Long: long,
Expand Down
76 changes: 40 additions & 36 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,87 +3,91 @@ module github.com/danielgtaylor/restish
go 1.18

require (
github.com/AlecAivazis/survey/v2 v2.3.4
github.com/AlecAivazis/survey/v2 v2.3.6
github.com/alecthomas/chroma v0.10.0
github.com/alexeyco/simpletable v1.0.0
github.com/amzn/ion-go v1.1.3
github.com/andybalholm/brotli v1.0.4
github.com/charmbracelet/glamour v0.5.0
github.com/danielgtaylor/casing v0.0.0-20210126043903-4e55e6373ac3
github.com/danielgtaylor/go-jmespath-plus v0.0.0-20200228063638-e0b6f132acba
github.com/danielgtaylor/shorthand v1.1.0
github.com/eliukblau/pixterm v1.3.1
github.com/fxamacker/cbor/v2 v2.4.0
github.com/gbl08ma/httpcache v1.0.2
github.com/getkin/kin-openapi v0.94.0
github.com/getkin/kin-openapi v0.105.0
github.com/ghodss/yaml v1.0.0
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/gosimple/slug v1.12.0
github.com/gosimple/slug v1.13.1
github.com/hexops/gotextdiff v1.0.3
github.com/iancoleman/strcase v0.2.0
github.com/logrusorgru/aurora v2.0.3+incompatible
github.com/mattn/go-colorable v0.1.12
github.com/mattn/go-isatty v0.0.14
github.com/mitchellh/mapstructure v1.4.3
github.com/mattn/go-colorable v0.1.13
github.com/mattn/go-isatty v0.0.16
github.com/mitchellh/mapstructure v1.5.0
github.com/shamaton/msgpack/v2 v2.1.0
github.com/spf13/cobra v1.4.0
github.com/spf13/cobra v1.6.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.10.1
github.com/stretchr/testify v1.7.0
github.com/spf13/viper v1.13.0
github.com/stretchr/testify v1.8.0
github.com/tent/http-link-go v0.0.0-20130702225549-ac974c61c2f9
golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29
golang.org/x/oauth2 v0.0.0-20220309155454-6242fa91716a
gopkg.in/h2non/gock.v1 v1.0.16
golang.org/x/crypto v0.0.0-20221012134737-56aed061732a
golang.org/x/oauth2 v0.0.0-20221006150949-b44042a4b9c1
golang.org/x/term v0.0.0-20220919170432-7a66f970e087
gopkg.in/h2non/gock.v1 v1.1.2
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/aymanbagabas/go-osc52 v1.2.1 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/charmbracelet/glamour v0.5.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/disintegration/imaging v1.6.2 // indirect
github.com/dlclark/regexp2 v1.4.0 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/dlclark/regexp2 v1.7.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/swag v0.21.1 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/btree v1.0.1 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/gosimple/unidecode v1.0.1 // indirect
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/invopop/yaml v0.2.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/microcosm-cc/bluemonday v1.0.17 // indirect
github.com/microcosm-cc/bluemonday v1.0.21 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.9.0 // indirect
github.com/muesli/termenv v0.13.0 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/rivo/uniseg v0.4.2 // indirect
github.com/spf13/afero v1.9.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
github.com/twpayne/httpcache v1.0.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
github.com/yuin/goldmark v1.4.4 // indirect
github.com/yuin/goldmark v1.5.2 // indirect
github.com/yuin/goldmark-emoji v1.0.1 // indirect
golang.org/x/image v0.0.0-20220321031419-a8550c1d254a // indirect
golang.org/x/net v0.0.0-20220403103023-749bd193bc2b // indirect
golang.org/x/sys v0.0.0-20220405210540-1e041c57c461 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/image v0.0.0-20220902085622-e7cb96979f69 // indirect
golang.org/x/net v0.0.0-20221014081412-f15817d10f9b // indirect
golang.org/x/sys v0.0.0-20221013171732-95e765b1cc43 // indirect
golang.org/x/text v0.3.8 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
launchpad.net/gocheck v0.0.0-20140225173054-000000000087 // indirect
)
Loading