in cmd/gomodgen/main.go [134:183]
func prepareTargetDirectory(path string, buildManifest manifest.Manifest) error {
if _, err := os.Stat(path); err != nil && os.IsNotExist(err) {
return os.MkdirAll(path, 0755)
} else if err != nil {
return err
}
targetManifest, err := manifest.LoadManifest(filepath.Join(path, manifestFileName))
var notFoundErr *manifest.NoManifestFound
if err != nil && !errors.As(err, ¬FoundErr) {
return err
}
var (
targetModule string
cleanupList []string
)
if err == nil {
targetModule = targetManifest.Module
cleanupList = targetManifest.Files
} else {
log.Printf("[WARN] target directory %v is missing generated.json, will only remove files present in build artifact", path)
if ok, err := gomod.IsGoModPresent(path); err != nil {
return err
} else if ok {
moduleFile, err := gomod.LoadModuleFile(path, nil, true)
if err != nil {
return err
}
targetModule, err = gomod.GetModulePath(moduleFile)
if err != nil {
return err
}
if targetModule != buildManifest.Module {
return fmt.Errorf("target module %v does not match build artifact %v", targetModule, buildManifest.Module)
}
}
cleanupList = buildManifest.Files
}
for _, fileName := range cleanupList {
filePath := filepath.Join(path, fileName)
if err := os.Remove(filePath); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("failed to remove %v: %w", filePath, err)
}
}
return nil
}