-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
gen.go
134 lines (112 loc) · 3.86 KB
/
gen.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2016 The Cockroach Authors.
//
// 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.
//
// Author: Cuong Do (cdo@cockroachlabs.com)
package cli
import (
"fmt"
"os"
"strings"
"github.com/cockroachdb/cockroach/pkg/build"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
)
var manPath string
var genManCmd = &cobra.Command{
Use: "man",
Short: "generate man pages for CockroachDB",
Long: `This command generates man pages for CockroachDB.
By default, this places man pages into the "man/man1" directory under the current directory.
Use "--path=PATH" to override the output directory. For example, to install man pages globally on
many Unix-like systems, use "--path=/usr/local/share/man/man1".
`,
RunE: MaybeDecorateGRPCError(runGenManCmd),
}
func runGenManCmd(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
return usageAndError(cmd)
}
info := build.GetInfo()
header := &doc.GenManHeader{
Section: "1",
Manual: "CockroachDB Manual",
Source: fmt.Sprintf("CockroachDB %s", info.Tag),
}
if !strings.HasSuffix(manPath, string(os.PathSeparator)) {
manPath += string(os.PathSeparator)
}
if _, err := os.Stat(manPath); err != nil {
if os.IsNotExist(err) {
if err := os.MkdirAll(manPath, 0755); err != nil {
return err
}
} else {
return err
}
}
if err := doc.GenManTree(cmd.Root(), header, manPath); err != nil {
return err
}
// TODO(cdo): The man page generated by the cobra package doesn't include a list of commands, so
// one has to notice the "See Also" section at the bottom of the page to know which commands
// are supported. I'd like to make this better somehow.
fmt.Println("Generated CockroachDB man pages in", manPath)
return nil
}
var autoCompletePath string
var genAutocompleteCmd = &cobra.Command{
Use: "autocomplete",
Short: "generate bash autocompletion script for CockroachDB",
Long: `Generate bash autocompletion script for CockroachDB. This takes an optional parameter
to specify the path for the completion file.
By default, completion file is written to ./cockroach.bash. Use "--out=/path/to/file" to
override the file location.
Note that for the generated file to work on OS X, you'll need to install Homebrew's bash-completion
package (or an equivalent) and follow the post-install instructions.
`,
RunE: MaybeDecorateGRPCError(runGenAutocompleteCmd),
}
func runGenAutocompleteCmd(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
return usageAndError(cmd)
}
if err := cmd.Root().GenBashCompletionFile(autoCompletePath); err != nil {
return err
}
fmt.Println("Generated bash completion file", autoCompletePath)
return nil
}
var genCmd = &cobra.Command{
Use: "gen [command]",
Short: "generate auxiliary files",
Long: "Generate manpages, example shell settings, example databases, etc.",
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Usage()
},
}
var genCmds = []*cobra.Command{
genManCmd,
genAutocompleteCmd,
genExamplesCmd,
genHAProxyCmd,
}
func init() {
genManCmd.PersistentFlags().StringVar(&manPath, "path", "man/man1",
"path where man pages will be outputted")
genAutocompleteCmd.PersistentFlags().StringVar(&autoCompletePath, "out", "cockroach.bash",
"path to generated autocomplete file")
genHAProxyCmd.PersistentFlags().StringVar(&haProxyPath, "out", "haproxy.cfg",
"path to generated haproxy configuration file")
genCmd.AddCommand(genCmds...)
}