fix(help): add HTTP serve subcommand
All checks were successful
Security Scan / security (push) Successful in 20s
All checks were successful
Security Scan / security (push) Successful in 20s
This commit is contained in:
parent
7dadf41670
commit
cdc765611f
2 changed files with 40 additions and 0 deletions
|
|
@ -9,6 +9,10 @@ import (
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var startHelpServer = func(catalog *gohelp.Catalog, addr string) error {
|
||||||
|
return gohelp.NewServer(catalog, addr).ListenAndServe()
|
||||||
|
}
|
||||||
|
|
||||||
func AddHelpCommands(root *cli.Command) {
|
func AddHelpCommands(root *cli.Command) {
|
||||||
var searchQuery string
|
var searchQuery string
|
||||||
|
|
||||||
|
|
@ -37,6 +41,18 @@ func AddHelpCommands(root *cli.Command) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var serveAddr string
|
||||||
|
serveCmd := &cli.Command{
|
||||||
|
Use: "serve",
|
||||||
|
Short: "Serve help documentation over HTTP",
|
||||||
|
Args: cobra.NoArgs,
|
||||||
|
RunE: func(cmd *cli.Command, args []string) error {
|
||||||
|
return startHelpServer(gohelp.DefaultCatalog(), serveAddr)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
serveCmd.Flags().StringVar(&serveAddr, "addr", ":8080", "HTTP listen address")
|
||||||
|
|
||||||
|
helpCmd.AddCommand(serveCmd)
|
||||||
helpCmd.Flags().StringVarP(&searchQuery, "search", "s", "", "Search help topics")
|
helpCmd.Flags().StringVarP(&searchQuery, "search", "s", "", "Search help topics")
|
||||||
root.AddCommand(helpCmd)
|
root.AddCommand(helpCmd)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -59,6 +59,30 @@ func TestAddHelpCommands_Good(t *testing.T) {
|
||||||
assert.Contains(t, out, topics[0].ID)
|
assert.Contains(t, out, topics[0].ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAddHelpCommands_Good_Serve(t *testing.T) {
|
||||||
|
root := &cli.Command{Use: "core"}
|
||||||
|
AddHelpCommands(root)
|
||||||
|
|
||||||
|
cmd, _, err := root.Find([]string{"help", "serve"})
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotNil(t, cmd)
|
||||||
|
|
||||||
|
oldStart := startHelpServer
|
||||||
|
defer func() { startHelpServer = oldStart }()
|
||||||
|
|
||||||
|
var gotAddr string
|
||||||
|
startHelpServer = func(catalog *gohelp.Catalog, addr string) error {
|
||||||
|
require.NotNil(t, catalog)
|
||||||
|
gotAddr = addr
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
require.NoError(t, cmd.Flags().Set("addr", "127.0.0.1:9090"))
|
||||||
|
err = cmd.RunE(cmd, nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "127.0.0.1:9090", gotAddr)
|
||||||
|
}
|
||||||
|
|
||||||
func TestRenderSearchResults_Good(t *testing.T) {
|
func TestRenderSearchResults_Good(t *testing.T) {
|
||||||
out := captureOutput(t, func() {
|
out := captureOutput(t, func() {
|
||||||
err := renderSearchResults([]*gohelp.SearchResult{
|
err := renderSearchResults([]*gohelp.SearchResult{
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue