Skip to content
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
6 changes: 3 additions & 3 deletions cmdx/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ const (
LOCAL_APP_DATA = "LocalAppData"
)

type ConfigLoaderOpts func(c *Config)
type ConfigLoaderOpt func(c *Config)

func WithFlags(pfs *pflag.FlagSet) ConfigLoaderOpts {
func WithFlags(pfs *pflag.FlagSet) ConfigLoaderOpt {
return func(c *Config) {
c.boundedPFlags = pfs
}
Expand Down Expand Up @@ -80,7 +80,7 @@ func (c *Config) Write(cfg interface{}) error {
return nil
}

func (c *Config) Load(cfg interface{}, opts ...ConfigLoaderOpts) error {
func (c *Config) Load(cfg interface{}, opts ...ConfigLoaderOpt) error {
for _, opt := range opts {
opt(c)
}
Expand Down
39 changes: 39 additions & 0 deletions cmdx/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cmdx

import (
"os"
"path/filepath"

"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
)

// GenerateMarkdownTree generate cobra cmd commands tree as markdown file
// rootOutputPath determines the folder where the markdown files are written
func GenerateMarkdownTree(rootOutputPath string, cmd *cobra.Command) error {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mabdh do we want to generate a single markdown or multiple?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think multiple markdowns would be easier to navigate via sidebar, wdyt @ravisuhag ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screen Shot 2022-10-19 at 13 24 04

this is how the markdown shown in Siren

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are showing it reference i think it better to show it single markdown. How about we make this as an option? Support both in salt.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes fair enough, we could also do that.

dirFilePath := filepath.Join(rootOutputPath, cmd.Name())
if len(cmd.Commands()) != 0 {
if _, err := os.Stat(dirFilePath); os.IsNotExist(err) {
if err := os.Mkdir(dirFilePath, os.ModePerm); err != nil {
return err
}
}
for _, subCmd := range cmd.Commands() {
GenerateMarkdownTree(dirFilePath, subCmd)
}
} else {
outFilePath := filepath.Join(rootOutputPath, cmd.Name())
outFilePath = outFilePath + ".md"

f, err := os.Create(outFilePath)
if err != nil {
return err
}

return doc.GenMarkdownCustom(cmd, f, func(s string) string {
return filepath.Join(dirFilePath, s)
})
}

return nil
}
25 changes: 18 additions & 7 deletions cmdx/ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,36 @@ import (
// This should be added on the root command and can
// be used as `help reference` or `reference help`.
func SetRefCmd(root *cobra.Command) *cobra.Command {
var isPlain bool
cmd := &cobra.Command{
Use: "reference",
Short: "Comprehensive reference of all commands",
Long: referenceLong(root),
Run: referenceHelpFn(),
Run: referenceHelpFn(&isPlain),
Annotations: map[string]string{
"group": "help",
},
}
cmd.SetHelpFunc(referenceHelpFn())
cmd.SetHelpFunc(referenceHelpFn(&isPlain))
cmd.Flags().BoolVarP(&isPlain, "plain", "p", true, "output in plain markdown (without ansi color)")
return cmd
}

func referenceHelpFn() func(*cobra.Command, []string) {
func referenceHelpFn(isPlain *bool) func(*cobra.Command, []string) {
return func(cmd *cobra.Command, args []string) {
md, err := printer.Markdown(cmd.Long)
if err != nil {
fmt.Println(err)
return
var (
md string
err error
)

if *isPlain {
md = cmd.Long
} else {
md, err = printer.Markdown(cmd.Long)
if err != nil {
fmt.Println(err)
return
}
}

fmt.Print(md)
Expand Down
9 changes: 7 additions & 2 deletions cmdx/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ import (
// bash, zsh, fish, and powershell. It should be added on the root
// command and can be used as `completion bash` or `completion zsh`.
func SetCompletionCmd(exec string) *cobra.Command {
var execs []interface{}
for i := 0; i < 12; i++ {
execs = append(execs, exec)
}
summary := heredoc.Docf(`To load completions:

`+"```"+`
Bash:

$ source <(%s completion bash)
Expand Down Expand Up @@ -49,7 +53,8 @@ func SetCompletionCmd(exec string) *cobra.Command {
# To load completions for every new session, run:
PS> %s completion powershell > %s.ps1
# and source this file from your PowerShell profile.
`, exec)
`+"```"+`
`, execs...)

return &cobra.Command{
Use: "completion [bash|zsh|fish|powershell]",
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSV
github.com/coreos/pkg v0.0.0-20160727233714-3ac0863d7acf/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.0 h1:EoUDS0afbrsXAZ9YQ9jdu/mZ2sXgT1/2yyNng4PGlyM=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
Expand Down Expand Up @@ -1146,6 +1147,7 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU=
github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfFZQK844Gfx8o5WFuvpxWRwnSoipWe/p622j1v06w=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
Expand All @@ -1165,6 +1167,7 @@ github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9Nz
github.com/shopspring/decimal v0.0.0-20200227202807-02e2044944cc/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.0.4-0.20170822132746-89742aefa4b2/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc=
github.com/sirupsen/logrus v1.0.6/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc=
Expand Down