Skip to content

Commit 07cc7cb

Browse files
feat(diag): adds diag builder for a developer friendly API.
1 parent 60637f6 commit 07cc7cb

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

diag/builder.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package diag
2+
3+
// Builder provides a log entry building api. This should not be used directly,
4+
// but should be used by calling the component based method on the supplied
5+
// Diagnostics.
6+
type Builder struct {
7+
d *Diagnostics
8+
e *Diagnostic
9+
}
10+
11+
func (b *Builder) Fatal(detail, summary string) *Diagnostics {
12+
return b.build(SeverityFatal, detail, summary)
13+
}
14+
15+
func (b *Builder) Error(detail, summary string) *Diagnostics {
16+
return b.build(SeverityError, detail, summary)
17+
}
18+
19+
func (b *Builder) Warn(detail, summary string) *Diagnostics {
20+
return b.build(SeverityWarn, detail, summary)
21+
}
22+
23+
func (b *Builder) Info(detail, summary string) *Diagnostics {
24+
return b.build(SeverityInfo, detail, summary)
25+
}
26+
27+
func (b *Builder) Debug(detail, summary string) *Diagnostics {
28+
return b.build(SeverityDebug, detail, summary)
29+
}
30+
31+
func (b *Builder) Trace(detail, summary string) *Diagnostics {
32+
return b.build(SeverityTrace, detail, summary)
33+
}
34+
35+
func (b *Builder) build(sev Severity, detail, summary string) *Diagnostics {
36+
diags, diag := b.d, b.e
37+
38+
diag.Severity = sev
39+
diag.Detail = detail
40+
diag.Summary = summary
41+
42+
diags.Append(*diag)
43+
44+
return diags
45+
}

diag/diagnostics.go

+29
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,35 @@ func (d *Diagnostics) Append(diags ...Diagnostic) {
88
*d = append(*d, diags...)
99
}
1010

11+
// GlobalFile enables building up a diagnostic message for a global
12+
// configuration file value.
13+
func (d *Diagnostics) GlobalFile(path string) *Builder {
14+
return d.builder(ComponentGlobalFile, path)
15+
}
16+
17+
// Env enables building up a diagnostic message for an environment variable value.
18+
func (d *Diagnostics) Env(path string) *Builder {
19+
return d.builder(ComponentEnvVar, path)
20+
}
21+
22+
// LocalFile enables building up a diagnostic message for a local configuration
23+
// file value.
24+
func (d *Diagnostics) LocalFile(path string) *Builder {
25+
return d.builder(ComponentLocalFile, path)
26+
}
27+
28+
// Flag enables building up a diagnostic message for a CLI flag value.
29+
func (d *Diagnostics) Flag(path string) *Builder {
30+
return d.builder(ComponentFlag, path)
31+
}
32+
33+
func (d *Diagnostics) builder(component Component, path string) *Builder {
34+
return &Builder{
35+
d: d,
36+
e: &Diagnostic{Component: component, Path: path},
37+
}
38+
}
39+
1140
// Fatals returns all diagnostic entries at SeverityFatal level.
1241
func (d *Diagnostics) Fatals() []Diagnostic {
1342
return d.getDiagsWithLevel(SeverityFatal)

0 commit comments

Comments
 (0)