[client] Name the two config readers for what they do

ReadConfig and GetConfig differed in one thing — what happens when the file is
absent — and neither name said which was which:

- ReadConfig      -> ReadOrGenerateConfig  (reads it, or generates one in memory)
- GetConfig       -> GetExistingConfig     (reads it, or fails)

Three comments went with them:

- GetConfig's said "return with Config and if it was created. Errors out if it
  does not exist", which described a bool it does not return and a creation it
  never performs.
- ReadConfig's explained that it does not write, which is what a reader is
  supposed to do anyway.
- Server.getConfig's said it "errors out if it does not exist", which it does
  not — it resolves a default config, and now provisions the identity too.
This commit is contained in:
riccardom
2026-09-02 15:21:35 +02:00
parent 7a1f095eb5
commit 844bf24a6f
14 changed files with 51 additions and 54 deletions
+5 -9
View File
@@ -1130,8 +1130,8 @@ func update(input ConfigInput) (*Config, error) {
return config, nil
}
// GetConfig read config file and return with Config and if it was created. Errors out if it does not exist
func GetConfig(configPath string) (*Config, error) {
// GetExistingConfig reads and returns the config if it exists on disk. Fails otherwise.
func GetExistingConfig(configPath string) (*Config, error) {
return readConfig(configPath, false)
}
@@ -1219,13 +1219,9 @@ func CreateInMemoryConfig(input ConfigInput) (*Config, error) {
return createNewConfig(input)
}
// ReadConfig reads the profile config at configPath, resolving a default config
// in memory when the file does not exist.
//
// It never writes. A caller that wants what it got back to be on disk calls
// WriteOutConfig itself, and one that resolved a config for a peer to run with
// calls EnsureIdentity first — see Server.getConfig for that pair.
func ReadConfig(configPath string) (*Config, error) {
// ReadOrGenerateConfig reads the profile config at configPath if it exists, or
// generates one in memory from the defaults.
func ReadOrGenerateConfig(configPath string) (*Config, error) {
return readConfig(configPath, true)
}
@@ -196,7 +196,7 @@ func TestWireguardPortZeroExplicit(t *testing.T) {
assert.Equal(t, 0, config.WgPort, "WgPort should be 0 when explicitly set by user")
// Verify it persists
readConfig, err := GetConfig(configPath)
readConfig, err := GetExistingConfig(configPath)
require.NoError(t, err)
assert.Equal(t, 0, readConfig.WgPort, "WgPort should remain 0 after reading from file")
}
@@ -108,8 +108,8 @@ func TestReadsDoNotWriteTheConfigBack(t *testing.T) {
denormalized := []byte(`{"WgIface":"wt0"}`)
for name, read := range map[string]func(string) (*Config, error){
"GetConfig": GetConfig,
"ReadConfig": ReadConfig,
"GetExistingConfig": GetExistingConfig,
"ReadOrGenerateConfig": ReadOrGenerateConfig,
} {
t.Run(name, func(t *testing.T) {
path := filepath.Join(t.TempDir(), "profile.json")
@@ -132,7 +132,7 @@ func TestReadsDoNotWriteTheConfigBack(t *testing.T) {
func TestReadConfigDoesNotCreateTheFile(t *testing.T) {
path := filepath.Join(t.TempDir(), "absent.json")
cfg, err := ReadConfig(path)
cfg, err := ReadOrGenerateConfig(path)
require.NoError(t, err)
require.Equal(t, DefaultManagementURL, cfg.ManagementURL.String())
@@ -198,7 +198,7 @@ func TestWouldChangeIgnoresURLSpelling(t *testing.T) {
})
require.NoError(t, err)
cfg, err := GetConfig(path)
cfg, err := GetExistingConfig(path)
require.NoError(t, err)
for _, spelling := range []string{
@@ -246,7 +246,7 @@ func TestUpdateConfigProvisionsAMissingIdentity(t *testing.T) {
require.NoError(t, err)
// Stand in for the logout, which zeroes the keys and writes the config out.
loggedOut, err := GetConfig(path)
loggedOut, err := GetExistingConfig(path)
require.NoError(t, err)
loggedOut.PrivateKey = ""
loggedOut.SSHKey = ""
@@ -257,7 +257,7 @@ func TestUpdateConfigProvisionsAMissingIdentity(t *testing.T) {
require.NotEmpty(t, cfg.PrivateKey, "the write path did not provision an identity")
require.NotEmpty(t, cfg.SSHKey)
persisted, err := GetConfig(path)
persisted, err := GetExistingConfig(path)
require.NoError(t, err)
require.Equal(t, cfg.PrivateKey, persisted.PrivateKey, "the provisioned identity was not persisted")
}