Skip to content

Commit

Permalink
feat: improve toml formatting (#758)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
phm07 committed Jun 6, 2024
1 parent d1c6678 commit eacb7dd
Show file tree
Hide file tree
Showing 8 changed files with 375 additions and 445 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/hetznercloud/cli
go 1.21

require (
github.com/BurntSushi/toml v1.3.2
github.com/boumenot/gocover-cobertura v1.2.0
github.com/cheggaaa/pb/v3 v3.1.5
github.com/dustin/go-humanize v1.0.1
Expand All @@ -13,7 +14,6 @@ require (
github.com/guptarohit/asciigraph v0.7.1
github.com/hetznercloud/hcloud-go/v2 v2.9.0
github.com/jedib0t/go-pretty/v6 v6.5.9
github.com/pelletier/go-toml/v2 v2.2.2
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.18.2
Expand All @@ -35,6 +35,7 @@ require (
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_golang v1.19.1 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/VividCortex/ewma v1.2.0 h1:f58SaIzcDXrSy3kWaHNvuJgJ3Nmz59Zji6XoJR/q1ow=
github.com/VividCortex/ewma v1.2.0/go.mod h1:nz4BbCtbLyFDeC9SUHbtcT5644juEuWfUAUnGx7j5l4=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
Expand Down
204 changes: 93 additions & 111 deletions internal/cmd/config/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,27 @@ func TestAdd(t *testing.T) {
)
defer deleteNestedArrayOption()

testConfig := `active_context = 'test_context'
testConfig := `active_context = "test_context"
[preferences]
debug = true
poll_interval = 1234000000
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']
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
name = "other_context"
token = "another super secret token"
[contexts.preferences]
poll_interval = "1.234s"
`

type testCase struct {
Expand All @@ -74,93 +71,84 @@ poll_interval = 1234000000
args: []string{"array-option", "a", "b", "c"},
config: testConfig,
expOut: `Added '[a b c]' to 'array-option' in context 'test_context'
active_context = 'test_context'
active_context = "test_context"
[preferences]
debug = true
poll_interval = 1234000000
debug = true
poll_interval = "1.234s"
[[contexts]]
name = 'test_context'
token = 'super secret token'
[contexts.preferences]
array_option = ['1', '2', '3', 'a', 'b', 'c']
endpoint = 'https://test-endpoint.com'
quiet = true
[contexts.preferences.nested]
array_option = ['1', '2', '3']
name = "test_context"
token = "super secret token"
[contexts.preferences]
array_option = ["1", "2", "3", "a", "b", "c"]
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
name = "other_context"
token = "another super secret token"
[contexts.preferences]
poll_interval = "1.234s"
`,
},
{
name: "add to nested",
args: []string{"nested.array-option", "a", "b", "c"},
config: testConfig,
expOut: `Added '[a b c]' to 'nested.array-option' in context 'test_context'
active_context = 'test_context'
active_context = "test_context"
[preferences]
debug = true
poll_interval = 1234000000
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', 'a', 'b', 'c']
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", "a", "b", "c"]
[[contexts]]
name = 'other_context'
token = 'another super secret token'
[contexts.preferences]
poll_interval = 1234000000
name = "other_context"
token = "another super secret token"
[contexts.preferences]
poll_interval = "1.234s"
`,
},
{
name: "global add to empty",
args: []string{"--global", "array-option", "a", "b", "c"},
config: testConfig,
expOut: `Added '[a b c]' to 'array-option' globally
active_context = 'test_context'
active_context = "test_context"
[preferences]
array_option = ['a', 'b', 'c']
debug = true
poll_interval = 1234000000
array_option = ["a", "b", "c"]
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']
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
name = "other_context"
token = "another super secret token"
[contexts.preferences]
poll_interval = "1.234s"
`,
},
{
Expand All @@ -169,31 +157,28 @@ poll_interval = 1234000000
config: testConfig,
expErr: "Warning: some values were already present or duplicate\n",
expOut: `Added '[a b c]' to 'array-option' globally
active_context = 'test_context'
active_context = "test_context"
[preferences]
array_option = ['a', 'b', 'c']
debug = true
poll_interval = 1234000000
array_option = ["a", "b", "c"]
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']
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
name = "other_context"
token = "another super secret token"
[contexts.preferences]
poll_interval = "1.234s"
`,
},
{
Expand All @@ -207,31 +192,28 @@ poll_interval = 1234000000
args: []string{"array-option", "I", "II", "III"},
config: testConfig,
expOut: `Added '[I II III]' to 'array-option' in context 'other_context'
active_context = 'test_context'
active_context = "test_context"
[preferences]
debug = true
poll_interval = 1234000000
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']
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]
array_option = ['I', 'II', 'III']
poll_interval = 1234000000
name = "other_context"
token = "another super secret token"
[contexts.preferences]
array_option = ["I", "II", "III"]
poll_interval = "1.234s"
`,
},
}
Expand Down
Loading

0 comments on commit eacb7dd

Please sign in to comment.