mirror of
https://github.com/NVIDIA/nvidia-container-toolkit
synced 2025-06-26 18:18:24 +00:00
Add config.Toml type to handle config files
This change introduced a config.Toml type that is used as the base for config file processing and manipulation. This ensures that configs -- including commented values -- can be handled consistently. Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
@@ -43,13 +43,15 @@ func loadConfig() (*config.Config, error) {
|
||||
}
|
||||
|
||||
for _, p := range configPaths {
|
||||
cfg, err := config.Load(p)
|
||||
cfg, err := config.New(
|
||||
config.WithConfigFile(p),
|
||||
)
|
||||
if err == nil {
|
||||
return cfg, nil
|
||||
return cfg.Config()
|
||||
} else if os.IsNotExist(err) && !required {
|
||||
continue
|
||||
}
|
||||
return nil, fmt.Errorf("couldn't open configuration file: %v", err)
|
||||
return nil, fmt.Errorf("couldn't open required configuration file: %v", err)
|
||||
}
|
||||
|
||||
return config.GetDefault()
|
||||
|
||||
@@ -17,12 +17,10 @@
|
||||
package defaultsubcommand
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
|
||||
"github.com/NVIDIA/nvidia-container-toolkit/internal/config"
|
||||
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
|
||||
@@ -102,57 +100,23 @@ func (m command) run(c *cli.Context, opts *options) error {
|
||||
return fmt.Errorf("unable to create output directory: %v", err)
|
||||
}
|
||||
|
||||
contents, err := opts.getFormattedConfig()
|
||||
cfgToml, err := opts.getConfig()
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to fix comments: %v", err)
|
||||
return fmt.Errorf("failed to load config: %v", err)
|
||||
}
|
||||
|
||||
if _, err := opts.Write(contents); err != nil {
|
||||
if _, err := opts.Write(cfgToml); err != nil {
|
||||
return fmt.Errorf("unable to write to output: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// getFormattedConfig returns the default config formatted as required from the specified config file.
|
||||
// The config is then formatted as required.
|
||||
// No indentation is used and comments are modified so that there is no space
|
||||
// after the '#' character.
|
||||
func (opts options) getFormattedConfig() ([]byte, error) {
|
||||
cfg, err := config.Load(opts.config)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to load or create config: %v", err)
|
||||
}
|
||||
|
||||
buffer := bytes.NewBuffer(nil)
|
||||
|
||||
if _, err := cfg.Save(buffer); err != nil {
|
||||
return nil, fmt.Errorf("unable to save config: %v", err)
|
||||
}
|
||||
return fixComments(buffer.Bytes())
|
||||
}
|
||||
|
||||
func fixComments(contents []byte) ([]byte, error) {
|
||||
r, err := regexp.Compile(`(\n*)\s*?#\s*(\S.*)`)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to compile regexp: %v", err)
|
||||
}
|
||||
replaced := r.ReplaceAll(contents, []byte("$1#$2"))
|
||||
|
||||
return replaced, nil
|
||||
}
|
||||
|
||||
func (opts options) outputExists() (bool, error) {
|
||||
if opts.output == "" {
|
||||
return false, nil
|
||||
}
|
||||
_, err := os.Stat(opts.output)
|
||||
if err == nil {
|
||||
return true, nil
|
||||
} else if !os.IsNotExist(err) {
|
||||
return false, fmt.Errorf("unable to stat output file: %v", err)
|
||||
}
|
||||
return false, nil
|
||||
// getConfig returns the TOML config for the specified options.
|
||||
func (opts options) getConfig() (*config.Toml, error) {
|
||||
return config.New(
|
||||
config.WithConfigFile(opts.config),
|
||||
)
|
||||
}
|
||||
|
||||
func (opts options) ensureOutputFolder() error {
|
||||
@@ -166,7 +130,7 @@ func (opts options) ensureOutputFolder() error {
|
||||
}
|
||||
|
||||
// Write writes the contents to the output file specified in the options.
|
||||
func (opts options) Write(contents []byte) (int, error) {
|
||||
func (opts options) Write(cfg *config.Toml) (int, error) {
|
||||
var output io.Writer
|
||||
if opts.output == "" {
|
||||
output = os.Stdout
|
||||
@@ -179,5 +143,6 @@ func (opts options) Write(contents []byte) (int, error) {
|
||||
output = outputFile
|
||||
}
|
||||
|
||||
return output.Write(contents)
|
||||
n, err := cfg.Save(output)
|
||||
return int(n), err
|
||||
}
|
||||
|
||||
@@ -1,82 +0,0 @@
|
||||
/**
|
||||
# Copyright (c) NVIDIA CORPORATION. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
**/
|
||||
|
||||
package defaultsubcommand
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestFixComment(t *testing.T) {
|
||||
testCases := []struct {
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
input: "# comment",
|
||||
expected: "#comment",
|
||||
},
|
||||
{
|
||||
input: " #comment",
|
||||
expected: "#comment",
|
||||
},
|
||||
{
|
||||
input: " # comment",
|
||||
expected: "#comment",
|
||||
},
|
||||
{
|
||||
input: strings.Join([]string{
|
||||
"some",
|
||||
"# comment",
|
||||
" # comment",
|
||||
" #comment",
|
||||
"other"}, "\n"),
|
||||
expected: strings.Join([]string{
|
||||
"some",
|
||||
"#comment",
|
||||
"#comment",
|
||||
"#comment",
|
||||
"other"}, "\n"),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.input, func(t *testing.T) {
|
||||
actual, _ := fixComments([]byte(tc.input))
|
||||
require.Equal(t, tc.expected, string(actual))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetFormattedConfig(t *testing.T) {
|
||||
expectedLines := []string{
|
||||
"#no-cgroups = false",
|
||||
"#debug = \"/var/log/nvidia-container-toolkit.log\"",
|
||||
"#debug = \"/var/log/nvidia-container-runtime.log\"",
|
||||
}
|
||||
|
||||
opts := &options{}
|
||||
contents, err := opts.getFormattedConfig()
|
||||
require.NoError(t, err)
|
||||
lines := strings.Split(string(contents), "\n")
|
||||
|
||||
for _, line := range expectedLines {
|
||||
require.Contains(t, lines, line)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user