• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mszostok / version / 4901432096

06 May 2023 11:58AM UTC coverage: 82.692% (+1.9%) from 80.781%
4901432096

push

github

GitHub
Add option to load style config from file (#92)

31 of 31 new or added lines in 2 files covered. (100.0%)

301 of 364 relevant lines covered (82.69%)

0.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.86
/printer/opts.go
1
package printer
2

3
import (
4
        "encoding/json"
5
        "os"
6
        "path/filepath"
7

8
        "go.szostok.io/version/style"
9
        "go.szostok.io/version/upgrade"
10
        "gopkg.in/yaml.v3"
11
)
12

13
// Inspired by https://github.com/kubernetes-sigs/controller-runtime/blob/v0.12.3/pkg/client/options.go
14
// It allows having the same functional opt func across all constructors. For example:
15
//
16
// - version.NewPrinter(version.WithPrettyRenderer(..))
17
// - version.NewPrettyPrinter(version.WithPrettyRenderer(..))
18

19
type (
20
        // PostHookFunc represents post execution function signature.
21
        PostHookFunc func() error
22

23
        // ContainerOption provides an option to set a Container options.
24
        ContainerOption interface {
25
                // ApplyToContainerOption sets a given option for Container.
26
                ApplyToContainerOption(*ContainerOptions)
27
        }
28

29
        // ContainerOptions holds Container possible customization settings.
30
        ContainerOptions struct {
31
                PrettyOptions []PrettyOption
32
                PostHookFunc  PostHookFunc
33
                UpgradeNotice *upgrade.GitHubDetector
34
        }
35

36
        // PrettyOption provides an option to set a Pretty printer options.
37
        PrettyOption interface {
38
                // ApplyToPrettyOption sets a given option to Pretty printer.
39
                ApplyToPrettyOption(*PrettyOptions)
40
        }
41

42
        // PrettyOptions holds Pretty possible customization settings.
43
        PrettyOptions struct {
44
                CustomRenderFn PrettyRenderFunc
45
                RenderConfig   *style.Config
46
                PostRenderFunc PrettyPostRenderFunc
47
        }
48
)
49

50
// CustomPrettyRenderer provides an option to set a custom renderer function across multiple constructors.
51
type CustomPrettyRenderer struct {
52
        rendererFn PrettyRenderFunc
53
}
54

55
// WithPrettyRenderer sets a custom renderer function.
56
func WithPrettyRenderer(renderer PrettyRenderFunc) *CustomPrettyRenderer {
1✔
57
        return &CustomPrettyRenderer{
1✔
58
                rendererFn: renderer,
1✔
59
        }
1✔
60
}
1✔
61

62
// ApplyToContainerOption sets a given option for Container.
63
func (c *CustomPrettyRenderer) ApplyToContainerOption(cfg *ContainerOptions) {
×
64
        cfg.PrettyOptions = append(cfg.PrettyOptions, c)
×
65
}
×
66

67
// ApplyToPrettyOption sets a given option to Pretty printer.
68
func (c *CustomPrettyRenderer) ApplyToPrettyOption(cfg *PrettyOptions) {
1✔
69
        cfg.CustomRenderFn = c.rendererFn
1✔
70
}
1✔
71

72
// CustomPrettyFormatting provides an option to set a custom pretty formatting across multiple constructors.
73
type CustomPrettyFormatting struct {
74
        formatting *style.Formatting
75
}
76

77
// WithPrettyFormatting sets a custom pretty formatting.
78
func WithPrettyFormatting(formatting *style.Formatting) *CustomPrettyFormatting {
1✔
79
        return &CustomPrettyFormatting{
1✔
80
                formatting: formatting,
1✔
81
        }
1✔
82
}
1✔
83

84
// ApplyToContainerOption sets a given option for Container.
85
func (c *CustomPrettyFormatting) ApplyToContainerOption(cfg *ContainerOptions) {
×
86
        cfg.PrettyOptions = append(cfg.PrettyOptions, c)
×
87
}
×
88

89
// ApplyToPrettyOption sets a given option to Pretty printer.
90
func (c *CustomPrettyFormatting) ApplyToPrettyOption(cfg *PrettyOptions) {
1✔
91
        if c == nil || c.formatting == nil {
1✔
92
                return
×
93
        }
×
94
        cfg.RenderConfig.Formatting = *c.formatting
1✔
95
}
96

97
// CustomPrettyLayout provides an option to set a custom pretty layout across multiple constructors.
98
type CustomPrettyLayout struct {
99
        layout *style.Layout
100
}
101

102
// WithPrettyLayout sets a custom pretty layout.
103
func WithPrettyLayout(layout *style.Layout) *CustomPrettyLayout {
1✔
104
        return &CustomPrettyLayout{
1✔
105
                layout: layout,
1✔
106
        }
1✔
107
}
1✔
108

109
// ApplyToContainerOption sets a given option for Container.
110
func (c *CustomPrettyLayout) ApplyToContainerOption(cfg *ContainerOptions) {
×
111
        cfg.PrettyOptions = append(cfg.PrettyOptions, c)
×
112
}
×
113

114
// ApplyToPrettyOption sets a given option to Pretty printer.
115
func (c *CustomPrettyLayout) ApplyToPrettyOption(cfg *PrettyOptions) {
1✔
116
        if c == nil || c.layout == nil {
1✔
117
                return
×
118
        }
×
119
        cfg.RenderConfig.Layout = *c.layout
1✔
120
}
121

122
// CustomPrettyStyle provides an option to set a custom pretty style across multiple constructors.
123
type CustomPrettyStyle struct {
124
        cfg *style.Config
125
}
126

127
// WithPrettyStyle sets a custom pretty style.
128
func WithPrettyStyle(cfg *style.Config) *CustomPrettyStyle {
1✔
129
        return &CustomPrettyStyle{
1✔
130
                cfg: cfg,
1✔
131
        }
1✔
132
}
1✔
133

134
// ApplyToContainerOption sets a given option for Container.
135
func (c *CustomPrettyStyle) ApplyToContainerOption(cfg *ContainerOptions) {
1✔
136
        cfg.PrettyOptions = append(cfg.PrettyOptions, c)
1✔
137
}
1✔
138

139
// ApplyToPrettyOption sets a given option to Pretty printer.
140
func (c *CustomPrettyStyle) ApplyToPrettyOption(cfg *PrettyOptions) {
1✔
141
        cfg.RenderConfig = c.cfg
1✔
142
}
1✔
143

144
// PrettyPostRenderHook provides an option to set post render function across multiple constructors.
145
type PrettyPostRenderHook struct {
146
        fn PrettyPostRenderFunc
147
}
148

149
// WithPrettyPostRenderHook sets post render function.
150
func WithPrettyPostRenderHook(fn PrettyPostRenderFunc) *PrettyPostRenderHook {
1✔
151
        return &PrettyPostRenderHook{
1✔
152
                fn: fn,
1✔
153
        }
1✔
154
}
1✔
155

156
// ApplyToContainerOption sets a given option for Container.
157
func (c *PrettyPostRenderHook) ApplyToContainerOption(cfg *ContainerOptions) {
×
158
        cfg.PrettyOptions = append(cfg.PrettyOptions, c)
×
159
}
×
160

161
// ApplyToPrettyOption sets a given option to Pretty printer.
162
func (c *PrettyPostRenderHook) ApplyToPrettyOption(cfg *PrettyOptions) {
1✔
163
        cfg.PostRenderFunc = c.fn
1✔
164
}
1✔
165

166
// PostHook provides an option to set post execution function across multiple constructors.
167
type PostHook struct {
168
        fn PostHookFunc
169
}
170

171
// WithPostHook sets post execution function.
172
func WithPostHook(fn PostHookFunc) *PostHook {
1✔
173
        return &PostHook{
1✔
174
                fn: fn,
1✔
175
        }
1✔
176
}
1✔
177

178
// ApplyToContainerOption sets a given option for Container.
179
func (c *PostHook) ApplyToContainerOption(cfg *ContainerOptions) {
1✔
180
        cfg.PostHookFunc = c.fn
1✔
181
}
1✔
182

183
// EnableUpgradeNotice provides an option to enable upgrade notice across multiple constructors.
184
type EnableUpgradeNotice struct {
185
        upgradeOpts []upgrade.Options
186
        repo        string
187
        owner       string
188
}
189

190
// WithUpgradeNotice enables upgrade notice.
191
func WithUpgradeNotice(owner, repo string, opts ...upgrade.Options) *EnableUpgradeNotice {
1✔
192
        return &EnableUpgradeNotice{
1✔
193
                owner:       owner,
1✔
194
                repo:        repo,
1✔
195
                upgradeOpts: opts,
1✔
196
        }
1✔
197
}
1✔
198

199
// ApplyToContainerOption sets a given option for Container.
200
func (c *EnableUpgradeNotice) ApplyToContainerOption(cfg *ContainerOptions) {
1✔
201
        cfg.UpgradeNotice = upgrade.NewGitHubDetector(c.owner, c.repo, c.upgradeOpts...)
1✔
202
}
1✔
203

204
// WithPrettyStyleFromEnv Load a custom style from environment variable
205
func WithPrettyStyleFromEnv(envVariable string) (*CustomPrettyStyle, error) {
1✔
206
        path := os.Getenv(envVariable)
1✔
207
        options, err := parseConfigFile(path)
1✔
208

1✔
209
        return options, err
1✔
210
}
1✔
211

212
// WithPrettyStyleFile Load a custom style from file
213
func WithPrettyStyleFile(path string) (*CustomPrettyStyle, error) {
1✔
214
        options, err := parseConfigFile(path)
1✔
215

1✔
216
        return options, err
1✔
217
}
1✔
218

219
func parseConfigFile(fileName string) (*CustomPrettyStyle, error) {
1✔
220
        options := &CustomPrettyStyle{cfg: PrettyDefaultRenderConfig()}
1✔
221
        if fileName == "" {
2✔
222
                return options, nil
1✔
223
        }
1✔
224

225
        fileName = filepath.Clean(fileName)
1✔
226
        body, err := os.ReadFile(fileName)
1✔
227
        if err != nil {
1✔
228
                return options, err
×
229
        }
×
230

231
        extension := filepath.Ext(fileName)
1✔
232
        switch extension {
1✔
233
        case ".yml", ".yaml":
1✔
234
                err = yaml.Unmarshal(body, &options.cfg)
1✔
235
        case ".json":
1✔
236
                err = json.Unmarshal(body, &options.cfg)
1✔
237
        }
238

239
        return options, err
1✔
240
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc