Skip to content

Commit 92f5d25

Browse files
authored
✨ Message formatting (#117)
<!-- Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved. SPDX-License-Identifier: Proprietary --> ### Description - Add ways to configure message formatting ### Test Coverage <!-- Please put an `x` in the correct box e.g. `[x]` to indicate the testing coverage of this change. --> - [x] This change is covered by existing or additional automated tests. - [ ] Manual testing has been performed (and evidence provided) as automated testing was not feasible. - [ ] Additional tests are not required for this change (e.g. documentation update).
1 parent 317d084 commit 92f5d25

File tree

7 files changed

+289
-94
lines changed

7 files changed

+289
-94
lines changed

.qlty/qlty.toml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
config_version = "0"
2+
3+
exclude_patterns = [
4+
"**/node_modules/**",
5+
"**/vendor/**",
6+
"config/**",
7+
"db/**",
8+
"dist/**",
9+
"features/**",
10+
"docs/**",
11+
"changes/**",
12+
"**/node_modules/**",
13+
"script/**",
14+
"**/spec/**",
15+
"**/mock/**",
16+
"**/mocks/**",
17+
"**/test/**",
18+
"**/tests/**",
19+
"**/vendor/**",
20+
]
21+
22+
test_patterns = ["**/*_test.go", "**/spec/**"]
23+
24+
[smells.return_statements]
25+
enabled = false
26+
27+
[[source]]
28+
name = "default"
29+
default = true
30+
31+
[[plugin]]
32+
name = "actionlint"
33+
34+
[[plugin]]
35+
name = "checkov"
36+
version = "3.2.49"
37+
38+
[[plugin]]
39+
name = "markdownlint"
40+
version = "0.31.1"
41+
42+
[[plugin]]
43+
name = "osv-scanner"
44+
45+
[[plugin]]
46+
name = "prettier"
47+
version = "2.8.4"
48+
49+
[[plugin]]
50+
name = "ripgrep"
51+
52+
[[plugin]]
53+
name = "shellcheck"
54+
version = "0.9.0"
55+
56+
[[plugin]]
57+
name = "shfmt"
58+
59+
[[plugin]]
60+
name = "trivy"
61+
62+
[[plugin]]
63+
name = "trufflehog"
64+
65+
[[plugin]]
66+
name = "yamllint"

changes/20250716161425.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:sparkles: Make the formatting of messages optional

utils/go.mod

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ module github.com/ARM-software/embedded-development-services-client-utils/utils
33
go 1.24
44

55
require (
6-
github.com/ARM-software/embedded-development-services-client/client v1.60.0
7-
github.com/ARM-software/golang-utils/utils v1.101.0
6+
github.com/ARM-software/embedded-development-services-client/client v1.65.0
7+
github.com/ARM-software/golang-utils/utils v1.106.0
88
github.com/go-faker/faker/v4 v4.6.1
99
github.com/go-logr/logr v1.4.3
1010
github.com/perimeterx/marshmallow v1.1.5
1111
github.com/stretchr/testify v1.10.0
1212
go.uber.org/atomic v1.11.0
1313
go.uber.org/goleak v1.3.0
1414
go.uber.org/mock v0.5.2
15-
golang.org/x/sync v0.15.0
15+
golang.org/x/sync v0.16.0
1616
)
1717

1818
require (
@@ -34,13 +34,13 @@ require (
3434
github.com/go-logr/zapr v1.3.0 // indirect
3535
github.com/go-ole/go-ole v1.3.0 // indirect
3636
github.com/go-ozzo/ozzo-validation/v4 v4.3.0 // indirect
37-
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
37+
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
3838
github.com/gogs/chardet v0.0.0-20211120154057-b7413eaefb8f // indirect
3939
github.com/google/cabbie v1.0.5 // indirect
4040
github.com/google/glazier v0.0.0-20250102133340-c90d5bf10f5f // indirect
4141
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
4242
github.com/hashicorp/go-hclog v1.6.3 // indirect
43-
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
43+
github.com/hashicorp/go-retryablehttp v0.7.8 // indirect
4444
github.com/iamacarpet/go-win64api v0.0.0-20240507095429-873e84e85847 // indirect
4545
github.com/joho/godotenv v1.5.1 // indirect
4646
github.com/josharian/intern v1.0.0 // indirect
@@ -59,7 +59,7 @@ require (
5959
github.com/sagikazarmark/locafero v0.7.0 // indirect
6060
github.com/sasha-s/go-deadlock v0.3.5 // indirect
6161
github.com/scjalliance/comshim v0.0.0-20240712181150-e070933cb68e // indirect
62-
github.com/shirou/gopsutil/v4 v4.25.5 // indirect
62+
github.com/shirou/gopsutil/v4 v4.25.6 // indirect
6363
github.com/sirupsen/logrus v1.9.3 // indirect
6464
github.com/sourcegraph/conc v0.3.0 // indirect
6565
github.com/spaolacci/murmur3 v1.1.0 // indirect
@@ -74,13 +74,13 @@ require (
7474
github.com/zailic/slogr v0.0.2-alpha // indirect
7575
go.uber.org/multierr v1.11.0 // indirect
7676
go.uber.org/zap v1.27.0 // indirect
77-
golang.org/x/crypto v0.39.0 // indirect
77+
golang.org/x/crypto v0.40.0 // indirect
7878
golang.org/x/exp v0.0.0-20250506013437-ce4c2cf36ca6 // indirect
79-
golang.org/x/mod v0.25.0 // indirect
79+
golang.org/x/mod v0.26.0 // indirect
8080
golang.org/x/oauth2 v0.30.0 // indirect
81-
golang.org/x/sys v0.33.0 // indirect
82-
golang.org/x/text v0.26.0 // indirect
83-
golang.org/x/tools v0.33.0 // indirect
81+
golang.org/x/sys v0.34.0 // indirect
82+
golang.org/x/text v0.27.0 // indirect
83+
golang.org/x/tools v0.34.0 // indirect
8484
gopkg.in/toast.v1 v1.0.0-20180812000517-0a84660828b2 // indirect
8585
gopkg.in/yaml.v3 v3.0.1 // indirect
8686
)

utils/go.sum

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
bitbucket.org/creachadair/stringset v0.0.9/go.mod h1:t+4WcQ4+PXTa8aQdNKe40ZP6iwesoMFWAxPGd3UGjyY=
2-
github.com/ARM-software/embedded-development-services-client/client v1.60.0 h1:/fzTKKNuWVq3GXmqbn3rhZ5U83DHsY1DKUqSwPtTLeM=
3-
github.com/ARM-software/embedded-development-services-client/client v1.60.0/go.mod h1:cYEKKuvkAllhxAjFq4yGGQHhNwcqeF8eVmuIUrlza3s=
4-
github.com/ARM-software/golang-utils/utils v1.101.0 h1:ntEDWPMypTMxKCOuVwnEu1HPq253nkC4UsKGk60YWW4=
5-
github.com/ARM-software/golang-utils/utils v1.101.0/go.mod h1:DRvfproHcka6bh9qREP5cBioSStjV6IHgAf48cSHIgA=
2+
github.com/ARM-software/embedded-development-services-client/client v1.65.0 h1:hXeGC5LEpbaJqp72uH69L1k1CgiTgx2gpsnrSSbkoxI=
3+
github.com/ARM-software/embedded-development-services-client/client v1.65.0/go.mod h1:scqEY8RUFLhiUXIiDZuBIqMcFatupus8R1AqALmZv2E=
4+
github.com/ARM-software/golang-utils/utils v1.106.0 h1:7mTKDvGb7mNGHPevcCd1ZRkixV08lI/Wgzy43BwWKhs=
5+
github.com/ARM-software/golang-utils/utils v1.106.0/go.mod h1:/4GqA4gT1M18ymFu3jArDvaGdTKJ5MMVbptS3RTgH6k=
66
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
77
github.com/OneOfOne/xxhash v1.2.8 h1:31czK/TI9sNkxIKfaUfGlU47BAxQ0ztGgd9vPyqimf8=
88
github.com/OneOfOne/xxhash v1.2.8/go.mod h1:eZbhyaAYD41SGSSsnmcpxVoRiQ/MPUTjUdIIOT9Um7Q=
@@ -61,8 +61,8 @@ github.com/go-ozzo/ozzo-validation/v4 v4.3.0 h1:byhDUpfEwjsVQb1vBunvIjh2BHQ9ead5
6161
github.com/go-ozzo/ozzo-validation/v4 v4.3.0/go.mod h1:2NKgrcHl3z6cJs+3Oo940FPRiTzuqKbvfrL2RxCj6Ew=
6262
github.com/go-test/deep v1.0.8 h1:TDsG77qcSprGbC6vTN8OuXp5g+J+b5Pcguhf7Zt61VM=
6363
github.com/go-test/deep v1.0.8/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
64-
github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIxtHqx8aGss=
65-
github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
64+
github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs=
65+
github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
6666
github.com/godbus/dbus v4.1.0+incompatible/go.mod h1:/YcGZj5zSblfDWMMoOzV4fas9FZnQYTkDnsGvmh2Grw=
6767
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
6868
github.com/gofrs/uuid/v5 v5.3.2 h1:2jfO8j3XgSwlz/wHqemAEugfnTlikAYHhnqQ8Xh4fE0=
@@ -103,8 +103,8 @@ github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9n
103103
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
104104
github.com/hashicorp/go-hclog v1.6.3 h1:Qr2kF+eVWjTiYmU7Y31tYlP1h0q/X3Nl3tPGdaB11/k=
105105
github.com/hashicorp/go-hclog v1.6.3/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M=
106-
github.com/hashicorp/go-retryablehttp v0.7.7 h1:C8hUCYzor8PIfXHa4UrZkU4VvK8o9ISHxT2Q8+VepXU=
107-
github.com/hashicorp/go-retryablehttp v0.7.7/go.mod h1:pkQpWZeYWskR+D1tR2O5OcBFOxfA7DoAO6xtkuQnHTk=
106+
github.com/hashicorp/go-retryablehttp v0.7.8 h1:ylXZWnqa7Lhqpk0L1P1LzDtGcCR0rPVUrx/c8Unxc48=
107+
github.com/hashicorp/go-retryablehttp v0.7.8/go.mod h1:rjiScheydd+CxvumBsIrFKlx3iS0jrZ7LvzFGFmuKbw=
108108
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
109109
github.com/iamacarpet/go-win64api v0.0.0-20210311141720-fe38760bed28/go.mod h1:oGJx9dz0Ny7HC7U55RZ0Smd6N9p3hXP/+hOFtuYrAxM=
110110
github.com/iamacarpet/go-win64api v0.0.0-20240507095429-873e84e85847 h1:cRHZFGwIDgQlr9abL/P93JXR7pYxzvf0xAIt0xzwrh0=
@@ -170,8 +170,8 @@ github.com/sasha-s/go-deadlock v0.3.5/go.mod h1:bugP6EGbdGYObIlx7pUZtWqlvo8k9H6v
170170
github.com/scjalliance/comshim v0.0.0-20190308082608-cf06d2532c4e/go.mod h1:9Tc1SKnfACJb9N7cw2eyuI6xzy845G7uZONBsi5uPEA=
171171
github.com/scjalliance/comshim v0.0.0-20240712181150-e070933cb68e h1:DHQTQhd+UU97hLiIaH5oDf61NqH6iBoHBgZoeWc1olc=
172172
github.com/scjalliance/comshim v0.0.0-20240712181150-e070933cb68e/go.mod h1:RS825256UevDX5P1oImjU4qUY3fwF6HDLHUD+Zbbd/A=
173-
github.com/shirou/gopsutil/v4 v4.25.5 h1:rtd9piuSMGeU8g1RMXjZs9y9luK5BwtnG7dZaQUJAsc=
174-
github.com/shirou/gopsutil/v4 v4.25.5/go.mod h1:PfybzyydfZcN+JMMjkF6Zb8Mq1A/VcogFFg7hj50W9c=
173+
github.com/shirou/gopsutil/v4 v4.25.6 h1:kLysI2JsKorfaFPcYmcJqbzROzsBWEOAtw6A7dIfqXs=
174+
github.com/shirou/gopsutil/v4 v4.25.6/go.mod h1:PfybzyydfZcN+JMMjkF6Zb8Mq1A/VcogFFg7hj50W9c=
175175
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
176176
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
177177
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
@@ -216,12 +216,12 @@ go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
216216
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
217217
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
218218
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
219-
golang.org/x/crypto v0.39.0 h1:SHs+kF4LP+f+p14esP5jAoDpHU8Gu/v9lFRK6IT5imM=
220-
golang.org/x/crypto v0.39.0/go.mod h1:L+Xg3Wf6HoL4Bn4238Z6ft6KfEpN0tJGo53AAPC632U=
219+
golang.org/x/crypto v0.40.0 h1:r4x+VvoG5Fm+eJcxMaY8CQM7Lb0l1lsmjGBQ6s8BfKM=
220+
golang.org/x/crypto v0.40.0/go.mod h1:Qr1vMER5WyS2dfPHAlsOj01wgLbsyWtFn/aY+5+ZdxY=
221221
golang.org/x/exp v0.0.0-20250506013437-ce4c2cf36ca6 h1:y5zboxd6LQAqYIhHnB48p0ByQ/GnQx2BE33L8BOHQkI=
222222
golang.org/x/exp v0.0.0-20250506013437-ce4c2cf36ca6/go.mod h1:U6Lno4MTRCDY+Ba7aCcauB9T60gsv5s4ralQzP72ZoQ=
223-
golang.org/x/mod v0.25.0 h1:n7a+ZbQKQA/Ysbyb0/6IbB1H/X41mKgbhfv7AfG/44w=
224-
golang.org/x/mod v0.25.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=
223+
golang.org/x/mod v0.26.0 h1:EGMPT//Ezu+ylkCijjPc+f4Aih7sZvaAr+O3EHBxvZg=
224+
golang.org/x/mod v0.26.0/go.mod h1:/j6NAhSk8iQ723BGAUyoAcn7SlD7s15Dp9Nd/SfeaFQ=
225225
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
226226
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
227227
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
@@ -230,8 +230,8 @@ golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qx
230230
golang.org/x/oauth2 v0.30.0 h1:dnDm7JmhM45NNpd8FDDeLhK6FwqbOf4MLCM9zb1BOHI=
231231
golang.org/x/oauth2 v0.30.0/go.mod h1:B++QgG3ZKulg6sRPGD/mqlHQs5rB3Ml9erfeDY7xKlU=
232232
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
233-
golang.org/x/sync v0.15.0 h1:KWH3jNZsfyT6xfAfKiz6MRNmd46ByHDYaZ7KSkCtdW8=
234-
golang.org/x/sync v0.15.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
233+
golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw=
234+
golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
235235
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
236236
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
237237
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -259,17 +259,17 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
259259
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
260260
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
261261
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
262-
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
263-
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
262+
golang.org/x/sys v0.34.0 h1:H5Y5sJ2L2JRdyv7ROF1he/lPdvFsd0mJHFw2ThKHxLA=
263+
golang.org/x/sys v0.34.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
264264
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
265265
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
266266
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
267267
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
268-
golang.org/x/text v0.26.0 h1:P42AVeLghgTYr4+xUnTRKDMqpar+PtX7KWuNQL21L8M=
269-
golang.org/x/text v0.26.0/go.mod h1:QK15LZJUUQVJxhz7wXgxSy/CJaTFjd0G+YLonydOVQA=
268+
golang.org/x/text v0.27.0 h1:4fGWRpyh641NLlecmyl4LOe6yDdfaYNrGb2zdfo4JV4=
269+
golang.org/x/text v0.27.0/go.mod h1:1D28KMCvyooCX9hBiosv5Tz/+YLxj0j7XhWjpSUF7CU=
270270
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
271-
golang.org/x/tools v0.33.0 h1:4qz2S3zmRxbGIhDIAgjxvFutSvH5EfnsYrRBj0UI0bc=
272-
golang.org/x/tools v0.33.0/go.mod h1:CIJMaWEY88juyUfo7UbgPqbC8rU2OqfAV1h2Qp0oMYI=
271+
golang.org/x/tools v0.34.0 h1:qIpSLOxeCYGg9TrcJokLBG4KFA6d795g0xkBkiESGlo=
272+
golang.org/x/tools v0.34.0/go.mod h1:pAP9OwEaY1CAW3HOmg3hLZC5Z0CCmzjAF2UQMSqNARg=
273273
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
274274
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
275275
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=

utils/messages/format.go

Lines changed: 91 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,52 +11,121 @@ import (
1111
"strings"
1212

1313
"github.com/ARM-software/golang-utils/utils/commonerrors"
14+
"github.com/ARM-software/golang-utils/utils/field"
1415
)
1516

16-
// FormatMessage formats a job message.
17-
func FormatMessage(msg IMessage) (s string, err error) {
17+
type FormatterOptions struct {
18+
source bool
19+
timestamp bool
20+
severity bool
21+
}
22+
23+
type FormatterOption func(*FormatterOptions) *FormatterOptions
24+
25+
// WithSource includes information about the source of the message.
26+
var WithSource FormatterOption = func(o *FormatterOptions) *FormatterOptions {
27+
if o == nil {
28+
return o
29+
}
30+
o.source = true
31+
return o
32+
}
33+
34+
// WithTimeStamp includes the timestamp of the message.
35+
var WithTimeStamp FormatterOption = func(o *FormatterOptions) *FormatterOptions {
36+
if o == nil {
37+
return o
38+
}
39+
o.timestamp = true
40+
return o
41+
}
42+
43+
// WithSeverity includes the severity of the message.
44+
var WithSeverity FormatterOption = func(o *FormatterOptions) *FormatterOptions {
45+
if o == nil {
46+
return o
47+
}
48+
o.severity = true
49+
return o
50+
}
51+
52+
type MessageFormatter struct {
53+
options FormatterOptions
54+
}
55+
56+
func (f *MessageFormatter) FormatMessage(msg IMessage) (s string, err error) {
1857
if msg == nil {
19-
err = fmt.Errorf("%w: missing message", commonerrors.ErrUndefined)
58+
err = commonerrors.UndefinedVariable("message")
2059
return
2160
}
2261
var b strings.Builder
23-
if source, ok := msg.GetSourceOk(); ok {
24-
_, err = b.WriteString(fmt.Sprintf("[%s] ", *source))
25-
if err != nil {
26-
err = fmt.Errorf("%w: %v", commonerrors.ErrUnexpected, err.Error())
27-
return
62+
if f.options.source {
63+
if source, ok := msg.GetSourceOk(); ok {
64+
_, err = b.WriteString(fmt.Sprintf("[%s] ", field.OptionalString(source, "")))
65+
if err != nil {
66+
err = commonerrors.WrapError(commonerrors.ErrUnexpected, err, "")
67+
return
68+
}
2869
}
2970
}
30-
if ctime, ok := msg.GetCtimeOk(); ok {
31-
_, err = b.WriteString(fmt.Sprintf("(%s) ", *ctime))
32-
if err != nil {
33-
err = fmt.Errorf("%w: %v", commonerrors.ErrUnexpected, err.Error())
34-
return
71+
if f.options.timestamp {
72+
if ctime, ok := msg.GetCtimeOk(); ok {
73+
_, err = b.WriteString(fmt.Sprintf("(%s) ", *ctime))
74+
if err != nil {
75+
err = commonerrors.WrapError(commonerrors.ErrUnexpected, err, "")
76+
return
77+
}
3578
}
3679
}
37-
if severity, ok := msg.GetSeverityOk(); ok {
38-
_, err = b.WriteString(fmt.Sprintf("%s ", *severity))
39-
if err != nil {
40-
err = fmt.Errorf("%w: %v", commonerrors.ErrUnexpected, err.Error())
41-
return
80+
if f.options.severity {
81+
if severity, ok := msg.GetSeverityOk(); ok {
82+
_, err = b.WriteString(fmt.Sprintf("%s ", field.OptionalString(severity, "")))
83+
if err != nil {
84+
err = commonerrors.WrapError(commonerrors.ErrUnexpected, err, "")
85+
return
86+
}
4287
}
4388
}
4489

4590
if message, ok := msg.GetMessageOk(); ok {
4691
if b.Len() > 0 {
47-
_, err = b.WriteString(fmt.Sprintf(": %s", *message))
92+
_, err = b.WriteString(fmt.Sprintf(": %s", field.OptionalString(message, "")))
4893
if err != nil {
49-
err = fmt.Errorf("%w: %v", commonerrors.ErrUnexpected, err.Error())
94+
err = commonerrors.WrapError(commonerrors.ErrUnexpected, err, "")
5095
return
5196
}
5297
} else {
53-
_, err = b.WriteString(*message)
98+
_, err = b.WriteString(field.OptionalString(message, ""))
5499
if err != nil {
55-
err = fmt.Errorf("%w: %v", commonerrors.ErrUnexpected, err.Error())
100+
err = commonerrors.WrapError(commonerrors.ErrUnexpected, err, "")
56101
return
57102
}
58103
}
59104
}
60105
s = b.String()
61106
return
62107
}
108+
109+
// NewMessageFormatter creates a formatter for messages
110+
func NewMessageFormatter(option ...FormatterOption) *MessageFormatter {
111+
options := &FormatterOptions{}
112+
for i := range option {
113+
options = option[i](options)
114+
}
115+
return &MessageFormatter{options: *options}
116+
}
117+
118+
// DefaultMessageFormatter returns a default message formatter.
119+
func DefaultMessageFormatter() *MessageFormatter {
120+
return NewMessageFormatter(WithSource, WithTimeStamp, WithSeverity)
121+
}
122+
123+
// FormatMessageWithOptions formats a job message with formatting options.
124+
func FormatMessageWithOptions(msg IMessage, option ...FormatterOption) (s string, err error) {
125+
return NewMessageFormatter(option...).FormatMessage(msg)
126+
}
127+
128+
// FormatMessage formats a job message.
129+
func FormatMessage(msg IMessage) (s string, err error) {
130+
return FormatMessageWithOptions(msg, WithSource, WithTimeStamp, WithSeverity)
131+
}

0 commit comments

Comments
 (0)