Skip to content
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
merged 2 commits into from
Aug 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ src
gin-bin
.godeps
.DS_Store
.idea/


# You can/should uncomment these if you like checking-in dependencies
Expand Down
70 changes: 66 additions & 4 deletions cmd/justgo/justgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -30,7 +42,6 @@ func main() {
}

app.Run(os.Args)

}

func buildProject(path string) {
Expand All @@ -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)
Copy link
Owner

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.

Copy link
Owner

Choose a reason for hiding this comment

The 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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}