github.com/zignig/go-ipfs@v0.0.0-20141111235910-c9e5fdf55a52/cmd/ipfs/commands.go (about)

     1  package main
     2  
     3  import (
     4  	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
     5  	u "github.com/jbenet/go-ipfs/util"
     6  	"strings"
     7  	"time"
     8  )
     9  
    10  var cmdIpfsCommands = &commander.Command{
    11  	UsageLine: "commands",
    12  	Short:     "List all available commands.",
    13  	Long: `ipfs commands - List all available commands.
    14  
    15      Lists all available commands (and sub-commands) and exits.
    16    `,
    17  	Run: commandsCmd,
    18  	Subcommands: []*commander.Command{
    19  		cmdIpfsCommandsHelp,
    20  	},
    21  }
    22  
    23  func commandsCmd(c *commander.Command, args []string) error {
    24  	var listCmds func(c *commander.Command)
    25  	listCmds = func(c *commander.Command) {
    26  		u.POut("%s\n", c.FullSpacedName())
    27  		for _, sc := range c.Subcommands {
    28  			listCmds(sc)
    29  		}
    30  	}
    31  
    32  	listCmds(c.Parent)
    33  	return nil
    34  }
    35  
    36  var cmdIpfsCommandsHelp = &commander.Command{
    37  	UsageLine: "help",
    38  	Short:     "List all available commands' help pages.",
    39  	Long: `ipfs commands help - List all available commands's help pages.
    40  
    41      Shows the pages of all available commands (and sub-commands) and exits.
    42      Outputs a markdown document.
    43    `,
    44  	Run: commandsHelpCmd,
    45  }
    46  
    47  func commandsHelpCmd(c *commander.Command, args []string) error {
    48  	u.POut(referenceHeaderMsg)
    49  	u.POut("Generated on %s.\n\n", time.Now().UTC().Format("2006-01-02"))
    50  
    51  	var printCmds func(*commander.Command, int)
    52  	printCmds = func(c *commander.Command, level int) {
    53  		u.POut("%s ", strings.Repeat("#", level))
    54  		u.POut("%s\n\n", c.FullSpacedName())
    55  		u.POut("```\n")
    56  		u.POut("%s\n", c.Long)
    57  		u.POut("```\n\n")
    58  
    59  		for _, sc := range c.Subcommands {
    60  			printCmds(sc, level+1)
    61  		}
    62  	}
    63  
    64  	printCmds(c.Parent.Parent, 1)
    65  	return nil
    66  }
    67  
    68  const referenceHeaderMsg = `
    69  # ipfs command reference
    70  
    71  This document lists every ipfs command (including subcommands), along with
    72  its help page. It can be viewed by running 'ipfs commands help'.
    73  
    74  `