-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ignore non-problematic files in destination folder #3
Merged
inadarei
merged 2 commits into
inadarei:master
from
cmlicata:ignore_nonproblematic_files
Aug 5, 2017
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,18 @@ import ( | |
"github.com/urfave/cli" | ||
) | ||
|
||
var nonProblematicFiles = map[string]bool{} | ||
|
||
func init() { | ||
// Initialize map of the non-problematic files to ignore. | ||
// Also, specify whether they will conflict with any files in the zip. | ||
nonProblematicFiles = map[string]bool{ | ||
".git": false, | ||
".gitignore": false, | ||
"README.md": true, | ||
} | ||
} | ||
|
||
func main() { | ||
app := cli.NewApp() | ||
app.Name = "justgo" | ||
|
@@ -30,7 +42,6 @@ func main() { | |
} | ||
|
||
app.Run(os.Args) | ||
|
||
} | ||
|
||
func buildProject(path string) { | ||
|
@@ -51,7 +62,20 @@ func buildProject(path string) { | |
|
||
fileUrl := "https://github.com/inadarei/justgo/archive/master.zip" | ||
tmpFilePath := os.TempDir() + "justgo.zip" | ||
//fmt.Println("Downloading to ", tmpFilePath) | ||
|
||
// Move all conflicting files to tmp dir and move them back post-build | ||
filesToMove := getConflictingFiles(path) | ||
if filesToMove != nil { | ||
for _, file := range filesToMove { | ||
srcPath := filepath.Join(path, file) | ||
tmpFilePath := filepath.Join(os.TempDir(), file) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By naming this variable tmpFilePath (which was already used for a different purpose on line 64) you are causing confusion and possibility of conflict. |
||
err := os.Rename(srcPath, tmpFilePath) | ||
abortIfErr(err) | ||
defer os.Remove(tmpFilePath) | ||
defer os.Rename(tmpFilePath, srcPath) | ||
} | ||
} | ||
|
||
err = downloadFile(tmpFilePath, fileUrl) | ||
abortIfErr(err) | ||
defer os.Remove(tmpFilePath) | ||
|
@@ -112,10 +136,13 @@ func folderIsEmpty(path string) bool { | |
} | ||
defer f.Close() | ||
|
||
_, err = f.Readdirnames(1) | ||
if err == io.EOF { | ||
filenames, err := f.Readdirnames(0) | ||
abortIfErr(err) | ||
|
||
if !containsProblematicFiles(filenames) { | ||
return true | ||
} | ||
|
||
// If not already exited, scanning children must have errored-out | ||
abortIfErr(err) | ||
return false | ||
|
@@ -200,3 +227,38 @@ func unzip(archive, target string, skipTop bool) error { | |
|
||
return nil | ||
} | ||
|
||
// Check whether folder contains any files other than those specified as non-problematic. | ||
func containsProblematicFiles(filesInDir []string) bool { | ||
if len(filesInDir) > len(nonProblematicFiles) { | ||
return true | ||
} | ||
|
||
// check if any files in the folder are considered to be problematic | ||
for _, filename := range filesInDir { | ||
|
||
// Is the file non-problematic? | ||
_, exists := nonProblematicFiles[filename] | ||
|
||
if !exists { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// Get Non-Problematic files in the 'target' folder that conflict with others in the zip. | ||
func getConflictingFiles(path string) []string { | ||
var filesWithConflicts []string | ||
|
||
for filename, hasConflict := range nonProblematicFiles { | ||
|
||
exists, err := pathExists(filepath.Join(path, filename)) | ||
abortIfErr(err) | ||
|
||
if exists && hasConflict == true { | ||
filesWithConflicts = append(filesWithConflicts, filename) | ||
} | ||
} | ||
return filesWithConflicts | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By using os.TempDir() directly, you will destroy any pre-existing files there which may have the same name(s) as the files you are stashing away. In extreme scenarios - you may run into even more severe conflicting cases.
Rather, let's make sure we create a unique folder under os.TempDir() and do our dirty work there.