/
generate_documentation.go
74 lines (69 loc) · 2.16 KB
/
generate_documentation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* *******************************************************************************
* * Copyright (c) 2020 Edgeworx, Inc.
* *
* * This program and the accompanying materials are made available under the
* * terms of the Eclipse Public License v. 2.0 which is available at
* * http://www.eclipse.org/legal/epl-2.0
* *
* * SPDX-License-Identifier: EPL-2.0
* *******************************************************************************
*
*/
package cmd
import (
"fmt"
"os"
"path"
"strings"
"github.com/eclipse-iofog/iofogctl/v3/pkg/util"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
)
func newGenerateDocumentationCommand(rootCmd *cobra.Command) *cobra.Command {
// Find home directory.
home, err := homedir.Dir()
var docDir string
util.Check(err)
cmd := &cobra.Command{
Use: "documentation TYPE",
Hidden: true,
Short: "Generate iofogctl documentation",
Long: "Generate iofogctl documentation as markdown or man page",
Example: `iofogctl documentation md
iofogctl documentation man`,
Args: cobra.ExactValidArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if docDir == "" {
docDir = home + "/.iofog/docs/"
err = os.MkdirAll(docDir, 0755)
util.Check(err)
}
switch t := strings.ToLower(args[0]); t {
case "md":
mdDir := path.Join(docDir, "md/")
err = os.MkdirAll(mdDir, 0755)
util.Check(err)
err = doc.GenMarkdownTree(rootCmd, mdDir)
util.Check(err)
util.PrintSuccess(fmt.Sprintf("markdown documentation generated at %s", mdDir))
case "man":
manDir := path.Join(docDir, "man/")
err = os.MkdirAll(manDir, 0755)
util.Check(err)
header := &doc.GenManHeader{
Title: "iofogctl",
Section: "1",
}
err := doc.GenManTree(rootCmd, header, manDir)
util.Check(err)
util.PrintSuccess(fmt.Sprintf("man documentation generated at %s", manDir))
default:
util.Check(util.NewNotFoundError(fmt.Sprintf("%s documentation format not supported for documentation generation\n Supported types are MAN and MD", t)))
}
},
}
cmd.Flags().StringVarP(&docDir, "output-dir", "o", "", "Output dir path")
return cmd
}