-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
188 lines (168 loc) · 5.28 KB
/
config.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"errors"
"flag"
"fmt"
"io"
"os"
"strconv"
"strings"
"github.com/g4s8/envdoc/types"
"github.com/g4s8/envdoc/utils"
)
type Config struct {
// Dir to search for files
Dir string
// FileGlob to filter by file name
FileGlob string
// TypeGlob to filter by type name
TypeGlob string
// OutFile to write the output to
OutFile string
// OutFormat specify the output format
OutFormat types.OutFormat
// EnvPrefix to prefix the env vars with
EnvPrefix string
// NoStyles to disable styles for HTML format
NoStyles bool
// FieldNames flag enables field names usage intead of `env` tag.
FieldNames bool
// TagName sets custom tag name, `env` by default.
TagName string
// TagDefault sets default env tag name, `envDefault` by default.
TagDefault string
// TagRequiredIfNoDef sets attributes as required if no default value is set.
RequiredIfNoDef bool
// ExecLine is the line of go:generate command
ExecLine int
// ExecFile is the file of go:generate command
ExecFile string
// Debug output enabled
Debug bool
}
//nolint:cyclop
func (c *Config) parseFlags(f *flag.FlagSet) error {
// input flags
f.StringVar(&c.Dir, "dir", "", "Dir to search for files, default is the file dir with go:generate command")
f.StringVar(&c.FileGlob, "files", "", "FileGlob to filter by file name")
f.StringVar(&c.TypeGlob, "types", "", "Type glob to filter by type name")
// output flags
f.StringVar(&c.OutFile, "output", "", "Output file path")
f.StringVar((*string)(&c.OutFormat), "format", "markdown", "Output format, default `markdown`")
f.BoolVar(&c.NoStyles, "no-styles", false, "Disable styles for HTML output")
// app config flags
f.StringVar(&c.EnvPrefix, "env-prefix", "", "Environment variable prefix")
f.BoolVar(&c.FieldNames, "field-names", false, "Use field names if tag is not specified")
f.BoolVar(&c.Debug, "debug", false, "Enable debug output")
// customization
f.StringVar(&c.TagName, "tag-name", "env", "Custom tag name")
f.StringVar(&c.TagDefault, "tag-default", "envDefault", "Default tag name")
f.BoolVar(&c.RequiredIfNoDef, "required-if-no-def", false, "Set attributes as required if no default value is set")
// deprecated flags
var (
typeName string
all bool
)
f.StringVar(&typeName, "type", "", "Type name to filter by type name (deprecated: use -types instead)")
f.BoolVar(&all, "all", false, "Generate documentation for all types in the file (deprecated: use -types='*' instead)")
// parse
if err := f.Parse(os.Args[1:]); err != nil {
return fmt.Errorf("parse flags: %w", err)
}
// deprecated flags `all`, `type` and new flag `types` can't be used together
if all && typeName != "" {
return errors.New("flags -all and -type can't be used together")
}
if all && c.TypeGlob != "" {
return errors.New("flags -all and -types can't be used together")
}
if typeName != "" && c.TypeGlob != "" {
return errors.New("flags -type and -types can't be used together")
}
// check for deprecated flags
var deprecatedWarning strings.Builder
if typeName != "" {
deprecatedWarning.WriteString("\t-type flag is deprecated, use -types instead\n")
c.TypeGlob = typeName
}
if all {
deprecatedWarning.WriteString("\t-all flag is deprecated, use -types='*' instead\n")
c.TypeGlob = "*"
}
if deprecatedWarning.Len() > 0 {
fmt.Fprintln(os.Stderr, "WARNING! Deprecated flags are used. It will be removed in the next major release.")
fmt.Fprintln(os.Stderr, deprecatedWarning.String())
}
return nil
}
var ErrNotCalledByGoGenerate = errors.New("not called by go generate")
func (c *Config) parseEnv() error {
inputFileName := os.Getenv("GOFILE")
if inputFileName == "" {
return fmt.Errorf("no exec input file specified: %w", ErrNotCalledByGoGenerate)
}
c.ExecFile = inputFileName
if e := os.Getenv("GOLINE"); e != "" {
i, err := strconv.Atoi(e)
if err != nil {
return fmt.Errorf("invalid exec line number specified: %w", err)
}
c.ExecLine = i
} else {
return fmt.Errorf("no exec line number specified: %w", ErrNotCalledByGoGenerate)
}
if e := os.Getenv("DEBUG"); e != "" {
c.Debug = true
}
return nil
}
func (c *Config) normalize() {
c.TypeGlob = utils.UnescapeGlob(c.TypeGlob)
c.FileGlob = utils.UnescapeGlob(c.FileGlob)
}
func (c *Config) setDefaults() {
if c.FileGlob == "" {
c.FileGlob = c.ExecFile
}
if c.Dir == "" {
c.Dir = "."
}
}
func (c *Config) fprint(out io.Writer) {
fmt.Fprintln(out, "Config:")
fmt.Fprintf(out, " Dir: %q\n", c.Dir)
if c.FileGlob != "" {
fmt.Fprintf(out, " FileGlob: %q\n", c.FileGlob)
}
if c.TypeGlob != "" {
fmt.Fprintf(out, " TypeGlob: %q\n", c.TypeGlob)
}
fmt.Fprintf(out, " OutFile: %q\n", c.OutFile)
fmt.Fprintf(out, " OutFormat: %q\n", c.OutFormat)
if c.EnvPrefix != "" {
fmt.Fprintf(out, " EnvPrefix: %q\n", c.EnvPrefix)
}
if c.NoStyles {
fmt.Fprintln(out, " NoStyles: true")
}
fmt.Printf(" ExecFile: %q\n", c.ExecFile)
fmt.Printf(" ExecLine: %d\n", c.ExecLine)
if c.FieldNames {
fmt.Fprintln(out, " FieldNames: true")
}
if c.Debug {
fmt.Fprintln(out, " Debug: true")
}
}
func (c *Config) Load() error {
fs := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
if err := c.parseFlags(fs); err != nil {
return fmt.Errorf("parse flags: %w", err)
}
if err := c.parseEnv(); err != nil {
return fmt.Errorf("parse env: %w", err)
}
c.setDefaults()
c.normalize()
return nil
}