-
Notifications
You must be signed in to change notification settings - Fork 175
/
options.go
60 lines (48 loc) · 1.78 KB
/
options.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
package generateapireference
import (
"fmt"
"os"
"github.com/spf13/cobra"
apierrors "k8s.io/apimachinery/pkg/util/errors"
)
type GenerateAPIRefsOptions struct {
CustomResourceDefinitionPaths []string
TemplatesDir string
OutputDir string
Overwrite bool
}
func NewGenerateAPIRefsOptions() *GenerateAPIRefsOptions {
return &GenerateAPIRefsOptions{
CustomResourceDefinitionPaths: nil,
OutputDir: "",
}
}
func (o *GenerateAPIRefsOptions) AddFlags(cmd *cobra.Command) {
cmd.PersistentFlags().StringVarP(&o.TemplatesDir, "templates-dir", "", o.TemplatesDir, "A directory containing docs templates.")
cmd.PersistentFlags().StringVarP(&o.OutputDir, "output-dir", "", o.OutputDir, "A directory where the generated files should be stored.")
cmd.PersistentFlags().BoolVarP(&o.Overwrite, "overwrite", "", o.Overwrite, "Allows writing to output dir that already contains data. Existing files will be overwritten.")
}
func (o *GenerateAPIRefsOptions) Validate(args []string) error {
var errs []error
if len(args) == 0 {
errs = append(errs, fmt.Errorf("at least one CRD has to be specified"))
}
if len(o.TemplatesDir) == 0 {
errs = append(errs, fmt.Errorf("templates-dir path can't be empty"))
}
if len(o.OutputDir) > 0 {
files, err := os.ReadDir(o.OutputDir)
if err == nil {
if len(files) > 0 && !o.Overwrite {
errs = append(errs, fmt.Errorf("output directory %q is not empty and overwrite isn't enabled", o.OutputDir))
}
} else if !os.IsNotExist(err) {
errs = append(errs, fmt.Errorf("can't read output-dir %q: %w", o.OutputDir, err))
}
}
return apierrors.NewAggregate(errs)
}
func (o *GenerateAPIRefsOptions) Complete(args []string) error {
o.CustomResourceDefinitionPaths = args
return nil
}