From cc622baf6745fa8a54183f1985d656bfaf9b8be6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Doma=C5=84ski?= Date: Mon, 24 Apr 2017 20:36:05 +0200 Subject: [PATCH] allow selecting git client --- git/git.go | 20 ++++++++++++++++---- git/git_test.go | 4 ++-- main.go | 11 ++++++----- main_test.go | 8 ++++---- 4 files changed, 28 insertions(+), 15 deletions(-) diff --git a/git/git.go b/git/git.go index 3705310..0bc1fe1 100644 --- a/git/git.go +++ b/git/git.go @@ -29,15 +29,27 @@ type BuildsRepo struct { client RepoClient } -func PrepareRepo() (*BuildsRepo, error) { +func PrepareRepo(gitClient string) (*BuildsRepo, error) { dir, err := ioutil.TempDir("", "tagger") if err != nil { return nil, err } - c, err := newFromLibgit(dir) - if err != nil { - return nil, err + var c RepoClient + + switch gitClient { + case "libgit": + c, err = newFromLibgit(dir) + if err != nil { + return nil, err + } + case "command": + c, err = newFromCommand(dir) + if err != nil { + return nil, err + } + default: + return nil, fmt.Errorf("Unknown git client '%s'", gitClient) } return &BuildsRepo{directory: dir, client: c}, nil diff --git a/git/git_test.go b/git/git_test.go index c2a2bdc..8306245 100644 --- a/git/git_test.go +++ b/git/git_test.go @@ -13,7 +13,7 @@ import ( ) func TestPrepareRepo(t *testing.T) { - repo, err := PrepareRepo() + repo, err := PrepareRepo("libgit") assert.Nil(t, err) defer repo.Close() @@ -30,7 +30,7 @@ func TestPrepareRepo(t *testing.T) { } func TestAddAndCommit(t *testing.T) { - repo, err := PrepareRepo() + repo, err := PrepareRepo("libgit") assert.Nil(t, err) defer repo.Close() diff --git a/main.go b/main.go index 668c36b..ff25e3a 100644 --- a/main.go +++ b/main.go @@ -107,10 +107,11 @@ type taggerOptionsArgs struct { type taggerOptions struct { Args taggerOptionsArgs `positional-args:"true" required:"true"` - Commit bool `short:"c" long:"commit" description:"Commit the changes. Will make a dry run without this flag."` - Build int32 `short:"b" long:"build" required:"false" default:"0" description:"Specify the build number to be placed inside the JSON."` - URL string `short:"u" long:"url" description:"Release notes URL"` - Codename string `short:"n" long:"codename" description:"Release codename"` + Commit bool `short:"c" long:"commit" description:"Commit the changes. Will make a dry run without this flag."` + Build int32 `short:"b" long:"build" required:"false" default:"0" description:"Specify the build number to be placed inside the JSON."` + URL string `short:"u" long:"url" description:"Release notes URL"` + Codename string `short:"n" long:"codename" description:"Release codename"` + GitClient string `long:"git-client" default:"libgit" description:"Git client. Either 'libgit' or 'command'"` } func retaggingStep(images map[string]string, opts *taggerOptions, tagTimestamp string) { @@ -155,7 +156,7 @@ func main() { fmt.Printf("Tag timestamp: %s\n", tagTimestamp) fmt.Printf("ISO timestamp: %s\n", isoTimestamp) - repo, err := git.PrepareRepo() + repo, err := git.PrepareRepo(opts.GitClient) if err != nil { log.Fatalf("Failed to clone the builds repo: %s", err.Error()) } diff --git a/main_test.go b/main_test.go index 15f569b..704a088 100644 --- a/main_test.go +++ b/main_test.go @@ -55,7 +55,7 @@ var testOldJSON = `[ // TestRenamedImages tests whether the image list // contains the same images with altered tags func TestRenamedImages(t *testing.T) { - repo, err := git.PrepareRepo() + repo, err := git.PrepareRepo("libgit") assert.Nil(t, err) defer repo.Close() @@ -127,7 +127,7 @@ func TestRenamedImages(t *testing.T) { // TestRenamedImages2 tests whether the Codename and URL have been updated func TestRenamedImages2(t *testing.T) { - repo, err := git.PrepareRepo() + repo, err := git.PrepareRepo("libgit") assert.Nil(t, err) defer repo.Close() @@ -200,7 +200,7 @@ func TestRenamedImages2(t *testing.T) { // TestRenamedImages3 tests whether the image list // contains the same images with unchanged tags func TestRenamedImages3(t *testing.T) { - repo, err := git.PrepareRepo() + repo, err := git.PrepareRepo("libgit") assert.Nil(t, err) defer repo.Close() @@ -271,7 +271,7 @@ func TestRenamedImages3(t *testing.T) { } func TestRenamedImagesBuildIncrement(t *testing.T) { - repo, err := git.PrepareRepo() + repo, err := git.PrepareRepo("libgit") assert.Nil(t, err) defer repo.Close()