-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a63df21
commit 7a32bb7
Showing
5 changed files
with
126 additions
and
0 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
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 | ||
}, | ||
} | ||
} |
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,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 | ||
} |
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,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) | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -17,6 +17,7 @@ func App() cli.App { | |
cmds.List(), | ||
cmds.Choose(), | ||
cmds.Connect(), | ||
cmds.Clone(), | ||
}, | ||
} | ||
} |