Skip to content

Commit

Permalink
Write git clone progress only if terminal is a TTY
Browse files Browse the repository at this point in the history
  • Loading branch information
KnisterPeter committed May 6, 2021
1 parent 70db997 commit 8f3ad1e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
github.com/joho/godotenv v1.3.0
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect
github.com/mattn/go-isatty v0.0.12
github.com/mitchellh/go-homedir v1.1.0
github.com/moby/buildkit v0.8.1
github.com/moby/sys/mount v0.2.0 // indirect
Expand Down
37 changes: 17 additions & 20 deletions pkg/common/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/go-ini/ini"
"github.com/mattn/go-isatty"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -238,29 +239,25 @@ func CloneIfRequired(ctx context.Context, refName plumbing.ReferenceName, input
r, err := git.PlainOpen(input.Dir)
if err != nil {
var progressWriter io.Writer
if entry, ok := logger.(*log.Entry); ok {
progressWriter = entry.WriterLevel(log.DebugLevel)
} else if lgr, ok := logger.(*log.Logger); ok {
progressWriter = lgr.WriterLevel(log.DebugLevel)
} else {
log.Errorf("Unable to get writer from logger (type=%T)", logger)
progressWriter = os.Stdout
if isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) {
if entry, ok := logger.(*log.Entry); ok {
progressWriter = entry.WriterLevel(log.DebugLevel)
} else if lgr, ok := logger.(*log.Logger); ok {
progressWriter = lgr.WriterLevel(log.DebugLevel)
} else {
log.Errorf("Unable to get writer from logger (type=%T)", logger)
progressWriter = os.Stdout
}
}

var cloneOptions git.CloneOptions
cloneOptions := git.CloneOptions{
URL: input.URL,
Progress: progressWriter,
}
if input.Token != "" {
cloneOptions = git.CloneOptions{
URL: input.URL,
Progress: progressWriter,
Auth: &http.BasicAuth{
Username: "token",
Password: input.Token,
},
}
} else {
cloneOptions = git.CloneOptions{
URL: input.URL,
Progress: progressWriter,
cloneOptions.Auth = &http.BasicAuth{
Username: "token",
Password: input.Token,
}
}

Expand Down

0 comments on commit 8f3ad1e

Please sign in to comment.