Skip to content

Commit

Permalink
[FEATURE] Default link for open command (#109)
Browse files Browse the repository at this point in the history
* WIP

* Fix unused vars in tests

* Reorganize

* Better doc

* debug

* Move some testing code to a test package

* better error message
  • Loading branch information
pior committed Jun 8, 2018
1 parent 15cdfcd commit c05c4e5
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 56 deletions.
7 changes: 7 additions & 0 deletions pkg/cmd/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ func noArgs(cmd *cobra.Command, args []string) error {
return nil
}

func zeroOrOneArg(cmd *cobra.Command, args []string) error {
if len(args) > 1 {
return fmt.Errorf("expecting zero or one argument")
}
return nil
}

func checkError(err error) {
if err != nil {
exitWithMessage(err.Error())
Expand Down
31 changes: 10 additions & 21 deletions pkg/cmd/open.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,31 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"

"github.com/devbuddy/devbuddy/pkg/helpers"
"github.com/devbuddy/devbuddy/pkg/helpers/open"
"github.com/devbuddy/devbuddy/pkg/project"
)

var openCmd = &cobra.Command{
Use: "open [github|pullrequest]",
Short: "Open a link about your project",
Run: openRun,
Args: onlyOneArg,
Args: zeroOrOneArg,
}

func openRun(cmd *cobra.Command, args []string) {
linkName := ""
if len(args) == 1 {
linkName = args[0]
}

proj, err := project.FindCurrent()
checkError(err)

var url string

switch args[0] {
case "github", "gh":
url, err = helpers.NewGitRepo(proj.Path).BuildGithubProjectURL()
case "pullrequest", "pr":
url, err = helpers.NewGitRepo(proj.Path).BuildGithubPullrequestURL()
default:
url = proj.Manifest.Open[args[0]]
if url != "" {
break
}
err = fmt.Errorf("no link for '%s'", args[0])
}
url, err := open.FindLink(proj, linkName)
checkError(err)
if url != "" {
err = helpers.Open(url)
}

err = open.Open(url)
checkError(err)
}
23 changes: 4 additions & 19 deletions pkg/helpers/git_test.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,18 @@
package helpers

import (
"os/exec"
"testing"

"github.com/Flaque/filet"
"github.com/stretchr/testify/require"
)

func buildGitRepos(t *testing.T, path string) {
init := `
set -ex
git init
git config user.email "you@example.com"
git config user.name "Your Name"
git commit -m Commit1 --allow-empty
git remote add origin git@github.com:org1/repo1.git
`
initFile := filet.TmpFile(t, "", init)
cmd := exec.Command("sh", initFile.Name(), path)
cmd.Dir = path
err := cmd.Run()
require.NoError(t, err, "init failed")
}
"github.com/devbuddy/devbuddy/pkg/test"
)

func TestGitGithubProjectURL(t *testing.T) {
defer filet.CleanUp(t)
tmpdir := filet.TmpDir(t, "")
buildGitRepos(t, tmpdir)
test.GitInit(t, tmpdir)

url, err := NewGitRepo(tmpdir).BuildGithubProjectURL()

Expand All @@ -38,7 +23,7 @@ func TestGitGithubProjectURL(t *testing.T) {
func TestGitGithubPullrequestURL(t *testing.T) {
defer filet.CleanUp(t)
tmpdir := filet.TmpDir(t, "")
buildGitRepos(t, tmpdir)
test.GitInit(t, tmpdir)

url, err := NewGitRepo(tmpdir).BuildGithubPullrequestURL()

Expand Down
16 changes: 0 additions & 16 deletions pkg/helpers/open.go

This file was deleted.

53 changes: 53 additions & 0 deletions pkg/helpers/open/open.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package open

import (
"fmt"
"os/exec"
"runtime"

"github.com/devbuddy/devbuddy/pkg/helpers"
"github.com/devbuddy/devbuddy/pkg/project"
)

// Open a file or URL with the default application, return immediately.
// Use `xdg-open` or `open` depending on the platform.
func Open(location string) error {
openCommand := "xdg-open"
if runtime.GOOS == "darwin" {
openCommand = "open"
}

return exec.Command(openCommand, location).Start()
}

// FindLink returns the url of a link about the project.
// Possible links are github/pullrequest pages and arbitrary links declared in dev.yml. In case of collision, links
// declared in dev.yml have precedence over Github links.
func FindLink(proj *project.Project, linkName string) (url string, err error) {
if linkName == "" {
if len(proj.Manifest.Open) == 1 {
for _, url = range proj.Manifest.Open {
return url, nil
}
}
return "", fmt.Errorf("which link should I open?")
}

url = proj.Manifest.Open[linkName]
if url != "" {
return
}

switch linkName {
case "github", "gh":
url, err = helpers.NewGitRepo(proj.Path).BuildGithubProjectURL()
return
case "pullrequest", "pr":
url, err = helpers.NewGitRepo(proj.Path).BuildGithubPullrequestURL()
return
default:
err = fmt.Errorf("no link for '%s'", linkName)
}

return
}
56 changes: 56 additions & 0 deletions pkg/helpers/open/open_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package open

import (
"testing"

"github.com/Flaque/filet"
"github.com/stretchr/testify/require"

"github.com/devbuddy/devbuddy/pkg/manifest"
"github.com/devbuddy/devbuddy/pkg/project"
"github.com/devbuddy/devbuddy/pkg/test"
)

func TestFindLink(t *testing.T) {
open := map[string]string{"doc": "http://doc.com", "logs": "http://logs"}
proj := &project.Project{Manifest: &manifest.Manifest{Open: open}}

_, err := FindLink(proj, "")
require.Error(t, err)

_, err = FindLink(proj, "unknown")
require.Error(t, err)

url, err := FindLink(proj, "doc")
require.NoError(t, err)
require.Equal(t, "http://doc.com", url)
}

func TestFindLinkDefault(t *testing.T) {
open := map[string]string{"doc": "http://doc.com"}
proj := &project.Project{Manifest: &manifest.Manifest{Open: open}}

url, err := FindLink(proj, "")
require.NoError(t, err)
require.Equal(t, "http://doc.com", url)
}

func TestFindLinkGithub(t *testing.T) {
tmpdir := filet.TmpDir(t, "")
defer filet.CleanUp(t)

test.GitInit(t, tmpdir)
proj := &project.Project{Path: tmpdir, Manifest: &manifest.Manifest{}}

nameToURL := map[string]string{
"pullrequest": "https://github.com/org1/repo1/pull/master?expand=1",
"pr": "https://github.com/org1/repo1/pull/master?expand=1",
"github": "https://github.com/org1/repo1/tree/master",
"gh": "https://github.com/org1/repo1/tree/master",
}
for name, expectedURL := range nameToURL {
url, err := FindLink(proj, name)
require.NoError(t, err)
require.Equal(t, expectedURL, url)
}
}
29 changes: 29 additions & 0 deletions pkg/test/git.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package test

import (
"os/exec"
"testing"

"github.com/stretchr/testify/require"
)

// GitInit initializes a path with a simple Git repo
func GitInit(t *testing.T, path string) {
cmd := exec.Command("git", "init")
cmd.Dir = path
require.NoError(t, cmd.Run())

cmd = exec.Command("git", "commit", "-m", "Commit1", "--allow-empty")
cmd.Dir = path
cmd.Env = []string{
"GIT_COMMITTER_NAME=John",
"GIT_AUTHOR_NAME=John",
"GIT_COMMITTER_EMAIL=john@doo.com",
"GIT_AUTHOR_EMAIL=john@doo.com",
}
require.NoError(t, cmd.Run())

cmd = exec.Command("git", "remote", "add", "origin", "git@github.com:org1/repo1.git")
cmd.Dir = path
require.NoError(t, cmd.Run())
}

0 comments on commit c05c4e5

Please sign in to comment.