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

added option to control progress of clone and push and configure logger levels #178

Merged
merged 1 commit into from
Sep 20, 2021
Merged
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
2 changes: 1 addition & 1 deletion cmd/commands/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func RunAppCreate(ctx context.Context, opts *AppCreateOptions) error {
return fmt.Errorf("failed to get application namespace: %w", err)
}

log.G(ctx).WithField("timeout", opts.Timeout).Infof("Waiting for '%s' to finish syncing", opts.AppOpts.AppName)
log.G(ctx).WithField("timeout", opts.Timeout).Infof("waiting for '%s' to finish syncing", opts.AppOpts.AppName)
fullName := fmt.Sprintf("%s-%s", opts.ProjectName, opts.AppOpts.AppName)

// wait for argocd to be ready before applying argocd-apps
Expand Down
2 changes: 1 addition & 1 deletion cmd/commands/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ func buildBootstrapManifests(namespace, appSpecifier string, cloneOpts *git.Clon
revision: cloneOpts.Revision(),
srcPath: filepath.Join(cloneOpts.Path(), store.Default.BootsrtrapDir, store.Default.ArgoCDName),
noFinalizer: true,
labels: argocdLabels,
labels: argocdLabels,
})
if err != nil {
return nil, err
Expand Down
27 changes: 20 additions & 7 deletions pkg/git/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ type (
PushOptions struct {
AddGlobPattern string
CommitMsg string
Progress io.Writer
}

repo struct {
gogit.Repository
auth Auth
auth Auth
progress io.Writer
}
)

Expand Down Expand Up @@ -205,6 +207,11 @@ func (r *repo) Persist(ctx context.Context, opts *PushOptions) (string, error) {
return "", ErrNilOpts
}

progress := opts.Progress
if progress == nil {
progress = r.progress
}

addPattern := "."

if opts.AddGlobPattern != "" {
Expand All @@ -227,7 +234,7 @@ func (r *repo) Persist(ctx context.Context, opts *PushOptions) (string, error) {

return h.String(), r.PushContext(ctx, &gg.PushOptions{
Auth: getAuth(r.auth),
Progress: os.Stderr,
Progress: progress,
})
}

Expand All @@ -236,15 +243,16 @@ var clone = func(ctx context.Context, opts *CloneOptions) (*repo, error) {
return nil, ErrNilOpts
}

if opts.Progress == nil {
opts.Progress = os.Stderr
progress := opts.Progress
if progress == nil {
progress = os.Stderr
}

cloneOpts := &gg.CloneOptions{
URL: opts.url,
Auth: getAuth(opts.Auth),
Depth: 1,
Progress: opts.Progress,
Progress: progress,
}

log.G(ctx).WithField("url", opts.url).Debug("cloning git repo")
Expand All @@ -253,7 +261,7 @@ var clone = func(ctx context.Context, opts *CloneOptions) (*repo, error) {
return nil, err
}

repo := &repo{Repository: r, auth: opts.Auth}
repo := &repo{Repository: r, auth: opts.Auth, progress: progress}

if opts.revision != "" {
if err := checkoutRef(repo, opts.revision); err != nil {
Expand Down Expand Up @@ -306,7 +314,12 @@ var initRepo = func(ctx context.Context, opts *CloneOptions) (*repo, error) {
return nil, err
}

r := &repo{Repository: ggr, auth: opts.Auth}
progress := opts.Progress
if progress == nil {
progress = os.Stderr
}

r := &repo{Repository: ggr, auth: opts.Auth, progress: progress}
if err = r.addRemote("origin", opts.url); err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/git/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ func Test_repo_Persist(t *testing.T) {
mockWt.On("AddGlob", mock.Anything).Return(tt.retErr)
mockWt.On("Commit", mock.Anything, mock.Anything).Return(plumbing.NewHash(tt.retRevision), tt.retErr)

r := &repo{Repository: mockRepo}
r := &repo{Repository: mockRepo, progress: os.Stderr}
worktree = func(r gogit.Repository) (gogit.Worktree, error) {
return mockWt, tt.retErr
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Logger interface {

// AddPFlags adds persistent logger flags to cmd
AddPFlags(*cobra.Command)
Configure() error
}

func WithLogger(ctx context.Context, logger Logger) context.Context {
Expand Down Expand Up @@ -88,3 +89,4 @@ func (NopLogger) Errorf(string, ...interface{}) {}
func (l NopLogger) WithField(string, interface{}) Logger { return l }
func (l NopLogger) WithFields(Fields) Logger { return l }
func (l NopLogger) WithError(error) Logger { return l }
func (l NopLogger) Configure() error { return nil }
8 changes: 6 additions & 2 deletions pkg/log/logrus.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,14 @@ func (l *logrusAdapter) AddPFlags(cmd *cobra.Command) {
cmdutil.LogFormat = *format
cmdutil.LogLevel = l.c.Level

return l.configure(flags)
return l.configure()
}
}

func (l *logrusAdapter) Configure() error {
return l.configure()
}

func (l *logrusAdapter) Printf(format string, args ...interface{}) {
if len(args) > 0 {
fmt.Printf(fmt.Sprintf("%s\n", format), args...)
Expand All @@ -89,7 +93,7 @@ func (l *logrusAdapter) WithError(err error) Logger {
return FromLogrus(l.Entry.WithError(err), l.c)
}

func (l *logrusAdapter) configure(f *pflag.FlagSet) error {
func (l *logrusAdapter) configure() error {
var (
err error
fmtr logrus.Formatter
Expand Down