-
Notifications
You must be signed in to change notification settings - Fork 31
/
main.go
75 lines (63 loc) · 1.7 KB
/
main.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
75
package main
import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
"strings"
cmd "github.com/aziontech/azion-cli/pkg/cmd/root"
"github.com/aziontech/azion-cli/pkg/cmdutil"
"github.com/aziontech/azion-cli/pkg/iostreams"
"github.com/spf13/cobra/doc"
"github.com/spf13/pflag"
)
func main() {
if err := run(os.Args); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func run(args []string) error {
flags := pflag.NewFlagSet("", pflag.ContinueOnError)
dir := flags.StringP("doc-path", "", "", "Path directory where you want generate doc files")
filetype := flags.String("file-type", "", "File type for generating the documentation: <yaml|md>")
help := flags.BoolP("help", "h", false, "Displays information about any command")
if err := flags.Parse(args); err != nil {
return err
}
if *help {
fmt.Fprintf(os.Stderr, "Usage of %s:\n\n%s", filepath.Base(args[0]), flags.FlagUsages())
return nil
}
if *dir == "" {
return fmt.Errorf("error: --doc-path not set")
}
rootCmd := cmd.NewCmd(&cmdutil.Factory{
IOStreams: iostreams.System(),
})
rootCmd.InitDefaultHelpCmd()
switch {
case *filetype == "yaml":
if err := os.MkdirAll(*dir, 0755); err != nil {
return err
}
err := doc.GenYamlTree(rootCmd, *dir)
if err != nil {
log.Fatal(err)
}
case *filetype == "md":
if err := os.MkdirAll(*dir, 0755); err != nil {
return err
}
removeMdSuffix := func(s string) string { return strings.TrimRight(s, ".md") }
dontPreprendFile := func(s string) string { return "" }
err := doc.GenMarkdownTreeCustom(rootCmd, *dir, dontPreprendFile, removeMdSuffix)
if err != nil {
log.Fatal(err)
}
default:
log.Fatal(errors.New("You must provide a valid file type"))
}
return nil
}