Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve toml formatting #758

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading