16
16
idColumnFlag = flag .String ("id-column" , "id" , "Name of the column that identifies a row" )
17
17
orderByFlag = flag .String ("order-by" , "" , "Include ORDER BY in 'SELECT *' statement" )
18
18
noReturningClauseFlag = flag .Bool ("no-returning-clause" , false , "Omit 'RETURNING *' in UPDATE statement" )
19
+ onlyFlag = flag .String ("only" , "" , "Limit output to 'schema' or 'queries'" )
19
20
)
20
21
21
22
var errBadArgument = errors .New ("bad argument" )
@@ -79,6 +80,15 @@ type column struct {
79
80
Constraint string
80
81
}
81
82
83
+ type outputMode uint8
84
+
85
+ const (
86
+ outputSchema outputMode = 1 << iota
87
+ outputQueries
88
+
89
+ outputAll = outputSchema | outputQueries
90
+ )
91
+
82
92
type scaffoldCommandArgs struct {
83
93
Table string
84
94
SingularEntity string
@@ -91,12 +101,14 @@ type scaffoldCommandArgs struct {
91
101
NoExistsClause bool
92
102
OrderBy string
93
103
NoReturningClause bool
104
+ Output outputMode
94
105
}
95
106
96
107
func parseScaffoldCommandArgs (args []string ) (* scaffoldCommandArgs , error ) {
97
108
if len (args ) == 0 {
98
109
return nil , fmt .Errorf ("%w: missing <name> and <column>" , errBadArgument )
99
110
}
111
+
100
112
tableParts := strings .Split (args [0 ], "/" )
101
113
if len (tableParts ) != 2 || len (tableParts [0 ]) == 0 || len (tableParts [1 ]) == 0 {
102
114
return nil , fmt .Errorf ("%w: invalid <name>: '%s', expected '<singular>/<plural>'" , errBadArgument , tableParts )
@@ -110,6 +122,17 @@ func parseScaffoldCommandArgs(args []string) (*scaffoldCommandArgs, error) {
110
122
NoReturningClause : * noReturningClauseFlag ,
111
123
OrderBy : * orderByFlag ,
112
124
}
125
+ switch * onlyFlag {
126
+ case "schema" :
127
+ sca .Output = sca .Output | outputSchema
128
+ case "queries" :
129
+ sca .Output = sca .Output | outputQueries
130
+ case "" :
131
+ sca .Output = sca .Output | outputAll
132
+ default :
133
+ return nil , fmt .Errorf ("%w: '-only %s', expected 'schema' or 'queries'" , errBadArgument , * onlyFlag )
134
+ }
135
+
113
136
for _ , arg := range args [1 :] {
114
137
parts := strings .Split (arg , ":" )
115
138
if len (parts ) < 2 || len (parts ) > 3 {
@@ -142,36 +165,41 @@ func parseScaffoldCommandArgs(args []string) (*scaffoldCommandArgs, error) {
142
165
func scaffoldCommand (args * scaffoldCommandArgs ) error {
143
166
b := & strings.Builder {}
144
167
145
- b .WriteString ("#############################################\n " )
146
- b .WriteString ("# Add the following to your SQL schema file #\n " )
147
- b .WriteString ("#############################################\n \n " )
148
- writeSchema (b , args )
149
- b .WriteString ("\n \n " )
150
-
151
- b .WriteString ("##############################################\n " )
152
- b .WriteString ("# Add the following to your SQL queries file #\n " )
153
- b .WriteString ("##############################################\n \n " )
154
- if args .IDColumn != nil {
155
- writeGetQuery (b , args )
168
+ if args .Output & outputAll == outputAll {
169
+ b .WriteString ("#############################################\n " )
170
+ b .WriteString ("# Add the following to your SQL schema file #\n " )
171
+ b .WriteString ("#############################################\n \n " )
172
+ }
173
+ if args .Output & outputSchema != 0 {
174
+ writeSchema (b , args )
156
175
b .WriteString ("\n \n " )
157
176
}
177
+ if args .Output & outputAll == outputAll {
178
+ b .WriteString ("##############################################\n " )
179
+ b .WriteString ("# Add the following to your SQL queries file #\n " )
180
+ b .WriteString ("##############################################\n \n " )
181
+ }
182
+ if args .Output & outputQueries != 0 {
183
+ if args .IDColumn != nil {
184
+ writeGetQuery (b , args )
185
+ b .WriteString ("\n \n " )
186
+ }
158
187
159
- writeListQuery (b , args )
160
- b .WriteString ("\n \n " )
161
-
162
- writeCreateQuery (b , args )
163
- b .WriteString ("\n " )
188
+ writeListQuery (b , args )
189
+ b .WriteString ("\n \n " )
164
190
165
- if args . IDColumn != nil {
191
+ writeCreateQuery ( b , args )
166
192
b .WriteString ("\n " )
167
- writeDeleteQuery (b , args )
168
- b .WriteString ("\n \n " )
169
- writeUpdateQuery (b , args )
170
- b .WriteString ("\n \n " )
171
- }
172
193
194
+ if args .IDColumn != nil {
195
+ b .WriteString ("\n " )
196
+ writeDeleteQuery (b , args )
197
+ b .WriteString ("\n \n " )
198
+ writeUpdateQuery (b , args )
199
+ b .WriteString ("\n \n " )
200
+ }
201
+ }
173
202
fmt .Print (b )
174
-
175
203
return nil
176
204
}
177
205
0 commit comments