From 7aa9480a75258dc798cbae90ae5d365a9b6e3b93 Mon Sep 17 00:00:00 2001 From: Christopher Licata Date: Sun, 23 Apr 2017 19:20:06 -0400 Subject: [PATCH 1/3] add .idea/ to .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index f95484a..44b0dea 100644 --- a/.gitignore +++ b/.gitignore @@ -28,6 +28,7 @@ src gin-bin .godeps .DS_Store +.idea/ # You can/should uncomment these if you like checking-in dependencies From 372add32e2e213bbbb13083c6ec06827bf398f21 Mon Sep 17 00:00:00 2001 From: Christopher Licata Date: Tue, 25 Apr 2017 23:41:33 -0400 Subject: [PATCH 2/3] ignore non-problematic files in the destination folder --- cmd/justgo/justgo.go | 70 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 66 insertions(+), 4 deletions(-) diff --git a/cmd/justgo/justgo.go b/cmd/justgo/justgo.go index 9b76d28..88e089c 100644 --- a/cmd/justgo/justgo.go +++ b/cmd/justgo/justgo.go @@ -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) + 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 +} \ No newline at end of file From 6be8e328bad746de39b9004977486b0d1a3f5bbd Mon Sep 17 00:00:00 2001 From: Irakli Nadareishvili Date: Sun, 6 Aug 2017 01:02:01 +0400 Subject: [PATCH 3/3] making couple of improvements to Chris' PR, as commented in the PR --- cmd/justgo/README.md | 21 ++++++++++ cmd/justgo/glide.yaml | 2 + cmd/justgo/go-wrapper | 97 +++++++++++++++++++++++++++++++++++++++++++ cmd/justgo/justgo.go | 18 +++++--- 4 files changed, 132 insertions(+), 6 deletions(-) create mode 100644 cmd/justgo/README.md create mode 100755 cmd/justgo/go-wrapper diff --git a/cmd/justgo/README.md b/cmd/justgo/README.md new file mode 100644 index 0000000..4510589 --- /dev/null +++ b/cmd/justgo/README.md @@ -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 +``` + +## 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. \ No newline at end of file diff --git a/cmd/justgo/glide.yaml b/cmd/justgo/glide.yaml index b21ff57..554233f 100644 --- a/cmd/justgo/glide.yaml +++ b/cmd/justgo/glide.yaml @@ -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 diff --git a/cmd/justgo/go-wrapper b/cmd/justgo/go-wrapper new file mode 100755 index 0000000..4de37ea --- /dev/null +++ b/cmd/justgo/go-wrapper @@ -0,0 +1,97 @@ +#!/bin/sh +set -e + +usage() { + base="$(basename "$0")" + cat <&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 \ No newline at end of file diff --git a/cmd/justgo/justgo.go b/cmd/justgo/justgo.go index 88e089c..00fe354 100644 --- a/cmd/justgo/justgo.go +++ b/cmd/justgo/justgo.go @@ -9,6 +9,7 @@ import ( "os" "path/filepath" + "github.com/satori/go.uuid" "github.com/urfave/cli" ) @@ -62,23 +63,28 @@ func buildProject(path string) { fileUrl := "https://github.com/inadarei/justgo/archive/master.zip" tmpFilePath := os.TempDir() + "justgo.zip" + 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) - tmpFilePath := filepath.Join(os.TempDir(), file) - err := os.Rename(srcPath, tmpFilePath) + tmpMovedFilePath := filepath.Join(uniqueTempFolder, file) + err := os.Rename(srcPath, tmpMovedFilePath) abortIfErr(err) - defer os.Remove(tmpFilePath) - defer os.Rename(tmpFilePath, srcPath) + 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) @@ -261,4 +267,4 @@ func getConflictingFiles(path string) []string { } } return filesWithConflicts -} \ No newline at end of file +}