Skip to content

Commit

Permalink
Add expand command
Browse files Browse the repository at this point in the history
  • Loading branch information
mattn committed Dec 24, 2019
1 parent bb059e4 commit 0ab7f95
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
34 changes: 34 additions & 0 deletions cmd_expand_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"

"github.com/Songmu/gitconfig"
"github.com/motemen/ghq/cmdutil"
)

func TestDoExpand(t *testing.T) {
defer func(orig func(cmd *exec.Cmd) error) {
cmdutil.CommandRunner = orig
}(cmdutil.CommandRunner)
defer func(orig string) { _home = orig }(_home)
defer gitconfig.WithConfig(t, "")()
tmpd := newTempDir(t)
defer os.RemoveAll(tmpd)
defer func(orig []string) { _localRepositoryRoots = orig }(_localRepositoryRoots)
_localRepositoryRoots = []string{tmpd}

out, _, _ := capture(func() {
newApp().Run([]string{"", "expand", "motemen/ghqq"})
})
out = strings.TrimSpace(out)
wantDir := filepath.Join(tmpd, "github.com/motemen/ghqq")

if out != wantDir {
t.Errorf("cmd.Dir = %q, want: %q", out, wantDir)
}
}
27 changes: 27 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

var commands = []cli.Command{
commandGet,
commandExpand,
commandList,
commandLook,
commandImport,
Expand Down Expand Up @@ -109,6 +110,13 @@ var commandDocs = map[string]commandDoc{
"create": {"", "<project> | <user>/<project> | <host>/<user>/<project>"},
}

var commandExpand = cli.Command{
Name: "expand",
Usage: "Expand to relative path from root root",
Action: doExpand,
Flags: []cli.Flag{},
}

// Makes template conditionals to generate per-command documents.
func mkCommandsTemplate(genTemplate func(commandDoc) string) string {
template := "{{if false}}"
Expand All @@ -135,3 +143,22 @@ OPTIONS:
{{end}}
{{end}}`
}

func doExpand(c *cli.Context) error {
var (
name = c.Args().First()
w = c.App.Writer
)
u, err := newURL(name, false, true)
if err != nil {
return err
}

localRepo, err := LocalRepositoryFromURL(u)
if err != nil {
return err
}

_, err = fmt.Fprintln(w, localRepo.FullPath)
return err
}

0 comments on commit 0ab7f95

Please sign in to comment.