-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat_callbacks.go
More file actions
62 lines (52 loc) · 2.1 KB
/
Copy pathformat_callbacks.go
File metadata and controls
62 lines (52 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package clicky
import (
"fmt"
"github.com/flanksource/clicky/formatters"
"github.com/flanksource/clicky/task"
)
type FormatManager = formatters.FormatManager
type FormatCallback = formatters.FormatCallback
type BeforeFormatFunc = formatters.BeforeFormatFunc
type AfterFormatFunc = formatters.AfterFormatFunc
// AddFormatCallback registers a global formatting callback. The callback is
// applied to top-level clicky formatting as well as HTTP/RPC response
// formatting paths.
func AddFormatCallback(callback FormatCallback) {
formatters.AddFormatCallback(callback)
}
// ClearFormatCallbacks removes all registered format callbacks.
// This is primarily useful in tests.
func ClearFormatCallbacks() {
formatters.ClearFormatCallbacks()
}
// FormatWithContext formats using the shared clicky formatter while forwarding
// ctx to registered format callbacks.
func FormatWithContext(ctx any, o any, opts ...FormatOptions) (string, error) {
return Formatter.FormatWithContext(ctx, formatters.MergeOptions(append([]FormatOptions{defaultOpts}, opts...)...), o)
}
// MustFormatWithContext formats using the shared clicky formatter and panics on
// error.
func MustFormatWithContext(ctx any, o any, opts ...FormatOptions) string {
result, err := Formatter.FormatWithContext(ctx, formatters.MergeOptions(append([]FormatOptions{defaultOpts}, opts...)...), o)
if err != nil {
panic(err)
}
return result
}
// MustPrintWithContext formats using the shared clicky formatter, writes the
// output to stdout, and forwards ctx to registered format callbacks.
func MustPrintWithContext(ctx any, o any, opts ...FormatOptions) {
_ = task.Wait()
result, err := FormatWithContext(ctx, o, opts...)
if err != nil {
panic(err)
}
fmt.Print(result)
}
// FormatToFileWithContext formats using the shared clicky formatter, writes the
// output to file, and forwards ctx to registered format callbacks.
func FormatToFileWithContext(ctx any, o any, opts FormatOptions, file string) error {
opts.Output = file
_opts := formatters.MergeOptions(append([]FormatOptions{defaultOpts}, opts)...)
return Formatter.FormatToFileWithContext(ctx, _opts, o)
}