Skip to content

Commit 3901944

Browse files
authored
Add the ability to configure logger for git functions (#57)
* Add the ability to configure logger for git functions * Configure logger only when nonnil * Fix test
1 parent 75d5cc4 commit 3901944

File tree

4 files changed

+33
-12
lines changed

4 files changed

+33
-12
lines changed

git/auth.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ import (
55

66
"github.com/gruntwork-io/go-commons/shell"
77
"github.com/hashicorp/go-multierror"
8+
"github.com/sirupsen/logrus"
89
)
910

1011
// ConfigureForceHTTPS configures git to force usage of https endpoints instead of SSH based endpoints for the three
1112
// primary VCS platforms (GitHub, GitLab, BitBucket).
12-
func ConfigureForceHTTPS() error {
13+
func ConfigureForceHTTPS(logger *logrus.Logger) error {
1314
opts := shell.NewShellOptions()
15+
if logger != nil {
16+
opts.Logger = logger
17+
}
1418

1519
var allErr error
1620

@@ -40,8 +44,12 @@ func ConfigureForceHTTPS() error {
4044
// with git over HTTPS. This uses the cache credentials store to configure the credentials. Refer to the git
4145
// documentation on credentials storage for more information:
4246
// https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage
43-
func ConfigureHTTPSAuth(gitUsername string, gitOauthToken string, vcsHost string) error {
47+
func ConfigureHTTPSAuth(logger *logrus.Logger, gitUsername string, gitOauthToken string, vcsHost string) error {
4448
opts := shell.NewShellOptions()
49+
if logger != nil {
50+
opts.Logger = logger
51+
}
52+
4553
if err := shell.RunShellCommand(
4654
opts,
4755
"git", "config", "--global",

git/git.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,32 @@ package git
33
import (
44
"github.com/gruntwork-io/go-commons/files"
55
"github.com/gruntwork-io/go-commons/shell"
6+
"github.com/sirupsen/logrus"
67
)
78

89
// Clone runs git clone to clone the specified repository into the given target directory.
9-
func Clone(repo string, targetDir string) error {
10+
func Clone(logger *logrus.Logger, repo string, targetDir string) error {
1011
if !files.IsDir(targetDir) {
1112
return TargetDirectoryNotExistsErr{dirPath: targetDir}
1213
}
1314

1415
opts := shell.NewShellOptions()
16+
if logger != nil {
17+
opts.Logger = logger
18+
}
1519
return shell.RunShellCommand(opts, "git", "clone", repo, targetDir)
1620
}
1721

1822
// Checkout checks out the given ref for the repo cloned in the target directory.
19-
func Checkout(ref string, targetDir string) error {
23+
func Checkout(logger *logrus.Logger, ref string, targetDir string) error {
2024
if !files.IsDir(targetDir) {
2125
return TargetDirectoryNotExistsErr{dirPath: targetDir}
2226
}
2327

2428
opts := shell.NewShellOptions()
29+
if logger != nil {
30+
opts.Logger = logger
31+
}
2532
opts.WorkingDir = targetDir
2633
return shell.RunShellCommand(opts, "git", "checkout", ref)
2734
}

git/git_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
"github.com/gruntwork-io/go-commons/files"
9+
"github.com/gruntwork-io/go-commons/logging"
910
"github.com/stretchr/testify/assert"
1011
"github.com/stretchr/testify/require"
1112
)
@@ -15,7 +16,7 @@ func TestGitClone(t *testing.T) {
1516

1617
tmpDir, err := ioutil.TempDir("", "git-test")
1718
require.NoError(t, err)
18-
require.NoError(t, Clone("https://github.com/gruntwork-io/go-commons.git", tmpDir))
19+
require.NoError(t, Clone(logging.GetLogger(t.Name()), "https://github.com/gruntwork-io/go-commons.git", tmpDir))
1920
assert.True(t, files.FileExists(filepath.Join(tmpDir, "LICENSE.txt")))
2021
}
2122

@@ -24,7 +25,7 @@ func TestGitCheckout(t *testing.T) {
2425

2526
tmpDir, err := ioutil.TempDir("", "git-test")
2627
require.NoError(t, err)
27-
require.NoError(t, Clone("https://github.com/gruntwork-io/go-commons.git", tmpDir))
28-
require.NoError(t, Checkout("v0.10.0", tmpDir))
28+
require.NoError(t, Clone(logging.GetLogger(t.Name()), "https://github.com/gruntwork-io/go-commons.git", tmpDir))
29+
require.NoError(t, Checkout(logging.GetLogger(t.Name()), "v0.10.0", tmpDir))
2930
assert.False(t, files.FileExists(filepath.Join(tmpDir, "git", "git_test.go")))
3031
}

git/test/auth_test.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/gruntwork-io/go-commons/files"
1212
"github.com/gruntwork-io/go-commons/git"
13+
"github.com/gruntwork-io/go-commons/logging"
1314
"github.com/gruntwork-io/terratest/modules/environment"
1415
"github.com/stretchr/testify/assert"
1516
"github.com/stretchr/testify/require"
@@ -19,6 +20,10 @@ const (
1920
gitPATEnvName = "GITHUB_OAUTH_TOKEN"
2021
)
2122

23+
var (
24+
logger = logging.GetLogger("testlogger")
25+
)
26+
2227
// NOTE: All these tests should be run in the provided docker environment to avoid polluting the local git configuration
2328
// settings. The tests will assert that it is running in the docker environment, and will fail if it is not.
2429

@@ -31,11 +36,11 @@ func TestHTTPSAuth(t *testing.T) {
3136

3237
environment.RequireEnvVar(t, gitPATEnvName)
3338
gitPAT := os.Getenv(gitPATEnvName)
34-
require.NoError(t, git.ConfigureHTTPSAuth("git", gitPAT, "github.com"))
39+
require.NoError(t, git.ConfigureHTTPSAuth(logger, "git", gitPAT, "github.com"))
3540

3641
tmpDir, err := ioutil.TempDir("", "git-test")
3742
require.NoError(t, err)
38-
require.NoError(t, git.Clone("https://github.com/gruntwork-io/terraform-aws-lambda.git", tmpDir))
43+
require.NoError(t, git.Clone(logger, "https://github.com/gruntwork-io/terraform-aws-lambda.git", tmpDir))
3944
assert.True(t, files.IsDir(filepath.Join(tmpDir, "modules/lambda")))
4045
}
4146

@@ -48,11 +53,11 @@ func TestForceHTTPS(t *testing.T) {
4853

4954
environment.RequireEnvVar(t, gitPATEnvName)
5055
gitPAT := os.Getenv(gitPATEnvName)
51-
require.NoError(t, git.ConfigureHTTPSAuth("git", gitPAT, "github.com"))
52-
require.NoError(t, git.ConfigureForceHTTPS())
56+
require.NoError(t, git.ConfigureHTTPSAuth(logger, "git", gitPAT, "github.com"))
57+
require.NoError(t, git.ConfigureForceHTTPS(logger))
5358

5459
tmpDir, err := ioutil.TempDir("", "git-test")
5560
require.NoError(t, err)
56-
require.NoError(t, git.Clone("git@github.com:gruntwork-io/terraform-aws-lambda.git", tmpDir))
61+
require.NoError(t, git.Clone(logger, "git@github.com:gruntwork-io/terraform-aws-lambda.git", tmpDir))
5762
assert.True(t, files.IsDir(filepath.Join(tmpDir, "modules/lambda")))
5863
}

0 commit comments

Comments
 (0)