From 33d9c1dd571d731c23b3f818258c0e9a89638f1e Mon Sep 17 00:00:00 2001 From: Evan Lezar Date: Tue, 29 Mar 2022 11:52:38 +0200 Subject: [PATCH] Split loading config from reader and getting config from toml.Tree Signed-off-by: Evan Lezar --- internal/config/runtime.go | 17 +++++++++++++---- internal/config/runtime_test.go | 2 +- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/internal/config/runtime.go b/internal/config/runtime.go index 4b197b74..5132c6d3 100644 --- a/internal/config/runtime.go +++ b/internal/config/runtime.go @@ -56,7 +56,7 @@ func GetRuntimeConfig() (*RuntimeConfig, error) { } defer tomlFile.Close() - cfg, err := getRuntimeConfigFrom(tomlFile) + cfg, err := loadRuntimeConfigFrom(tomlFile) if err != nil { return nil, fmt.Errorf("failed to read config values: %v", err) } @@ -64,20 +64,29 @@ func GetRuntimeConfig() (*RuntimeConfig, error) { return cfg, nil } -// getRuntimeConfigFrom reads the config from the specified Reader -func getRuntimeConfigFrom(reader io.Reader) (*RuntimeConfig, error) { +// loadRuntimeConfigFrom reads the config from the specified Reader +func loadRuntimeConfigFrom(reader io.Reader) (*RuntimeConfig, error) { toml, err := toml.LoadReader(reader) if err != nil { return nil, err } + return getRuntimeConfigFrom(toml), nil +} + +// getRuntimeConfigFrom reads the nvidia container runtime config from the specified toml Tree. +func getRuntimeConfigFrom(toml *toml.Tree) *RuntimeConfig { cfg := getDefaultRuntimeConfig() + if toml == nil { + return cfg + } + cfg.DebugFilePath = toml.GetDefault("nvidia-container-runtime.debug", cfg.DebugFilePath).(string) cfg.Experimental = toml.GetDefault("nvidia-container-runtime.experimental", cfg.Experimental).(bool) cfg.DiscoverMode = toml.GetDefault("nvidia-container-runtime.discover-mode", cfg.DiscoverMode).(string) - return cfg, nil + return cfg } // getDefaultRuntimeConfig defines the default values for the config diff --git a/internal/config/runtime_test.go b/internal/config/runtime_test.go index 244d0f66..1601f392 100644 --- a/internal/config/runtime_test.go +++ b/internal/config/runtime_test.go @@ -95,7 +95,7 @@ func TestGerRuntimeConfig(t *testing.T) { t.Run(tc.description, func(t *testing.T) { reader := strings.NewReader(strings.Join(tc.contents, "\n")) - cfg, err := getRuntimeConfigFrom(reader) + cfg, err := loadRuntimeConfigFrom(reader) if tc.expectedError != nil { require.Error(t, err) } else {