Skip to content

Commit 023e772

Browse files
authored
feat: reimplement clone command (#197)
1 parent 4c71111 commit 023e772

File tree

8 files changed

+161
-54
lines changed

8 files changed

+161
-54
lines changed

cloner/clone_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package cloner
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/joshmedeski/sesh/model"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestListSessions(t *testing.T) {
12+
t.Run("get path with both cmd and repo", func(t *testing.T) {
13+
mockOpts := model.GitCloneOptions{Repo: "https://www.github.comtest/repo.git", CmdDir: "cmdDir", Dir: "dir"}
14+
actual := getPath(mockOpts)
15+
assert.Equal(t, "cmdDir/dir", actual)
16+
})
17+
t.Run("get path cmdDir", func(t *testing.T) {
18+
mockOpts := model.GitCloneOptions{Repo: "https://www.github.comtest/repo.git", CmdDir: "cmdDir"}
19+
actual := getPath(mockOpts)
20+
assert.Equal(t, "cmdDir/repo", actual)
21+
})
22+
t.Run("get path dir", func(t *testing.T) {
23+
mockOpts := model.GitCloneOptions{Repo: "https://www.github.comtest/repo.git", Dir: "dir"}
24+
actual := getPath(mockOpts)
25+
expected, _ := os.Getwd()
26+
expected = expected + "/" + "dir"
27+
assert.Equal(t, expected, actual)
28+
})
29+
t.Run("get path with no dir or cmdDir", func(t *testing.T) {
30+
mockOpts := model.GitCloneOptions{Repo: "https://www.github.comtest/repo.git"}
31+
actual := getPath(mockOpts)
32+
expected, _ := os.Getwd()
33+
expected = expected + "/" + "repo"
34+
assert.Equal(t, expected, actual)
35+
})
36+
}

cloner/cloner.go

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package cloner
22

33
import (
4+
"os"
5+
"strings"
6+
47
"github.com/joshmedeski/sesh/connector"
58
"github.com/joshmedeski/sesh/git"
9+
"github.com/joshmedeski/sesh/model"
610
)
711

812
type Cloner interface {
913
// Clones a git repository
10-
Clone(path string) (string, error)
14+
Clone(opts model.GitCloneOptions) (string, error)
1115
}
1216

1317
type RealCloner struct {
@@ -22,9 +26,42 @@ func NewCloner(connector connector.Connector, git git.Git) Cloner {
2226
}
2327
}
2428

25-
func (c *RealCloner) Clone(path string) (string, error) {
26-
// TODO: clone
27-
// TODO: get name of directory
28-
// TODO: connect to that directory
29+
func (c *RealCloner) Clone(opts model.GitCloneOptions) (string, error) {
30+
if _, err := c.git.Clone(opts.Repo, opts.CmdDir, opts.Dir); err != nil {
31+
return "", err
32+
}
33+
34+
path := getPath(opts)
35+
36+
newOpts := model.ConnectOpts{}
37+
if _, err := c.connector.Connect(path, newOpts); err != nil {
38+
return "", err
39+
}
40+
2941
return "", nil
42+
43+
}
44+
45+
func getPath(opts model.GitCloneOptions) string {
46+
var path string
47+
if opts.CmdDir != "" {
48+
path = opts.CmdDir
49+
} else {
50+
path, _ = os.Getwd()
51+
}
52+
53+
if opts.Dir != "" {
54+
path = path + "/" + opts.Dir
55+
} else {
56+
repoName := getRepoName(opts.Repo)
57+
path = path + "/" + repoName
58+
}
59+
return path
60+
}
61+
62+
func getRepoName(url string) string {
63+
parts := strings.Split(url, "/")
64+
lastPart := parts[len(parts)-1]
65+
repoName := strings.TrimSuffix(lastPart, ".git")
66+
return repoName
3067
}

cloner/mock_Cloner.go

Lines changed: 19 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

git/git.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
type Git interface {
88
ShowTopLevel(name string) (bool, string, error)
99
GitCommonDir(name string) (bool, string, error)
10-
Clone(name string) (string, error)
10+
Clone(url string, cmdDir string, dir string) (string, error)
1111
}
1212

1313
type RealGit struct {
@@ -34,8 +34,19 @@ func (g *RealGit) GitCommonDir(path string) (bool, string, error) {
3434
return true, out, nil
3535
}
3636

37-
func (g *RealGit) Clone(name string) (string, error) {
38-
out, err := g.shell.Cmd("git", "clone", name)
37+
func (g *RealGit) Clone(url string, cmdDir string, dir string) (string, error) {
38+
var out string
39+
var err error
40+
41+
args := []string{"clone", url}
42+
if cmdDir != "" {
43+
args = append([]string{"-C", cmdDir}, args...)
44+
}
45+
if dir != "" {
46+
args = append(args, dir)
47+
}
48+
49+
out, err = g.shell.Cmd("git", args...)
3950
if err != nil {
4051
return "", err
4152
}

git/mock_Git.go

Lines changed: 17 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

oswrap/mock_Os.go

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

seshcli/clone.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package seshcli
22

33
import (
4-
"fmt"
4+
"errors"
55

6+
"github.com/joshmedeski/sesh/cloner"
7+
"github.com/joshmedeski/sesh/model"
68
cli "github.com/urfave/cli/v2"
79
)
810

9-
func Clone() *cli.Command {
11+
func Clone(c cloner.Cloner) *cli.Command {
1012
return &cli.Command{
1113
Name: "clone",
1214
Aliases: []string{"cl"},
@@ -15,14 +17,28 @@ func Clone() *cli.Command {
1517
Flags: []cli.Flag{
1618
&cli.StringFlag{
1719
Name: "cmdDir",
18-
Aliases: []string{"d"},
20+
Aliases: []string{"c"},
1921
Usage: "The directory to run the git command in",
2022
},
23+
&cli.StringFlag{
24+
Name: "dir",
25+
Aliases: []string{"d"},
26+
Usage: "The name of the directory that git is creating",
27+
},
2128
},
2229
Action: func(cCtx *cli.Context) error {
23-
// TODO: implement clone command
24-
fmt.Println("Clone command coming soon to sesh v2")
25-
return nil
30+
31+
if cCtx.NArg() != 1 {
32+
return errors.New("please provide url to clone")
33+
}
34+
repo := cCtx.Args().First()
35+
36+
opts := model.GitCloneOptions{CmdDir: cCtx.String("cmdDir"), Repo: repo, Dir: cCtx.String("dir")}
37+
if _, err := c.Clone(opts); err != nil {
38+
return err
39+
} else {
40+
return nil
41+
}
2642
},
2743
}
2844
}

0 commit comments

Comments
 (0)