Skip to content

Commit 9fafbf9

Browse files
committed
Accept auto-generated files from GitHub into the allowed set of files
Made an list that makes it easy to add new files to the allowed list with their justification Signed-off-by: Adrian Orive Oneca <[email protected]>
1 parent 685558d commit 9fafbf9

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

pkg/plugins/golang/v3/init.go

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -189,34 +189,46 @@ func (p *initSubcommand) PostScaffold() error {
189189
return nil
190190
}
191191

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.
194193
// 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.
198195
func checkDir() error {
199196
err := filepath.Walk(".",
200197
func(path string, info os.FileInfo, err error) error {
201198
if err != nil {
202199
return err
203200
}
204-
// Allow the whole .git directory tree
201+
// Allow directory trees starting with '.'
205202
if info.IsDir() && strings.HasPrefix(info.Name(), ".") && info.Name() != "." {
206203
return filepath.SkipDir
207204
}
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+
}
215220
}
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)
217225
})
218226
if err != nil {
219227
return err
220228
}
221229
return nil
222230
}
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

Comments
 (0)