@@ -29,6 +29,9 @@ func NewGenCmd(app *core.App) *cobra.Command {
29
29
a := & GenAction {
30
30
App : app ,
31
31
}
32
+ if err := defaults .Set (a ); err != nil {
33
+ panic (err )
34
+ }
32
35
33
36
cmd := & cobra.Command {
34
37
Use : "gen" ,
@@ -42,7 +45,8 @@ func NewGenCmd(app *core.App) *cobra.Command {
42
45
flags := cmd .Flags ()
43
46
flags .StringVarP (& a .InPath , "in" , "i" , a .InPath , "file path or dir to one or more JSON schema files" )
44
47
flags .StringVarP (& a .OutDir , "out" , "o" , a .OutDir , "output dir to generate files to" )
45
- flags .StringVarP (& a .TemplatePath , "template" , "t" , a .TemplatePath , "optional template path" )
48
+ flags .StringVarP (& a .OutFile , "outfile" , "f" , a .OutFile , "custom filename pattern for generated files" )
49
+ flags .StringVarP (& a .TemplatePath , "template" , "t" , a .TemplatePath , "custom template path" )
46
50
47
51
return cmd
48
52
}
@@ -52,6 +56,7 @@ type GenAction struct {
52
56
53
57
InPath string `validate:"required"`
54
58
OutDir string `validate:"required" default:"out"`
59
+ OutFile string `validate:"required" default:"{{ .EntityName }}.md"`
55
60
SchemaPaths []string
56
61
TemplatePath string
57
62
}
@@ -93,8 +98,12 @@ func (a *GenAction) Run(_ context.Context, _ []string) error {
93
98
return err
94
99
}
95
100
96
- renderedPath := filepath .Join (a .OutDir , fmt .Sprintf ("%s.md" , scm .EntityName ()))
97
- if err := os .WriteFile (renderedPath , []byte (rendered ), fsutil .DefaultFileMode ); err != nil {
101
+ outFile , err := render .String (a .OutFile , scm )
102
+ if err != nil {
103
+ return err
104
+ }
105
+ outPath := filepath .Join (a .OutDir , outFile )
106
+ if err := os .WriteFile (outPath , []byte (rendered ), fsutil .DefaultFileMode ); err != nil {
98
107
return err
99
108
}
100
109
}
@@ -105,13 +114,11 @@ func (a *GenAction) Run(_ context.Context, _ []string) error {
105
114
func (a * GenAction ) setup () error {
106
115
start := time .Now ()
107
116
108
- if err := defaults .Set (a ); err != nil {
109
- return err
110
- }
111
117
if err := validate .Struct (a ); err != nil {
112
118
msg := err .Error ()
113
119
msg = strings .ReplaceAll (msg , "InPath" , `'--in'` )
114
120
msg = strings .ReplaceAll (msg , "OutDir" , `'--out'` )
121
+ msg = strings .ReplaceAll (msg , "OutFile" , `'--outfile'` )
115
122
msg = strings .ReplaceAll (msg , "field" , "flag" )
116
123
return fmt .Errorf (msg )
117
124
}
@@ -150,6 +157,7 @@ func (a *GenAction) setup() error {
150
157
"duration" , time .Since (start ),
151
158
"in" , a .SchemaPaths ,
152
159
"out" , a .OutDir ,
160
+ "outfile" , a .OutFile ,
153
161
"template" , a .TemplatePath ,
154
162
)
155
163
return nil
0 commit comments