-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Default link for open command (#109)
* WIP * Fix unused vars in tests * Reorganize * Better doc * debug * Move some testing code to a test package * better error message
- Loading branch information
Showing
7 changed files
with
159 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |