Exemple #1
0
func GenMarkdownTreeCustom(cmd *cobra.Command, dir string, filePrepender, linkHandler func(string) string) error {
	for _, c := range cmd.Commands() {
		if !c.IsAvailableCommand() || c.IsHelpCommand() {
			continue
		}
		if err := GenMarkdownTreeCustom(c, dir, filePrepender, linkHandler); err != nil {
			return err
		}
	}

	basename := strings.Replace(cmd.CommandPath(), " ", "_", -1) + ".md"
	filename := filepath.Join(dir, basename)
	f, err := os.Create(filename)
	if err != nil {
		return err
	}
	defer f.Close()

	if _, err := io.WriteString(f, filePrepender(filename)); err != nil {
		return err
	}
	if err := GenMarkdownCustom(cmd, f, linkHandler); err != nil {
		return err
	}
	return nil
}
Exemple #2
0
func printOptions(w io.Writer, cmd *cobra.Command, name string) error {
	flags := cmd.NonInheritedFlags()
	flags.SetOutput(w)
	if flags.HasFlags() {
		if _, err := fmt.Fprintf(w, "### Options\n\n```\n"); err != nil {
			return err
		}
		flags.PrintDefaults()
		if _, err := fmt.Fprintf(w, "```\n\n"); err != nil {
			return err
		}
	}

	parentFlags := cmd.InheritedFlags()
	parentFlags.SetOutput(w)
	if parentFlags.HasFlags() {
		if _, err := fmt.Fprintf(w, "### Options inherited from parent commands\n\n```\n"); err != nil {
			return err
		}
		parentFlags.PrintDefaults()
		if _, err := fmt.Fprintf(w, "```\n\n"); err != nil {
			return err
		}
	}
	return nil
}
Exemple #3
0
// GenManTree will generate a man page for this command and all decendants
// in the directory given. The header may be nil. This function may not work
// correctly if your command names have - in them. If you have `cmd` with two
// subcmds, `sub` and `sub-third`. And `sub` has a subcommand called `third`
// it is undefined which help output will be in the file `cmd-sub-third.1`.
func GenManTree(cmd *cobra.Command, header *GenManHeader, dir string) error {
	if header == nil {
		header = &GenManHeader{}
	}
	for _, c := range cmd.Commands() {
		if !c.IsAvailableCommand() || c.IsHelpCommand() {
			continue
		}
		if err := GenManTree(c, header, dir); err != nil {
			return err
		}
	}
	needToResetTitle := header.Title == ""

	basename := strings.Replace(cmd.CommandPath(), " ", "_", -1) + ".1"
	filename := filepath.Join(dir, basename)
	f, err := os.Create(filename)
	if err != nil {
		return err
	}
	defer f.Close()

	if err := GenMan(cmd, header, f); err != nil {
		return err
	}

	if needToResetTitle {
		header.Title = ""
	}
	return nil
}
Exemple #4
0
func ListIt(cmd *cobra.Command, args []string) {
	if len(args) != 1 {
		cmd.Help()
		return
	}
	do.Name = args[0]
	err := files.ListFiles(do)
	IfExit(err)
	logger.Println(do.Result)
}
Exemple #5
0
func FilesCat(cmd *cobra.Command, args []string) {
	if len(args) != 1 {
		cmd.Help()
		return
	}
	do.Name = args[0]
	err := files.CatFiles(do)
	IfExit(err)
	log.Warn(do.Result)

}
Exemple #6
0
// Test to see if we have a reason to print See Also information in docs
// Basically this is a test for a parent commend or a subcommand which is
// both not deprecated and not the autogenerated help command.
func hasSeeAlso(cmd *cobra.Command) bool {
	if cmd.HasParent() {
		return true
	}
	for _, c := range cmd.Commands() {
		if !c.IsAvailableCommand() || c.IsHelpCommand() {
			continue
		}
		return true
	}
	return false
}
Exemple #7
0
func PinIt(cmd *cobra.Command, args []string) {
	if do.CSV == "" {
		if len(args) != 1 {
			cmd.Help()
			return
		}
		do.Name = args[0]
	} else {
		do.Name = ""
	}
	err := files.PinFiles(do)
	IfExit(err)
	logger.Println(do.Result)
}
Exemple #8
0
func manPrintOptions(out io.Writer, command *cobra.Command) {
	flags := command.NonInheritedFlags()
	if flags.HasFlags() {
		fmt.Fprintf(out, "# OPTIONS\n")
		manPrintFlags(out, flags)
		fmt.Fprintf(out, "\n")
	}
	flags = command.InheritedFlags()
	if flags.HasFlags() {
		fmt.Fprintf(out, "# OPTIONS INHERITED FROM PARENT COMMANDS\n")
		manPrintFlags(out, flags)
		fmt.Fprintf(out, "\n")
	}
}
Exemple #9
0
//restrict flag behaviour when needed (rare but used sometimes)
func FlagCheck(num int, comp string, cmd *cobra.Command, flags []string) error {
	switch comp {
	case "eq":
		if len(flags) != num {
			cmd.Help()
			return fmt.Errorf("\n**Note** you sent our marmots the wrong number of flags.\nPlease send the marmots %d flags only.", num)
		}
	case "ge":
		if len(flags) < num {
			cmd.Help()
			return fmt.Errorf("\n**Note** you sent our marmots the wrong number of flags.\nPlease send the marmots at least %d flag(s).", num)
		}
	}
	return nil
}
Exemple #10
0
func GenerateTree(cmd *cobra.Command, dir string, specs []string) {
	filePrepender := func(s string) string {
		s = strings.Replace(s, RENDER_DIR, "", 1)
		s = strings.Replace(s, ".md", "", -1)
		s = strings.Replace(s, "_", " ", -1)
		pre := strings.Replace(FRONT_MATTER, "{{}}", s, -1)
		return pre
	}

	linkHandler := func(s string) string {
		s = strings.Replace(s, ".md", "/", -1)
		link := BASE_URL + s
		return link
	}

	for _, c := range cmd.Commands() {
		GenerateTree(c, dir, specs)
	}
	out := new(bytes.Buffer)

	GenerateSingle(cmd, out, linkHandler, specs)

	filename := cmd.CommandPath()
	filename = dir + strings.Replace(filename, " ", "_", -1) + ".md"
	outFile, err := os.Create(filename)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	defer outFile.Close()

	_, err = outFile.WriteString(filePrepender(filename))
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	_, err = outFile.Write(out.Bytes())
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}
Exemple #11
0
// make the genesis files for a chain
//
// MakeChain will assemble the necessary files for a new blockchain. it does not
// actually make the chain or start it (that happens in new), but it does create
// the predicate files for more complex chains than is typically used in default
func MakeChain(cmd *cobra.Command, args []string) {
	IfExit(ArgCheck(1, "ge", cmd, args))
	do.Name = args[0]
	if do.Known && (do.ChainMakeActs == "" || do.ChainMakeVals == "") {
		cmd.Help()
		IfExit(fmt.Errorf("\nIf you are using the --known flag the --validators *and* the --accounts flags are both required."))
	}
	if len(do.AccountTypes) > 0 && do.ChainType != "" {
		cmd.Help()
		IfExit(fmt.Errorf("\nThe --account-types flag is incompatible with the --chain-type flag. Please use one or the other."))
	}
	if (len(do.AccountTypes) > 0 || do.ChainType != "") && do.Known {
		cmd.Help()
		IfExit(fmt.Errorf("\nThe --account-types and --chain-type flags are incompatible with the --known flag. Please use only one of these."))
	}
	IfExit(chns.MakeChain(do))
}
Exemple #12
0
func GenerateSingle(cmd *cobra.Command, out *bytes.Buffer, linkHandler func(string) string, specs []string) {
	name := cmd.CommandPath()

	short := cmd.Short
	long := cmd.Long
	if len(long) == 0 {
		long = short
	}

	fmt.Fprintf(out, "# %s\n\n", name)
	fmt.Fprintf(out, "%s\n\n", short)
	fmt.Fprintf(out, "## Synopsis\n")
	fmt.Fprintf(out, "\n%s\n\n", long)

	if cmd.Runnable() {
		fmt.Fprintf(out, "```bash\n%s\n```\n\n", cmd.UseLine())
	}

	if len(cmd.Example) > 0 {
		fmt.Fprintf(out, "## Examples\n\n")
		fmt.Fprintf(out, "```bash\n%s\n```\n\n", cmd.Example)
	}

	flags := cmd.NonInheritedFlags()
	flags.SetOutput(out)
	if flags.HasFlags() {
		fmt.Fprintf(out, "## Options\n\n```\n")
		flags.PrintDefaults()
		fmt.Fprintf(out, "```\n\n")
	}

	parentFlags := cmd.InheritedFlags()
	parentFlags.SetOutput(out)
	if parentFlags.HasFlags() {
		fmt.Fprintf(out, "## Options inherited from parent commands\n\n```\n")
		parentFlags.PrintDefaults()
		fmt.Fprintf(out, "```\n\n")
	}

	if len(cmd.Commands()) > 0 {
		fmt.Fprintf(out, "## Subcommands\n\n")
		children := cmd.Commands()
		sort.Sort(byName(children))

		for _, child := range children {
			if len(child.Deprecated) > 0 {
				continue
			}
			cname := name + " " + child.Name()
			link := cname + ".md"
			link = strings.Replace(link, " ", "_", -1)
			fmt.Fprintf(out, "* [%s](%s)\t - %s\n", cname, linkHandler(link), child.Short)
		}
	}

	if len(cmd.Commands()) > 0 && cmd.HasParent() {
		fmt.Fprintf(out, "\n")
	}

	if cmd.HasParent() {
		fmt.Fprintf(out, "## See Also\n\n")
		parent := cmd.Parent()
		pname := parent.CommandPath()
		link := pname + ".md"
		link = strings.Replace(link, " ", "_", -1)
		fmt.Fprintf(out, "* [%s](%s)\t - %s\n", pname, linkHandler(link), parent.Short)
	}

	fmt.Fprintf(out, "\n## Specifications\n\n")
	for _, spec := range specs {
		spec = strings.Replace(spec, RENDER_DIR, "", 1)
		title := strings.Replace(spec, "_", " ", -1)
		title = strings.Replace(title, ".md", "", 1)
		// title = strings.Replace(title, "spec", "specification", 1)
		title = strings.Title(title)
		fmt.Fprintf(out, "* [%s](%s)\n", title, linkHandler(spec))
	}

	fmt.Fprintf(out, "\n")
}
Exemple #13
0
//XXX flags were not deduplicated if only one known instance of being used
//this command can probably be refactored...it's quite the bloated
func buildFlag(cmd *cobra.Command, do *definitions.Do, flag, typ string) { //doesn't return anything; just sets the command
	//typ always given but not always needed. also useful for restrictions
	switch flag {
	//listing functions (services, chains)
	case "known":
		cmd.Flags().BoolVarP(&do.Known, "known", "k", false, fmt.Sprintf("list all the %s definition files that exist", typ))
	case "existing":
		cmd.Flags().BoolVarP(&do.Existing, "existing", "e", false, fmt.Sprintf("list all the all current containers which exist for a %s", typ))
	case "running":
		cmd.Flags().BoolVarP(&do.Running, "running", "r", false, fmt.Sprintf("list all the current containers which are running for a %s", typ))
	case "quiet":
		if typ == "action" {
			cmd.Flags().BoolVarP(&do.Quiet, "quiet", "q", false, "suppress action output")
		} else {
			cmd.Flags().BoolVarP(&do.Quiet, "quiet", "q", false, "machine parsable output")
		}

	// stopping (services, chains) & other...eg timeout
	case "force": //collect discrepancies here...
		cmd.Flags().BoolVarP(&do.Force, "force", "f", false, "kill the container instantly without waiting to exit") //why do we even have a timeout??
	case "timeout":
		cmd.Flags().UintVarP(&do.Timeout, "timeout", "t", 10, "manually set the timeout; overridden by --force")
	case "volumes":
		cmd.Flags().BoolVarP(&do.Volumes, "vol", "o", false, "remove volumes")
	case "rm-volumes":
		cmd.Flags().BoolVarP(&do.Volumes, "vol", "o", true, "remove volumes")
	case "rm":
		cmd.Flags().BoolVarP(&do.Rm, "rm", "r", false, "remove containers after stopping")
	case "data":
		if typ == "chain-make" {
			cmd.Flags().BoolVarP(&do.RmD, "data", "x", true, "remove data containers after stopping")
		} else {
			cmd.Flags().BoolVarP(&do.RmD, "data", "x", false, "remove data containers after stopping")
		}
		//exec (services, chains)
	case "publish":
		cmd.PersistentFlags().BoolVarP(&do.Operations.PublishAllPorts, "publish", "p", false, "publish random ports")
	case "interactive":
		cmd.Flags().BoolVarP(&do.Operations.Interactive, "interactive", "i", false, "interactive shell")
		//update
	case "pull":
		cmd.Flags().BoolVarP(&do.Pull, "pull", "p", false, fmt.Sprintf("pull an updated version of the %s's base service image from docker hub", typ))
	case "env":
		cmd.PersistentFlags().StringSliceVarP(&do.Env, "env", "e", nil, "multiple env vars can be passed using the KEY1=val1,KEY2=val2 syntax") //last digit; 1 or 2?
	case "links":
		cmd.PersistentFlags().StringSliceVarP(&do.Links, "links", "l", nil, "multiple containers can be linked can be passed using the KEY1:val1,KEY2:val2 syntax")
		//logs
	case "follow":
		cmd.Flags().BoolVarP(&do.Follow, "follow", "f", false, "follow logs, like tail -f")
	case "tail":
		cmd.Flags().StringVarP(&do.Tail, "tail", "t", "150", "number of lines to show from end of logs")
		//remove
	case "file":
		if typ == "action" {
			cmd.Flags().BoolVarP(&do.File, "file", "f", false, fmt.Sprintf("remove %s definition file", typ))
		} else {
			cmd.Flags().BoolVarP(&do.File, "file", "", false, fmt.Sprintf("remove %s definition file as well as %s container", typ, typ))
		}
	case "chain":
		if typ == "service" {
			cmd.Flags().StringVarP(&do.ChainName, "chain", "c", "", "specify a chain the service depends on")
		} else if typ == "action" {
			cmd.Flags().StringVarP(&do.ChainName, "chain", "c", "", "run action against a particular chain")
		} else {
		}
	case "csv":
		if typ == "chain" {
			cmd.PersistentFlags().StringVarP(&do.CSV, "csv", "", "", "render a genesis.json from a csv file")
		} else if typ == "files" {
			cmd.Flags().StringVarP(&do.CSV, "csv", "", "", "specify a .csv with entries of format: hash,fileName")
		}
	case "services":
		cmd.Flags().StringSliceVarP(&do.ServicesSlice, "services", "s", []string{}, "comma separated list of services to start")
	case "config":
		cmd.PersistentFlags().StringVarP(&do.ConfigFile, "config", "c", "", "main config file (config.toml) for the chain")
	case "serverconf":
		cmd.PersistentFlags().StringVarP(&do.ServerConf, "serverconf", "", "", "pass in a server_conf.toml file")
	case "dir":
		cmd.PersistentFlags().StringVarP(&do.Path, "dir", "", "", "a directory whose contents should be copied into the chain's main dir")
	case "api":
		cmd.PersistentFlags().BoolVarP(&do.Run, "api", "a", true, "turn the chain on using erisdb's api")
	}
}
Exemple #14
0
func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) string) error {
	name := cmd.CommandPath()

	short := cmd.Short
	long := cmd.Long
	if len(long) == 0 {
		long = short
	}

	if _, err := fmt.Fprintf(w, "## %s\n\n", name); err != nil {
		return err
	}
	if _, err := fmt.Fprintf(w, "%s\n\n", short); err != nil {
		return err
	}
	if _, err := fmt.Fprintf(w, "### Synopsis\n\n"); err != nil {
		return err
	}
	if _, err := fmt.Fprintf(w, "\n%s\n\n", long); err != nil {
		return err
	}

	if cmd.Runnable() {
		if _, err := fmt.Fprintf(w, "```\n%s\n```\n\n", cmd.UseLine()); err != nil {
			return err
		}
	}

	if len(cmd.Example) > 0 {
		if _, err := fmt.Fprintf(w, "### Examples\n\n"); err != nil {
			return err
		}
		if _, err := fmt.Fprintf(w, "```\n%s\n```\n\n", cmd.Example); err != nil {
			return err
		}
	}

	if err := printOptions(w, cmd, name); err != nil {
		return err
	}
	if hasSeeAlso(cmd) {
		if _, err := fmt.Fprintf(w, "### SEE ALSO\n"); err != nil {
			return err
		}
		if cmd.HasParent() {
			parent := cmd.Parent()
			pname := parent.CommandPath()
			link := pname + ".md"
			link = strings.Replace(link, " ", "_", -1)
			if _, err := fmt.Fprintf(w, "* [%s](%s)\t - %s\n", pname, linkHandler(link), parent.Short); err != nil {
				return err
			}
			cmd.VisitParents(func(c *cobra.Command) {
				if c.DisableAutoGenTag {
					cmd.DisableAutoGenTag = c.DisableAutoGenTag
				}
			})
		}

		children := cmd.Commands()
		sort.Sort(byName(children))

		for _, child := range children {
			if !child.IsAvailableCommand() || child.IsHelpCommand() {
				continue
			}
			cname := name + " " + child.Name()
			link := cname + ".md"
			link = strings.Replace(link, " ", "_", -1)
			if _, err := fmt.Fprintf(w, "* [%s](%s)\t - %s\n", cname, linkHandler(link), child.Short); err != nil {
				return err
			}
		}
		if _, err := fmt.Fprintf(w, "\n"); err != nil {
			return err
		}
	}
	if !cmd.DisableAutoGenTag {
		if _, err := fmt.Fprintf(w, "###### Auto generated by spf13/cobra on %s\n", time.Now().Format("2-Jan-2006")); err != nil {
			return err
		}
	}
	return nil
}
Exemple #15
0
func genMan(cmd *cobra.Command, header *GenManHeader) []byte {
	// something like `rootcmd subcmd1 subcmd2`
	commandName := cmd.CommandPath()
	// something like `rootcmd-subcmd1-subcmd2`
	dashCommandName := strings.Replace(commandName, " ", "-", -1)

	fillHeader(header, commandName)

	buf := new(bytes.Buffer)

	short := cmd.Short
	long := cmd.Long
	if len(long) == 0 {
		long = short
	}

	manPreamble(buf, header, commandName, short, long)
	manPrintOptions(buf, cmd)
	if len(cmd.Example) > 0 {
		fmt.Fprintf(buf, "# EXAMPLE\n")
		fmt.Fprintf(buf, "```\n%s\n```\n", cmd.Example)
	}
	if hasSeeAlso(cmd) {
		fmt.Fprintf(buf, "# SEE ALSO\n")
		if cmd.HasParent() {
			parentPath := cmd.Parent().CommandPath()
			dashParentPath := strings.Replace(parentPath, " ", "-", -1)
			fmt.Fprintf(buf, "**%s(%s)**", dashParentPath, header.Section)
			cmd.VisitParents(func(c *cobra.Command) {
				if c.DisableAutoGenTag {
					cmd.DisableAutoGenTag = c.DisableAutoGenTag
				}
			})
		}
		children := cmd.Commands()
		sort.Sort(byName(children))
		for i, c := range children {
			if !c.IsAvailableCommand() || c.IsHelpCommand() {
				continue
			}
			if cmd.HasParent() || i > 0 {
				fmt.Fprintf(buf, ", ")
			}
			fmt.Fprintf(buf, "**%s-%s(%s)**", dashCommandName, c.Name(), header.Section)
		}
		fmt.Fprintf(buf, "\n")
	}
	if !cmd.DisableAutoGenTag {
		fmt.Fprintf(buf, "# HISTORY\n%s Auto generated by spf13/cobra\n", header.Date.Format("2-Jan-2006"))
	}
	return buf.Bytes()
}