Skip to content

Commit

Permalink
feat: add clone command (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshmedeski authored Jan 17, 2024
1 parent a63df21 commit 7a32bb7
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 0 deletions.
39 changes: 39 additions & 0 deletions cmds/clone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cmds

import (
"github.com/joshmedeski/sesh/connect"
"github.com/joshmedeski/sesh/git"

"github.com/urfave/cli/v2"
)

func Clone() *cli.Command {
return &cli.Command{
Name: "clone",
Aliases: []string{"cl"},
Usage: "Clone a git repo and connect to it as a session",
UseShortOptionHandling: true,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "cmdDir",
Aliases: []string{"d"},
Usage: "The directory to run the git command in",
},
},
Action: func(cCtx *cli.Context) error {
repo := cCtx.Args().First()
dir := cCtx.Args().Get(1)
cmdDir := cCtx.String("cmdDir")
c, err := git.Clone(git.CloneOptions{
Dir: &dir,
CmdDir: &cmdDir,
Repo: repo,
})
if err != nil {
return cli.Exit(err, 1)
}
connect.Connect(c.Path, false, "")
return nil
},
}
}
50 changes: 50 additions & 0 deletions git/clone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package git

import (
"os/exec"
"regexp"
"strings"
)

type CloneOptions struct {
Dir *string
CmdDir *string
Repo string
}

type ClonedRepo struct {
Name string
Path string
}

func Clone(o CloneOptions) (ClonedRepo, error) {
cmdArgs := []string{"clone", o.Repo}
if o.Dir != nil && strings.TrimSpace(*o.Dir) != "" {
cmdArgs = append(cmdArgs, *o.Dir)
}
cmd := exec.Command("git", cmdArgs...)
if o.CmdDir != nil && strings.TrimSpace(*o.CmdDir) != "" {
cmd.Dir = *o.CmdDir
}
_, err := cmd.Output()
cmd.Wait()
if err != nil {
return ClonedRepo{}, err
}
name := findRepo(o.Repo)
if o.Dir != nil && strings.TrimSpace(*o.Dir) != "" {
name = *o.Dir
}
path := cmd.Dir + "/" + name
return ClonedRepo{
Name: name,
Path: path,
}, nil
}

func findRepo(repo string) string {
repo = strings.TrimSuffix(repo, ".git")
re := regexp.MustCompile(`([^\/]*)$`)
match := re.FindString(repo)
return match
}
23 changes: 23 additions & 0 deletions git/clone_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package git

import (
"strings"
"testing"
)

func TestFindRepo(t *testing.T) {
repos := []string{
"https://github.com/username/repository.git",
"[email protected]:username/repository.git",
"https://github.com/username/repository",
"[email protected]:username/repository",
"username/repository",
}

for _, repo := range repos {
result := strings.TrimSpace(findRepo(repo))
if result != "repository" {
t.Errorf("Expected repository for URL %s, got %s instead", repo, result)
}
}
}
13 changes: 13 additions & 0 deletions git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ import (
"strings"
)

func gitCmd(args []string) ([]byte, error) {
tmux, err := exec.LookPath("git")
if err != nil {
return nil, err
}
cmd := exec.Command(tmux, args...)
output, err := cmd.Output()
if err != nil {
return nil, err
}
return output, nil
}

func RootPath(path string) string {
gitRootPathCmd := exec.Command("git", "-C", path, "rev-parse", "--show-toplevel")
gitRootPathByteOutput, err := gitRootPathCmd.CombinedOutput()
Expand Down
1 change: 1 addition & 0 deletions seshcli/seshcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func App() cli.App {
cmds.List(),
cmds.Choose(),
cmds.Connect(),
cmds.Clone(),
},
}
}

0 comments on commit 7a32bb7

Please sign in to comment.