Skip to content

Commit f20ddd3

Browse files
authored
Remove diag.SafeDiagnostics; replace with logdiag (#3197)
logdiag is thread-safe as well and easier to use
1 parent 4582126 commit f20ddd3

File tree

4 files changed

+23
-55
lines changed

4 files changed

+23
-55
lines changed

bundle/apps/upload_config.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/databricks/cli/bundle/deploy"
1313
"github.com/databricks/cli/libs/diag"
1414
"github.com/databricks/cli/libs/filer"
15+
"github.com/databricks/cli/libs/logdiag"
1516
"golang.org/x/sync/errgroup"
1617

1718
"gopkg.in/yaml.v3"
@@ -22,7 +23,6 @@ type uploadConfig struct {
2223
}
2324

2425
func (u *uploadConfig) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
25-
var diags diag.SafeDiagnostics
2626
errGroup, ctx := errgroup.WithContext(ctx)
2727

2828
for key, app := range b.Config.Resources.Apps {
@@ -45,7 +45,7 @@ func (u *uploadConfig) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnos
4545
errGroup.Go(func() error {
4646
err := f.Write(ctx, path.Join(appPath, "app.yml"), buf, filer.OverwriteIfExists)
4747
if err != nil {
48-
diags.Append(diag.Diagnostic{
48+
logdiag.LogDiag(ctx, diag.Diagnostic{
4949
Severity: diag.Error,
5050
Summary: "Failed to save config",
5151
Detail: fmt.Sprintf("Failed to write %s file: %s", path.Join(app.SourceCodePath, "app.yml"), err),
@@ -58,10 +58,10 @@ func (u *uploadConfig) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnos
5858
}
5959

6060
if err := errGroup.Wait(); err != nil {
61-
diags.AppendError(err)
61+
logdiag.LogError(ctx, err)
6262
}
6363

64-
return diags.Diags
64+
return nil
6565
}
6666

6767
// Name implements bundle.Mutator.

bundle/config/validate/validate_sync_patterns.go

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/databricks/cli/libs/diag"
1010
"github.com/databricks/cli/libs/dyn"
1111
"github.com/databricks/cli/libs/fileset"
12+
"github.com/databricks/cli/libs/logdiag"
1213
"golang.org/x/sync/errgroup"
1314
)
1415

@@ -24,26 +25,18 @@ func (v *validateSyncPatterns) Name() string {
2425

2526
func (v *validateSyncPatterns) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
2627
s := b.Config.Sync
27-
if len(s.Exclude) == 0 && len(s.Include) == 0 {
28-
return nil
29-
}
30-
31-
diags, err := checkPatterns(s.Exclude, "sync.exclude", b)
32-
if err != nil {
33-
return diag.FromErr(err)
34-
}
3528

36-
includeDiags, err := checkPatterns(s.Include, "sync.include", b)
37-
if err != nil {
38-
return diag.FromErr(err)
29+
checkPatterns(ctx, s.Exclude, "sync.exclude", b)
30+
if logdiag.HasError(ctx) {
31+
return nil
3932
}
4033

41-
return diags.Extend(includeDiags)
34+
checkPatterns(ctx, s.Include, "sync.include", b)
35+
return nil
4236
}
4337

44-
func checkPatterns(patterns []string, path string, b *bundle.Bundle) (diag.Diagnostics, error) {
38+
func checkPatterns(ctx context.Context, patterns []string, path string, b *bundle.Bundle) {
4539
var errs errgroup.Group
46-
var diags diag.SafeDiagnostics
4740

4841
for index, pattern := range patterns {
4942
// If the pattern is negated, strip the negation prefix
@@ -65,7 +58,7 @@ func checkPatterns(patterns []string, path string, b *bundle.Bundle) (diag.Diagn
6558

6659
if len(all) == 0 {
6760
path := fmt.Sprintf("%s[%d]", path, index)
68-
diags.Append(diag.Diagnostic{
61+
logdiag.LogDiag(ctx, diag.Diagnostic{
6962
Severity: diag.Warning,
7063
Summary: fmt.Sprintf("Pattern %s does not match any files", pattern),
7164
Locations: b.Config.GetLocations(path),
@@ -76,5 +69,8 @@ func checkPatterns(patterns []string, path string, b *bundle.Bundle) (diag.Diagn
7669
})
7770
}
7871

79-
return diags.Diags, errs.Wait()
72+
err := errs.Wait()
73+
if err != nil {
74+
logdiag.LogError(ctx, err)
75+
}
8076
}

bundle/terranova/apply.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/databricks/cli/libs/dagrun"
1616
"github.com/databricks/cli/libs/diag"
1717
"github.com/databricks/cli/libs/log"
18+
"github.com/databricks/cli/libs/logdiag"
1819
"github.com/databricks/cli/libs/structdiff"
1920
"github.com/databricks/databricks-sdk-go"
2021
)
@@ -46,14 +47,13 @@ func (m *terranovaApplyMutator) Apply(ctx context.Context, b *bundle.Bundle) dia
4647
}
4748

4849
client := b.WorkspaceClient()
49-
var diags diag.SafeDiagnostics
5050

5151
err := g.Run(defaultParallelism, func(node deployplan.Action) {
5252
// TODO: if a given node fails, all downstream nodes should not be run. We should report those nodes.
5353
// TODO: ensure that config for this node is fully resolved at this point.
5454

5555
if node.ActionType == deployplan.ActionTypeUnset {
56-
diags.AppendErrorf("internal error, no action set %#v", node)
56+
logdiag.LogError(ctx, fmt.Errorf("internal error, no action set %#v", node))
5757
return
5858
}
5959

@@ -63,7 +63,7 @@ func (m *terranovaApplyMutator) Apply(ctx context.Context, b *bundle.Bundle) dia
6363
var ok bool
6464
config, ok = b.GetResourceConfig(node.Group, node.Name)
6565
if !ok {
66-
diags.AppendErrorf("internal error: cannot get config for group=%v name=%v", node.Group, node.Name)
66+
logdiag.LogError(ctx, fmt.Errorf("internal error: cannot get config for group=%v name=%v", node.Group, node.Name))
6767
return
6868
}
6969
}
@@ -78,20 +78,20 @@ func (m *terranovaApplyMutator) Apply(ctx context.Context, b *bundle.Bundle) dia
7878
// TODO: pass node.ActionType and respect it. Do not change Update to Recreate or Delete for example.
7979
err := d.Deploy(ctx, config, node.ActionType)
8080
if err != nil {
81-
diags.AppendError(err)
81+
logdiag.LogError(ctx, err)
8282
return
8383
}
8484
})
8585
if err != nil {
86-
diags.AppendError(err)
86+
logdiag.LogError(ctx, err)
8787
}
8888

8989
err = b.ResourceDatabase.Finalize()
9090
if err != nil {
91-
diags.AppendError(err)
91+
logdiag.LogError(ctx, err)
9292
}
9393

94-
return diags.Diags
94+
return nil
9595
}
9696

9797
type Deployer struct {

libs/diag/diagnostic.go

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

87
"github.com/databricks/cli/libs/dyn"
98
)
@@ -147,30 +146,3 @@ func (ds Diagnostics) Filter(severity Severity) Diagnostics {
147146
}
148147
return out
149148
}
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)