Skip to content

Commit

Permalink
Merge branch 'cmlicata-ignore_nonproblematic_files'
Browse files Browse the repository at this point in the history
  • Loading branch information
inadarei committed Aug 5, 2017
2 parents be88887 + 6be8e32 commit 1198014
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 5 deletions.
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
21 changes: 21 additions & 0 deletions cmd/justgo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## Instructions for developing the CLI Interface

If you are interested in contributing to the development of this CLI tool,
following are the instructions for setting up a dev environment:

## Installation

```BASH
> git clone https://github.com/inadarei/justgo.git
> cd justgo/cmd/justgo
> glide install
> ./go-wrapper install
> ./go-wrapper run <someFolderToTestInstallTo>
```

## Warning for VS Code Users

If you are using VS Code with Go tooling, you will want to change the default
`"go.formatTool": "goreturns",` formatter to `"go.formatTool": "gofmt",` instead since the former
seems unable to properly detect the usage of uuid in the code and keeps removing the uuid package's
import statement from code, making it error-out during a build. Gofmt has no such issues.
2 changes: 2 additions & 0 deletions cmd/justgo/glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ package: github.com/inadarei/justgo/cmd/justgo
import:
- package: github.com/urfave/cli
version: ^1.19.1
- package: github.com/satori/go.uuid
version: ^1.1.0
97 changes: 97 additions & 0 deletions cmd/justgo/go-wrapper
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#!/bin/sh
set -e

usage() {
base="$(basename "$0")"
cat <<EOUSAGE
usage: $base command [args]
This script assumes that is is run from the root of your Go package (for
example, "/go/src/app" if your GOPATH is set to "/go").
In Go 1.4, a feature was introduced to supply the canonical "import path" for a
given package in a comment attached to a package statement
(https://golang.org/s/go14customimport).
This script allows us to take a generic directory of Go source files such as
"/go/src/app" and determine that the canonical "import path" of where that code
expects to live and reference itself is "github.com/jsmith/my-cool-app". It
will then ensure that "/go/src/github.com/jsmith/my-cool-app" is a symlink to
"/go/src/app", which allows us to build and run it under the proper package
name.
For compatibility with versions of Go older than 1.4, the "import path" may also
be placed in a file named ".godir".
Available Commands:
$base download
$base download -u
(equivalent to "go get -d [args] [godir]")
$base install
$base install -race
(equivalent to "go install [args] [godir]")
$base run
$base run -app -specific -arguments
(assumes "GOPATH/bin" is in "PATH")
EOUSAGE
}

# make sure there is a subcommand specified
if [ "$#" -eq 0 ]; then
usage >&2
exit 1
fi
# "shift" so that "$@" becomes the remaining arguments and can be passed along to other "go" subcommands easily
cmd="$1"
shift

goDir="$(go list -e -f '{{.ImportComment}}' 2>/dev/null || true)"

if [ -z "$goDir" -a -s .godir ]; then
goDir="$(cat .godir)"
fi

dir="$(pwd -P)"
if [ "$goDir" ]; then
goPath="${GOPATH%%:*}" # this just grabs the first path listed in GOPATH, if there are multiple (which is the detection logic "go get" itself uses, too)
goDirPath="$goPath/src/$goDir"
mkdir -p "$(dirname "$goDirPath")"
if [ ! -e "$goDirPath" ]; then
ln -sfv "$dir" "$goDirPath"
elif [ ! -L "$goDirPath" ]; then
echo >&2 "error: $goDirPath already exists but is unexpectedly not a symlink!"
exit 1
fi
goBin="$goPath/bin/$(basename "$goDir")"
else
goBin="$(basename "$dir")" # likely "app"
fi

case "$cmd" in
download)
set -- go get -v -d "$@"
if [ "$goDir" ]; then set -- "$@" "$goDir"; fi
set -x; exec "$@"
;;

install)
set -- go install -v "$@"
if [ "$goDir" ]; then set -- "$@" "$goDir"; fi
set -x; exec "$@"
;;

run)
set -x; exec "$goBin" "$@"
;;

*)
echo >&2 'error: unknown command:' "$cmd"
usage >&2
exit 1
;;
esac
78 changes: 73 additions & 5 deletions cmd/justgo/justgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,22 @@ import (
"os"
"path/filepath"

"github.com/satori/go.uuid"
"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 +43,6 @@ func main() {
}

app.Run(os.Args)

}

func buildProject(path string) {
Expand All @@ -51,10 +63,28 @@ func buildProject(path string) {

fileUrl := "https://github.com/inadarei/justgo/archive/master.zip"
tmpFilePath := os.TempDir() + "justgo.zip"
//fmt.Println("Downloading to ", tmpFilePath)
defer os.Remove(tmpFilePath)

// Move all conflicting files to tmp dir and move them back post-build
filesToMove := getConflictingFiles(path)
uniqueToken := uuid.NewV4()
uniqueTempFolder := filepath.Join(os.TempDir(), fmt.Sprintf("%s", uniqueToken))
os.MkdirAll(uniqueTempFolder, os.ModePerm)
defer os.Remove(uniqueTempFolder)

if filesToMove != nil {
for _, file := range filesToMove {
srcPath := filepath.Join(path, file)
tmpMovedFilePath := filepath.Join(uniqueTempFolder, file)
err := os.Rename(srcPath, tmpMovedFilePath)
abortIfErr(err)
defer os.Remove(tmpMovedFilePath)
defer os.Rename(tmpMovedFilePath, srcPath)
}
}

err = downloadFile(tmpFilePath, fileUrl)
abortIfErr(err)
defer os.Remove(tmpFilePath)

err = unzip(tmpFilePath, path, true)
abortIfErr(err)
Expand Down Expand Up @@ -112,10 +142,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 +233,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 1198014

Please sign in to comment.