Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: clone command #25

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions clone/clone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package clone

import (
"fmt"
"os"
"zit/cli"
"zit/config"
"zit/git"
"zit/identity"

"github.com/spf13/cobra"
)

// CloneCmd TODO
var CloneCmd = &cobra.Command{
Use: "clone",
Short: "Clone git repo and do all the setup",
RunE: func(cmd *cobra.Command, args []string) error {
url := args[0]

repo, err := git.ExtractRepoInfo(url)
if err != nil {
return err
}

confPath, err := config.LocateConfFile()
cli.PrintlnExit(err)

confFile, err := os.Open(confPath)
cli.PrintlnExit(err)

hostMap, err := config.ReadHostMap(confPath, confFile)
cli.PrintlnExit(err)

conf, err := hostMap.Get((*repo).Host)
cli.PrintlnExit(err)

cred := identity.FindBestMatch(*conf, *repo)
if cred == nil {
cli.PrintlnExit(fmt.Errorf("cannot find a match for host %q", (*repo).Host))
}

// clone repo
if err := git.Clone(url); err != nil {
return err
}

// cd repo-name
if err := os.Chdir(repo.Name); err != nil {
return err
}

// zit set
cli.PrintlnExit(
git.SetConfig("--local", "user.name", cred.Name),
)
cli.PrintlnExit(
git.SetConfig("--local", "user.email", cred.Email),
)

fmt.Printf("cloned %s\n", url)
fmt.Printf("set user: %s <%s>\n", cred.Name, cred.Email)

return nil
},
}
8 changes: 8 additions & 0 deletions git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,11 @@ func IsGitDir(dir string) (bool, error) {

return true, nil
}

// Clone clones a repository
func Clone(url string) error {
if _, err := git("clone", url); err != nil {
return err
}
return nil
}
3 changes: 2 additions & 1 deletion identity/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"zit/git"
)

func findBestMatch(conf config.Config, repo git.RepoInfo) (user *config.User) {
// FindBestMatch finds best credentials match given host, owner, and repo name
func FindBestMatch(conf config.Config, repo git.RepoInfo) (user *config.User) {
if conf.Default != nil {
user = &config.User{
conf.Default.Name,
Expand Down
8 changes: 4 additions & 4 deletions identity/identity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestFindBestMatch(t *testing.T) {
}

want := &defaultUser
have := findBestMatch(conf, repoInfo)
have := FindBestMatch(conf, repoInfo)

if !reflect.DeepEqual(want, have) {
t.Errorf("want: %s; have: %s", want, have)
Expand All @@ -74,7 +74,7 @@ func TestFindBestMatch(t *testing.T) {
}

want := &corpUser1
have := findBestMatch(conf, repoInfo)
have := FindBestMatch(conf, repoInfo)

if !reflect.DeepEqual(want, have) {
t.Errorf("want: %s; have: %s", want, have)
Expand All @@ -100,7 +100,7 @@ func TestFindBestMatch(t *testing.T) {
}

want := &corpUser1
have := findBestMatch(conf, repoInfo)
have := FindBestMatch(conf, repoInfo)

if !reflect.DeepEqual(want, have) {
t.Errorf("want: %s; have: %s", want, have)
Expand Down Expand Up @@ -136,7 +136,7 @@ func TestFindBestMatch(t *testing.T) {
}

want := &corpUser2
have := findBestMatch(conf, repoInfo)
have := FindBestMatch(conf, repoInfo)

if !reflect.DeepEqual(want, have) {
t.Errorf("want: %s; have: %s", want, have)
Expand Down
2 changes: 1 addition & 1 deletion identity/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ defined in the configuration file:
conf, err := hostMap.Get((*repo).Host)
cli.PrintlnExit(err)

cred := findBestMatch(*conf, *repo)
cred := FindBestMatch(*conf, *repo)
if cred == nil {
cli.PrintlnExit(fmt.Errorf("cannot find a match for host %q", (*repo).Host))
}
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"zit/cli"
"zit/clone"
"zit/doctor"
"zit/identity"
"zit/version"
Expand All @@ -23,5 +24,6 @@ func init() {
identity.SetCmd,
version.VersionCmd,
doctor.DoctorCmd,
clone.CloneCmd,
)
}