Skip to content

Commit 2c6aeb6

Browse files
authored
Add diag.SafeDiagnostics class (#2927)
## Changes A new helper to update diags from multiple threads. Also includes shortcuts for frequently used Error and Errorf. ## Why Simpler and less error-prone code in mutators. Using this in #2926 ## Tests Existing tests.
1 parent c2b7c6e commit 2c6aeb6

File tree

3 files changed

+35
-15
lines changed

3 files changed

+35
-15
lines changed

bundle/apps/upload_config.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"path"
88
"strings"
9-
"sync"
109

1110
"github.com/databricks/cli/bundle"
1211
"github.com/databricks/cli/bundle/config/resources"
@@ -23,10 +22,9 @@ type uploadConfig struct {
2322
}
2423

2524
func (u *uploadConfig) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
26-
var diags diag.Diagnostics
25+
var diags diag.SafeDiagnostics
2726
errGroup, ctx := errgroup.WithContext(ctx)
2827

29-
mu := sync.Mutex{}
3028
for key, app := range b.Config.Resources.Apps {
3129
// If the app has a config, we need to deploy it first.
3230
// It means we need to write app.yml file with the content of the config field
@@ -47,25 +45,23 @@ func (u *uploadConfig) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnos
4745
errGroup.Go(func() error {
4846
err := f.Write(ctx, path.Join(appPath, "app.yml"), buf, filer.OverwriteIfExists)
4947
if err != nil {
50-
mu.Lock()
51-
diags = append(diags, diag.Diagnostic{
48+
diags.Append(diag.Diagnostic{
5249
Severity: diag.Error,
5350
Summary: "Failed to save config",
5451
Detail: fmt.Sprintf("Failed to write %s file: %s", path.Join(app.SourceCodePath, "app.yml"), err),
5552
Locations: b.Config.GetLocations("resources.apps." + key),
5653
})
57-
mu.Unlock()
5854
}
5955
return nil
6056
})
6157
}
6258
}
6359

6460
if err := errGroup.Wait(); err != nil {
65-
return diags.Extend(diag.FromErr(err))
61+
diags.AppendError(err)
6662
}
6763

68-
return diags
64+
return diags.Diags
6965
}
7066

7167
// Name implements bundle.Mutator.

bundle/config/validate/validate_sync_patterns.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"strings"
7-
"sync"
87

98
"github.com/databricks/cli/bundle"
109
"github.com/databricks/cli/libs/diag"
@@ -43,9 +42,8 @@ func (v *validateSyncPatterns) Apply(ctx context.Context, b *bundle.Bundle) diag
4342
}
4443

4544
func checkPatterns(patterns []string, path string, b *bundle.Bundle) (diag.Diagnostics, error) {
46-
var mu sync.Mutex
4745
var errs errgroup.Group
48-
var diags diag.Diagnostics
46+
var diags diag.SafeDiagnostics
4947

5048
for index, pattern := range patterns {
5149
// If the pattern is negated, strip the negation prefix
@@ -67,18 +65,16 @@ func checkPatterns(patterns []string, path string, b *bundle.Bundle) (diag.Diagn
6765

6866
if len(all) == 0 {
6967
path := fmt.Sprintf("%s[%d]", path, index)
70-
mu.Lock()
71-
diags = diags.Append(diag.Diagnostic{
68+
diags.Append(diag.Diagnostic{
7269
Severity: diag.Warning,
7370
Summary: fmt.Sprintf("Pattern %s does not match any files", pattern),
7471
Locations: b.Config.GetLocations(path),
7572
Paths: []dyn.Path{dyn.MustPathFromString(path)},
7673
})
77-
mu.Unlock()
7874
}
7975
return nil
8076
})
8177
}
8278

83-
return diags, errs.Wait()
79+
return diags.Diags, errs.Wait()
8480
}

libs/diag/diagnostic.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package diag
33
import (
44
"errors"
55
"fmt"
6+
"sync"
67

78
"github.com/databricks/cli/libs/dyn"
89
)
@@ -146,3 +147,30 @@ func (ds Diagnostics) Filter(severity Severity) Diagnostics {
146147
}
147148
return out
148149
}
150+
151+
type SafeDiagnostics struct {
152+
Mutex sync.Mutex
153+
Diags Diagnostics
154+
}
155+
156+
func (t *SafeDiagnostics) Append(d Diagnostic) {
157+
t.Mutex.Lock()
158+
defer t.Mutex.Unlock()
159+
160+
t.Diags = t.Diags.Append(d)
161+
}
162+
163+
func (t *SafeDiagnostics) Extend(other Diagnostics) {
164+
t.Mutex.Lock()
165+
defer t.Mutex.Unlock()
166+
167+
t.Diags = t.Diags.Extend(other)
168+
}
169+
170+
func (t *SafeDiagnostics) AppendErrorf(msg string, args ...any) {
171+
t.Extend(FromErr(fmt.Errorf(msg, args...)))
172+
}
173+
174+
func (t *SafeDiagnostics) AppendError(err error) {
175+
t.Extend(FromErr(err))
176+
}

0 commit comments

Comments
 (0)