示例#1
0
// Generate the man page for the given command (alternative name included).
func generateMan(info os.FileInfo, name string) {
	contents, err := ioutil.ReadFile(info.Name())
	if err != nil {
		fmt.Printf("Could not read file: %v\n", err)
		os.Exit(1)
	}
	out := md2man.Render(contents)
	if len(out) == 0 {
		fmt.Println("Failed to render")
		os.Exit(1)
	}
	complete := filepath.Join(m1Dir, name)
	if err := ioutil.WriteFile(complete, out, info.Mode()); err != nil {
		fmt.Printf("Could not write file: %v\n", err)
		os.Exit(1)
	}

	// Check duplicates (e.g. lu and list-updates)
	name = duplicateName(name)
	if name != "" {
		complete = filepath.Join(m1Dir, name)
		if err := ioutil.WriteFile(complete, out, info.Mode()); err != nil {
			fmt.Printf("Could not write file: %v\n", err)
			os.Exit(1)
		}
	}
}
示例#2
0
文件: md2man.go 项目: ringtail/etcd
func main() {
	flag.Parse()

	inFile, err := os.Open(*inFilePath)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	defer inFile.Close()

	doc, err := ioutil.ReadAll(inFile)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	out := md2man.Render(doc)

	outFile, err := os.Create(*outFilePath)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	defer outFile.Close()
	_, err = outFile.Write(out)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}
示例#3
0
func genMarkdown(command *cobra.Command, parent, docsDir string) {
	dparent := strings.Replace(parent, " ", "-", -1)
	name := command.Name()
	dname := name
	cmdName := name
	if len(parent) > 0 {
		dname = dparent + "-" + name
		name = parent + " " + name
		cmdName = parent
	}

	out := new(bytes.Buffer)
	short := command.Short
	long := command.Long
	if len(long) == 0 {
		long = short
	}

	preamble(out, cmdName, name, short, long)
	printOptions(out, command)

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

	if len(command.Commands()) > 0 || len(parent) > 0 {
		fmt.Fprintf(out, "# SEE ALSO\n")
		if len(parent) > 0 {
			fmt.Fprintf(out, "**%s(1)**, ", dparent)
		}
		for _, c := range command.Commands() {
			fmt.Fprintf(out, "**%s-%s(1)**, ", dname, c.Name())
			genMarkdown(c, name, docsDir)
		}
		fmt.Fprintf(out, "\n")
	}

	out.WriteString(`
# HISTORY
June 2016, Ported from the Kubernetes man-doc generator
`)

	final := mangen.Render(out.Bytes())

	filename := docsDir + dname + ".1"
	outFile, err := os.Create(filename)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	defer outFile.Close()
	_, err = outFile.Write(final)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

}
示例#4
0
// GenMan will generate a man page for the given command in the out buffer.
// The header argument may be nil, however obviously out may not.
func (cmd *Command) GenMan(header *GenManHeader, out *bytes.Buffer) {
	if header == nil {
		header = &GenManHeader{}
	}
	buf := genMarkdown(cmd, header)
	final := mangen.Render(buf)
	out.Write(final)
}
示例#5
0
文件: man_docs.go 项目: Tecsisa/dex
// GenMan will generate a man page for the given command in the out buffer.
// The header argument may be nil, however obviously out may not.
func GenMan(cmd *cobra.Command, header *GenManHeader, out io.Writer) {
	if header == nil {
		header = &GenManHeader{}
	}
	buf := genMan(cmd, header)
	final := mangen.Render(buf)
	out.Write(final)
}
示例#6
0
// GenMan will generate a man page for the given command and write it to
// w. The header argument may be nil, however obviously w may not.
func GenMan(cmd *cobra.Command, header *GenManHeader, w io.Writer) error {
	if header == nil {
		header = &GenManHeader{}
	}
	b := genMan(cmd, header)
	final := mangen.Render(b)
	_, err := w.Write(final)
	return err
}
func genMarkdown(command *cobra.Command, parent, docsDir string) {
	dparent := strings.Replace(parent, " ", "-", -1)
	name := command.Name()
	dname := name
	if len(parent) > 0 {
		dname = dparent + "-" + name
		name = parent + " " + name
	}

	out := new(bytes.Buffer)
	short := command.Short
	long := command.Long
	if len(long) == 0 {
		long = short
	}

	preamble(out, name, short, long)
	printOptions(out, command)

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

	if len(command.Commands()) > 0 || len(parent) > 0 {
		fmt.Fprintf(out, "# SEE ALSO\n")
		if len(parent) > 0 {
			fmt.Fprintf(out, "**%s(1)**, ", dparent)
		}
		for _, c := range command.Commands() {
			fmt.Fprintf(out, "**%s-%s(1)**, ", dname, c.Name())
			genMarkdown(c, name, docsDir)
		}
		fmt.Fprintf(out, "\n")
	}

	out.WriteString(`
# HISTORY
January 2015, Originally compiled by Eric Paris (eparis at redhat dot com) based on the kubernetes source material, but hopefully they have been automatically generated since!
`)

	final := mangen.Render(out.Bytes())

	filename := docsDir + dname + ".1"
	outFile, err := os.Create(filename)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	defer outFile.Close()
	_, err = outFile.Write(final)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

}
示例#8
0
文件: man_docs.go 项目: tj/cobra
// GenMan will generate a man page for the given command and write it to
// w. The header argument may be nil, however obviously w may not.
func GenMan(cmd *cobra.Command, header *GenManHeader, w io.Writer) error {
	if header == nil {
		header = &GenManHeader{}
	}
	fillHeader(header, cmd.CommandPath())

	b := genMan(cmd, header)
	_, err := w.Write(mangen.Render(b))
	return err
}
示例#9
0
文件: md2man.go 项目: giantswarm/mayu
func main() {
	var err error
	flag.Parse()

	inFile := os.Stdin
	if *inFilePath != "" {
		inFile, err = os.Open(*inFilePath)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
	}
	defer inFile.Close()

	doc, err := ioutil.ReadAll(inFile)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	out := md2man.Render(doc)

	outFile := os.Stdout
	if *outFilePath != "" {
		outFile, err = os.Create(*outFilePath)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		defer outFile.Close()
	}
	_, err = outFile.Write(out)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}
示例#10
0
func (cmd *Command) GenMan(projectName string, out *bytes.Buffer) {

	buf := genMarkdown(cmd, projectName)
	final := mangen.Render(buf)
	out.Write(final)
}