From 64b41026966f7da3024294c35b4d5afcb90a74df Mon Sep 17 00:00:00 2001 From: KyleBanks Date: Wed, 22 Mar 2017 19:32:31 -0400 Subject: [PATCH] Default to ~/go when gopath is unavailable, attempt to resolve cp bug in makefile (#22) --- Makefile | 26 ++++++++++++++++++++------ README.md | 10 +++++++++- cmd/goggles/goggles.go | 2 ++ pkg/sys/runner.go | 6 +++--- pkg/sys/runner_test.go | 6 ++++-- pkg/sys/sys.go | 12 ++++++++++-- pkg/sys/sys_test.go | 23 +++++++++++++++-------- 7 files changed, 63 insertions(+), 22 deletions(-) diff --git a/Makefile b/Makefile index cc3fd7b..34ce700 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,16 @@ -VERSION = 0.1.0 +VERSION = 0.1.1 INSTALL_PKG = ./cmd/goggles -APP_FOLDER = ./bin/goggles.app +BIN = ./bin +APP_NAME = Goggles.app +APP_FOLDER = $(BIN)/$(APP_NAME) APP_STATIC_FOLDER = $(APP_FOLDER)/Contents/MacOS/static LOG_FILE = ~/Library/Logs/goggles.log +BUNDLE_ID = com.kylewbanks.goggles +BUNDLE_NAME = Goggles + # Runs goggles and opens the logs. # # This is the default command. @@ -22,7 +27,7 @@ gulp: # Cleans any built artifacts. clean: - @rm -rf $(APP_FOLDER) + @rm -rf $(BIN) @rm -f $(LOG_FILE) .PHONY: clean @@ -30,15 +35,24 @@ clean: build: | clean gulp @mkdir -p bin/ @go build -v -o bin/goggles $(INSTALL_PKG) - @gallium-bundle bin/goggles --output $(APP_FOLDER) + @gallium-bundle bin/goggles \ + --output $(APP_FOLDER) \ + --identifier $(BUNDLE_ID) \ + --name $(BUNDLE_NAME) @mkdir -p $(APP_STATIC_FOLDER) - @cp -r ./_static/ $(APP_STATIC_FOLDER) + @cp -a ./_static/. $(APP_STATIC_FOLDER) @rm -rf $(APP_STATIC_FOLDER)/node_modules .PHONY: build +# Builds a release bundle. +release: | build + @cd $(BIN) ; \ + zip -r -y goggles.$(VERSION).zip $(APP_NAME) +.PHONY: release + # Runs the goggles application. run.goggles: | build - @pkill goggles || true + @pkill Goggles || true @open $(APP_FOLDER) .PHONY: run diff --git a/README.md b/README.md index 01435f5..07a65c5 100644 --- a/README.md +++ b/README.md @@ -9,13 +9,21 @@ ![Goggles Demo](./demo.gif) +## Features + +- Browse and search local packages +- View package documentation +- Open the project folder in Finder or Terminal +- Open the project repository in your browser +- Displays badges for GoDoc, Goreportcard, and Travis.CI (if .travis.yml is present) + ## Install ### Stable Grab the latest release from the [Releases](https://github.com/KyleBanks/goggles/releases) page. -**Note:** It is currently a known issue that Goggles can only be run via the command-line with `open Goggles.app`, simply double-clicking `Goggles.app` has issues finding your $GOPATH. +**Note:** If you have a custom `$GOPATH` it's currently a known issue that Goggles must be run via the command-line with `open Goggles.app`, simply double-clicking `Goggles.app` will only work with the default of `$HOME/go`. ### From Source diff --git a/cmd/goggles/goggles.go b/cmd/goggles/goggles.go index 463dff4..1f0b419 100644 --- a/cmd/goggles/goggles.go +++ b/cmd/goggles/goggles.go @@ -33,6 +33,8 @@ func init() { } func startServer() { + log.Printf("$GOPATH=%v, srcdir=%v", sys.Gopath(), sys.Srcdir()) + p := provider{goggles.Service{}} api := server.New(p, filepath.Dir(os.Args[0])) addr := fmt.Sprintf(":%v", port) diff --git a/pkg/sys/runner.go b/pkg/sys/runner.go index a96fdbf..0d3cb6c 100644 --- a/pkg/sys/runner.go +++ b/pkg/sys/runner.go @@ -9,13 +9,13 @@ var DefaultRunner Runner = CmdRunner{} // Runner defines a type that can run system commands. type Runner interface { - Run(string, ...string) error + Run(string, ...string) ([]byte, error) } // CmdRunner runs system commands. type CmdRunner struct{} // Run executes a system command. -func (CmdRunner) Run(cmd string, args ...string) error { - return exec.Command(cmd, args...).Run() +func (CmdRunner) Run(cmd string, args ...string) ([]byte, error) { + return exec.Command(cmd, args...).CombinedOutput() } diff --git a/pkg/sys/runner_test.go b/pkg/sys/runner_test.go index eab7000..ae61721 100644 --- a/pkg/sys/runner_test.go +++ b/pkg/sys/runner_test.go @@ -8,14 +8,16 @@ func TestCmdRunner_Run(t *testing.T) { var r CmdRunner // Expect an error - err := r.Run("fake command", "this", "will", "fail") + _, err := r.Run("fake command", "this", "will", "fail") if err == nil { t.Fatal("Expected error, got nil") } // Expect success - err = r.Run("echo", "hi") + out, err := r.Run("echo", "hi") if err != nil { t.Fatal(err) + } else if string(out) != "hi\n" { + t.Fatalf("Unexpected output, expected=%v, got=%v", "hi", string(out)) } } diff --git a/pkg/sys/sys.go b/pkg/sys/sys.go index 72af90a..ba8f76f 100644 --- a/pkg/sys/sys.go +++ b/pkg/sys/sys.go @@ -15,6 +15,8 @@ var ( cmdOpenFileExplorer = []string{"open"} cmdOpenTerminal = []string{"open", "-a", "Terminal"} cmdOpenBrowser = []string{"open"} + + defaultGoPath = os.ExpandEnv("$HOME/go") ) // OpenFileExplorer opens the system file explorer application to the @@ -49,7 +51,13 @@ func Srcdir() string { return filepath.Join(Gopath(), srcDirName) } -// Gopath returns the $GOPATH environment variable. +// Gopath returns the $GOPATH environment variable, defaulting to $HOME/go +// if the environment variable is not set. func Gopath() string { - return os.Getenv(gopathEnv) + gopath := os.Getenv(gopathEnv) + if len(gopath) == 0 { + gopath = defaultGoPath + } + + return gopath } diff --git a/pkg/sys/sys_test.go b/pkg/sys/sys_test.go index 71061b3..85cfe77 100644 --- a/pkg/sys/sys_test.go +++ b/pkg/sys/sys_test.go @@ -7,10 +7,10 @@ import ( ) type mockRunner struct { - runFn func(string, ...string) error + runFn func(string, ...string) ([]byte, error) } -func (m mockRunner) Run(cmd string, args ...string) error { return m.runFn(cmd, args...) } +func (m mockRunner) Run(cmd string, args ...string) ([]byte, error) { return m.runFn(cmd, args...) } func Test_OpenFileExplorer(t *testing.T) { expect := []string{"/foo/bar/gopath", "src", "github.com/foo/bar"} @@ -19,10 +19,10 @@ func Test_OpenFileExplorer(t *testing.T) { var gotCmd string var gotPath string DefaultRunner = mockRunner{ - runFn: func(cmd string, args ...string) error { + runFn: func(cmd string, args ...string) ([]byte, error) { gotCmd = cmd gotPath = args[0] - return nil + return nil, nil }, } @@ -42,10 +42,10 @@ func Test_OpenTerminal(t *testing.T) { var gotCmd string var gotArgs []string DefaultRunner = mockRunner{ - runFn: func(cmd string, args ...string) error { + runFn: func(cmd string, args ...string) ([]byte, error) { gotCmd = cmd gotArgs = args - return nil + return nil, nil }, } @@ -72,10 +72,10 @@ func Test_OpenBrowser(t *testing.T) { var gotCmd string var gotURL string DefaultRunner = mockRunner{ - runFn: func(cmd string, args ...string) error { + runFn: func(cmd string, args ...string) ([]byte, error) { gotCmd = cmd gotURL = args[0] - return nil + return nil, nil }, } @@ -108,9 +108,16 @@ func Test_Srcdir(t *testing.T) { } func Test_Gopath(t *testing.T) { + // GOPATH avaiable expect := "/foo/bar/path" os.Setenv("GOPATH", expect) + if Gopath() != expect { + t.Fatalf("Unexpected Gopath, expected=%v, got=%v", expect, Gopath()) + } + // Default + expect = defaultGoPath + os.Setenv("GOPATH", "") if Gopath() != expect { t.Fatalf("Unexpected Gopath, expected=%v, got=%v", expect, Gopath()) }