Skip to content

Commit eacb7dd

Browse files
authored
feat: improve toml formatting (#758)
This PR changes the TOML library from `pelletier/go-toml/v2` to `BurntSushi/toml`. This improves the formatting of TOML files by using indents and also uses `time.Duration`'s string values instead of writing nanoseconds into the config file. ### Config format example Before: ```toml active_context = 'test_context' [preferences] debug = true poll_interval = 1234000000 [[contexts]] name = 'test_context' token = 'super secret token' [contexts.preferences] array_option = ['1', '2', '3'] endpoint = 'https://test-endpoint.com' quiet = true [contexts.preferences.nested] array_option = ['1', '2', '3'] [[contexts]] name = 'other_context' token = 'another super secret token' [contexts.preferences] poll_interval = 1234000000 ``` After: ```toml active_context = "test_context" [preferences] debug = true poll_interval = "1.234s" [[contexts]] name = "test_context" token = "super secret token" [contexts.preferences] array_option = ["1", "2", "3"] endpoint = "https://test-endpoint.com" quiet = true [contexts.preferences.nested] array_option = ["1", "2", "3"] [[contexts]] name = "other_context" token = "another super secret token" [contexts.preferences] poll_interval = "1.234s" ``` Contexts are now grouped together instead of there being exactly one newline between every section, regardless of being in another context or not. Also, nested sections are now indented, which helps with understanding the structure. ### Binary size Before: ``` ➜ cli git:(21e342b) GOARCH=amd64 GOOS=linux go build -o hcloud cmd/hcloud/main.go ➜ cli git:(21e342b) wc -c hcloud 20606086 hcloud ``` After: ``` ➜ cli git:(d336765) GOARCH=amd64 GOOS=linux go build -o hcloud cmd/hcloud/main.go ➜ cli git:(d336765) wc -c hcloud 20890490 hcloud ``` The binary size increases by ~1.4%, which is negligible.
1 parent d1c6678 commit eacb7dd

File tree

8 files changed

+375
-445
lines changed

8 files changed

+375
-445
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/hetznercloud/cli
33
go 1.21
44

55
require (
6+
github.com/BurntSushi/toml v1.3.2
67
github.com/boumenot/gocover-cobertura v1.2.0
78
github.com/cheggaaa/pb/v3 v3.1.5
89
github.com/dustin/go-humanize v1.0.1
@@ -13,7 +14,6 @@ require (
1314
github.com/guptarohit/asciigraph v0.7.1
1415
github.com/hetznercloud/hcloud-go/v2 v2.9.0
1516
github.com/jedib0t/go-pretty/v6 v6.5.9
16-
github.com/pelletier/go-toml/v2 v2.2.2
1717
github.com/spf13/cobra v1.8.0
1818
github.com/spf13/pflag v1.0.5
1919
github.com/spf13/viper v1.18.2
@@ -35,6 +35,7 @@ require (
3535
github.com/mattn/go-isatty v0.0.20 // indirect
3636
github.com/mattn/go-runewidth v0.0.15 // indirect
3737
github.com/mitchellh/mapstructure v1.5.0 // indirect
38+
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
3839
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
3940
github.com/prometheus/client_golang v1.19.1 // indirect
4041
github.com/prometheus/client_model v0.5.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
2+
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
13
github.com/VividCortex/ewma v1.2.0 h1:f58SaIzcDXrSy3kWaHNvuJgJ3Nmz59Zji6XoJR/q1ow=
24
github.com/VividCortex/ewma v1.2.0/go.mod h1:nz4BbCtbLyFDeC9SUHbtcT5644juEuWfUAUnGx7j5l4=
35
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=

internal/cmd/config/add_test.go

Lines changed: 93 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -32,30 +32,27 @@ func TestAdd(t *testing.T) {
3232
)
3333
defer deleteNestedArrayOption()
3434

35-
testConfig := `active_context = 'test_context'
35+
testConfig := `active_context = "test_context"
3636
3737
[preferences]
38-
debug = true
39-
poll_interval = 1234000000
38+
debug = true
39+
poll_interval = "1.234s"
4040
4141
[[contexts]]
42-
name = 'test_context'
43-
token = 'super secret token'
44-
45-
[contexts.preferences]
46-
array_option = ['1', '2', '3']
47-
endpoint = 'https://test-endpoint.com'
48-
quiet = true
49-
50-
[contexts.preferences.nested]
51-
array_option = ['1', '2', '3']
42+
name = "test_context"
43+
token = "super secret token"
44+
[contexts.preferences]
45+
array_option = ["1", "2", "3"]
46+
endpoint = "https://test-endpoint.com"
47+
quiet = true
48+
[contexts.preferences.nested]
49+
array_option = ["1", "2", "3"]
5250
5351
[[contexts]]
54-
name = 'other_context'
55-
token = 'another super secret token'
56-
57-
[contexts.preferences]
58-
poll_interval = 1234000000
52+
name = "other_context"
53+
token = "another super secret token"
54+
[contexts.preferences]
55+
poll_interval = "1.234s"
5956
`
6057

6158
type testCase struct {
@@ -74,93 +71,84 @@ poll_interval = 1234000000
7471
args: []string{"array-option", "a", "b", "c"},
7572
config: testConfig,
7673
expOut: `Added '[a b c]' to 'array-option' in context 'test_context'
77-
active_context = 'test_context'
74+
active_context = "test_context"
7875
7976
[preferences]
80-
debug = true
81-
poll_interval = 1234000000
77+
debug = true
78+
poll_interval = "1.234s"
8279
8380
[[contexts]]
84-
name = 'test_context'
85-
token = 'super secret token'
86-
87-
[contexts.preferences]
88-
array_option = ['1', '2', '3', 'a', 'b', 'c']
89-
endpoint = 'https://test-endpoint.com'
90-
quiet = true
91-
92-
[contexts.preferences.nested]
93-
array_option = ['1', '2', '3']
81+
name = "test_context"
82+
token = "super secret token"
83+
[contexts.preferences]
84+
array_option = ["1", "2", "3", "a", "b", "c"]
85+
endpoint = "https://test-endpoint.com"
86+
quiet = true
87+
[contexts.preferences.nested]
88+
array_option = ["1", "2", "3"]
9489
9590
[[contexts]]
96-
name = 'other_context'
97-
token = 'another super secret token'
98-
99-
[contexts.preferences]
100-
poll_interval = 1234000000
91+
name = "other_context"
92+
token = "another super secret token"
93+
[contexts.preferences]
94+
poll_interval = "1.234s"
10195
`,
10296
},
10397
{
10498
name: "add to nested",
10599
args: []string{"nested.array-option", "a", "b", "c"},
106100
config: testConfig,
107101
expOut: `Added '[a b c]' to 'nested.array-option' in context 'test_context'
108-
active_context = 'test_context'
102+
active_context = "test_context"
109103
110104
[preferences]
111-
debug = true
112-
poll_interval = 1234000000
105+
debug = true
106+
poll_interval = "1.234s"
113107
114108
[[contexts]]
115-
name = 'test_context'
116-
token = 'super secret token'
117-
118-
[contexts.preferences]
119-
array_option = ['1', '2', '3']
120-
endpoint = 'https://test-endpoint.com'
121-
quiet = true
122-
123-
[contexts.preferences.nested]
124-
array_option = ['1', '2', '3', 'a', 'b', 'c']
109+
name = "test_context"
110+
token = "super secret token"
111+
[contexts.preferences]
112+
array_option = ["1", "2", "3"]
113+
endpoint = "https://test-endpoint.com"
114+
quiet = true
115+
[contexts.preferences.nested]
116+
array_option = ["1", "2", "3", "a", "b", "c"]
125117
126118
[[contexts]]
127-
name = 'other_context'
128-
token = 'another super secret token'
129-
130-
[contexts.preferences]
131-
poll_interval = 1234000000
119+
name = "other_context"
120+
token = "another super secret token"
121+
[contexts.preferences]
122+
poll_interval = "1.234s"
132123
`,
133124
},
134125
{
135126
name: "global add to empty",
136127
args: []string{"--global", "array-option", "a", "b", "c"},
137128
config: testConfig,
138129
expOut: `Added '[a b c]' to 'array-option' globally
139-
active_context = 'test_context'
130+
active_context = "test_context"
140131
141132
[preferences]
142-
array_option = ['a', 'b', 'c']
143-
debug = true
144-
poll_interval = 1234000000
133+
array_option = ["a", "b", "c"]
134+
debug = true
135+
poll_interval = "1.234s"
145136
146137
[[contexts]]
147-
name = 'test_context'
148-
token = 'super secret token'
149-
150-
[contexts.preferences]
151-
array_option = ['1', '2', '3']
152-
endpoint = 'https://test-endpoint.com'
153-
quiet = true
154-
155-
[contexts.preferences.nested]
156-
array_option = ['1', '2', '3']
138+
name = "test_context"
139+
token = "super secret token"
140+
[contexts.preferences]
141+
array_option = ["1", "2", "3"]
142+
endpoint = "https://test-endpoint.com"
143+
quiet = true
144+
[contexts.preferences.nested]
145+
array_option = ["1", "2", "3"]
157146
158147
[[contexts]]
159-
name = 'other_context'
160-
token = 'another super secret token'
161-
162-
[contexts.preferences]
163-
poll_interval = 1234000000
148+
name = "other_context"
149+
token = "another super secret token"
150+
[contexts.preferences]
151+
poll_interval = "1.234s"
164152
`,
165153
},
166154
{
@@ -169,31 +157,28 @@ poll_interval = 1234000000
169157
config: testConfig,
170158
expErr: "Warning: some values were already present or duplicate\n",
171159
expOut: `Added '[a b c]' to 'array-option' globally
172-
active_context = 'test_context'
160+
active_context = "test_context"
173161
174162
[preferences]
175-
array_option = ['a', 'b', 'c']
176-
debug = true
177-
poll_interval = 1234000000
163+
array_option = ["a", "b", "c"]
164+
debug = true
165+
poll_interval = "1.234s"
178166
179167
[[contexts]]
180-
name = 'test_context'
181-
token = 'super secret token'
182-
183-
[contexts.preferences]
184-
array_option = ['1', '2', '3']
185-
endpoint = 'https://test-endpoint.com'
186-
quiet = true
187-
188-
[contexts.preferences.nested]
189-
array_option = ['1', '2', '3']
168+
name = "test_context"
169+
token = "super secret token"
170+
[contexts.preferences]
171+
array_option = ["1", "2", "3"]
172+
endpoint = "https://test-endpoint.com"
173+
quiet = true
174+
[contexts.preferences.nested]
175+
array_option = ["1", "2", "3"]
190176
191177
[[contexts]]
192-
name = 'other_context'
193-
token = 'another super secret token'
194-
195-
[contexts.preferences]
196-
poll_interval = 1234000000
178+
name = "other_context"
179+
token = "another super secret token"
180+
[contexts.preferences]
181+
poll_interval = "1.234s"
197182
`,
198183
},
199184
{
@@ -207,31 +192,28 @@ poll_interval = 1234000000
207192
args: []string{"array-option", "I", "II", "III"},
208193
config: testConfig,
209194
expOut: `Added '[I II III]' to 'array-option' in context 'other_context'
210-
active_context = 'test_context'
195+
active_context = "test_context"
211196
212197
[preferences]
213-
debug = true
214-
poll_interval = 1234000000
198+
debug = true
199+
poll_interval = "1.234s"
215200
216201
[[contexts]]
217-
name = 'test_context'
218-
token = 'super secret token'
219-
220-
[contexts.preferences]
221-
array_option = ['1', '2', '3']
222-
endpoint = 'https://test-endpoint.com'
223-
quiet = true
224-
225-
[contexts.preferences.nested]
226-
array_option = ['1', '2', '3']
202+
name = "test_context"
203+
token = "super secret token"
204+
[contexts.preferences]
205+
array_option = ["1", "2", "3"]
206+
endpoint = "https://test-endpoint.com"
207+
quiet = true
208+
[contexts.preferences.nested]
209+
array_option = ["1", "2", "3"]
227210
228211
[[contexts]]
229-
name = 'other_context'
230-
token = 'another super secret token'
231-
232-
[contexts.preferences]
233-
array_option = ['I', 'II', 'III']
234-
poll_interval = 1234000000
212+
name = "other_context"
213+
token = "another super secret token"
214+
[contexts.preferences]
215+
array_option = ["I", "II", "III"]
216+
poll_interval = "1.234s"
235217
`,
236218
},
237219
}

0 commit comments

Comments
 (0)