@@ -189,34 +189,46 @@ func (p *initSubcommand) PostScaffold() error {
189
189
return nil
190
190
}
191
191
192
- // checkDir will return error if the current directory has files which are
193
- // not the go.mod and/or starts with the prefix (.) such as .gitignore.
192
+ // checkDir will return error if the current directory has files which are not allowed.
194
193
// Note that, it is expected that the directory to scaffold the project is cleaned.
195
- // Otherwise, it might face issues to do the scaffold. The go.mod is allowed because user might run
196
- // go mod init before use the plugin it for not be required inform
197
- // the go module via the repo --flag.
194
+ // Otherwise, it might face issues to do the scaffold.
198
195
func checkDir () error {
199
196
err := filepath .Walk ("." ,
200
197
func (path string , info os.FileInfo , err error ) error {
201
198
if err != nil {
202
199
return err
203
200
}
204
- // Allow the whole .git directory tree
201
+ // Allow directory trees starting with '.'
205
202
if info .IsDir () && strings .HasPrefix (info .Name (), "." ) && info .Name () != "." {
206
203
return filepath .SkipDir
207
204
}
208
- // Also allow go.mod and dot-files
209
- if info .Name () != "go.mod" && info .Name () != "go.sum" && ! strings .HasPrefix (info .Name (), "." ) {
210
- return fmt .Errorf (
211
- "target directory is not empty " +
212
- "(only go.mod, go.sum, and files and directories with the prefix \" .\" are allowed); " +
213
- "found existing file %q" ,
214
- path )
205
+ // Allow files starting with '.'
206
+ if strings .HasPrefix (info .Name (), "." ) {
207
+ return nil
208
+ }
209
+ // Allow files in the following list
210
+ allowedFiles := []string {
211
+ "go.mod" , // user might run `go mod init` instead of providing the `--flag` at init
212
+ "go.sum" , // auto-generated file related to go.mod
213
+ "LICENSE" , // can be generated when initializing a GitHub project
214
+ "README.md" , // can be generated when initializing a GitHub project
215
+ }
216
+ for _ , allowedFile := range allowedFiles {
217
+ if info .Name () == allowedFile {
218
+ return nil
219
+ }
215
220
}
216
- return nil
221
+ // Do not allow any other file
222
+ return fmt .Errorf (
223
+ "target directory is not empty (only %s, and files and directories with the prefix \" .\" are " +
224
+ "allowed); found existing file %q" , strings .Join (allowedFiles , ", " ), path )
217
225
})
218
226
if err != nil {
219
227
return err
220
228
}
221
229
return nil
222
230
}
231
+
232
+ // The go.mod is allowed because user might run
233
+ // go mod init before use the plugin it for not be required inform
234
+ // the go module via the repo --flag.
0 commit comments