github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/internal/runners/export/docs/docs.go (about)

     1  package docs
     2  
     3  import (
     4  	_ "embed"
     5  
     6  	"github.com/ActiveState/cli/internal/captain"
     7  	"github.com/ActiveState/cli/internal/errs"
     8  	"github.com/ActiveState/cli/internal/output"
     9  	"github.com/ActiveState/cli/internal/primer"
    10  	"github.com/ActiveState/cli/internal/strutils"
    11  )
    12  
    13  type Docs struct {
    14  	output output.Outputer
    15  }
    16  
    17  type Params struct{}
    18  
    19  type primeable interface {
    20  	primer.Outputer
    21  }
    22  
    23  func New(primer primeable) *Docs {
    24  	return &Docs{primer.Output()}
    25  }
    26  
    27  //go:embed docs.md.tpl
    28  var tpl string
    29  
    30  func (d *Docs) Run(p *Params, cmd *captain.Command) error {
    31  	stateCmd := cmd.TopParent()
    32  	commands := make([][]*captain.Command, 0)
    33  	commands = append(commands, grabChildren(stateCmd))
    34  
    35  	var output string
    36  	for _, cmds := range commands {
    37  		out, err := strutils.ParseTemplate(tpl, map[string]interface{}{
    38  			"Commands": cmds,
    39  		}, nil)
    40  		if err != nil {
    41  			return errs.Wrap(err, "Could not parse template")
    42  		}
    43  		output += out
    44  	}
    45  
    46  	d.output.Print(output)
    47  
    48  	return nil
    49  }
    50  
    51  func grabChildren(cmd *captain.Command) []*captain.Command {
    52  	children := []*captain.Command{}
    53  	for _, child := range cmd.Children() {
    54  		if child.Hidden() {
    55  			continue
    56  		}
    57  		children = append(children, child)
    58  		children = append(children, grabChildren(child)...)
    59  	}
    60  
    61  	return children
    62  }