Skip to content

Commit

Permalink
ignore non-problematic files in the destination folder
Browse files Browse the repository at this point in the history
  • Loading branch information
cmlicata committed Apr 26, 2017
1 parent 7aa9480 commit 372add3
Showing 1 changed file with 66 additions and 4 deletions.
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)
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
}

0 comments on commit 372add3

Please sign in to comment.