forked from rayjohnson/cobraman
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tool.go
94 lines (79 loc) · 3.07 KB
/
tool.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright © 2018 Ray Johnson <[email protected]>.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cobraman
import (
"path/filepath"
"github.com/spf13/cobra"
)
// DocGenTool is an opaque type created by CreateDocGenCmdLineTool.
type DocGenTool struct {
installDirectory string
docCmd *cobra.Command
appCmd *cobra.Command
}
// CreateDocGenCmdLineTool creates a command line parser that can be used
// in a utility tool to generate documentation for a companion application.
func CreateDocGenCmdLineTool(appCmd *cobra.Command) *DocGenTool {
dg := &DocGenTool{
appCmd: appCmd,
}
dg.docCmd = &cobra.Command{
Use: "doc",
Args: cobra.NoArgs,
Short: "Generate documentation, etc.",
}
dg.docCmd.PersistentFlags().StringVar(&dg.installDirectory, "directory", ".", "Directory to install generated files")
return dg
}
// AddBashCompletionGenerator will create a subcommand for the utility tool
// that will generate a Bash Completion file for the companion app. It will
// support a --directory flag and use the fileName passed into this function.
func (dg *DocGenTool) AddBashCompletionGenerator(fileName string) *DocGenTool {
completeCmd := &cobra.Command{
Use: "generate-auto-complete",
Args: cobra.NoArgs,
Short: "Generate bash auto complete script",
RunE: func(myCmd *cobra.Command, args []string) error {
path := filepath.Join(dg.installDirectory, fileName)
return dg.appCmd.GenBashCompletionFile(path)
},
}
dg.docCmd.AddCommand(completeCmd)
return dg
}
// AddDocGenerator will create a subcommand for the utility tool that will
// generate documentation with the passed in Options and templateName.
// It supports a --directory flag for where to place the generated files. The
// subcommand will be named generate-<templateName> where templateName is the
// same as the template used to generate the documentation.
func (dg *DocGenTool) AddDocGenerator(opts *Options, templateName string) *DocGenTool {
// Make sure template exists or we will later get runtime panic
_, ok := templateMap[templateName]
if !ok {
panic("the given template has not been registered: " + templateName)
}
genCmd := &cobra.Command{
Use: "generate-" + templateName,
Args: cobra.NoArgs,
Short: "Generate docs with the " + templateName + " template",
RunE: func(myCmd *cobra.Command, args []string) error {
return GenerateDocs(dg.appCmd, opts, dg.installDirectory, templateName)
},
}
dg.docCmd.AddCommand(genCmd)
return dg
}
// Execute will parse args and execute the command line.
func (dg *DocGenTool) Execute() error {
return dg.docCmd.Execute()
}