cmd/ctrld: honor configPath when writing config file

Updates #58
This commit is contained in:
Cuong Manh Le
2023-02-14 00:30:36 +07:00
committed by Cuong Manh Le
parent 35c890048b
commit 71b1b324db
2 changed files with 25 additions and 0 deletions

View File

@@ -445,6 +445,8 @@ func initCLI() {
func writeConfigFile() error {
if cfu := v.ConfigFileUsed(); cfu != "" {
defaultConfigFile = cfu
} else if configPath != "" {
defaultConfigFile = configPath
}
f, err := os.OpenFile(defaultConfigFile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, os.FileMode(0o644))
if err != nil {

23
cmd/ctrld/cli_test.go Normal file
View File

@@ -0,0 +1,23 @@
package main
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_writeConfigFile(t *testing.T) {
tmpdir := t.TempDir()
// simulate --config CLI flag by setting configPath manually.
configPath = filepath.Join(tmpdir, "ctrld.toml")
_, err := os.Stat(configPath)
assert.True(t, os.IsNotExist(err))
assert.NoError(t, writeConfigFile())
_, err = os.Stat(configPath)
require.NoError(t, err)
}