Skip to content

Commit d5204e7

Browse files
committed
fix(config): repair missing onctl.yaml and warn about legacy provider files
initializeOnctlEnv treated any existing .onctl directory as fully initialized, even one created before the single-onctl.yaml config that only had per-provider yaml files. That left ReadConfig permanently failing with "no configuration directory found" since onctl.yaml was never written. Now both init and every ReadConfig call populate a missing onctl.yaml and print a warning when legacy per-provider files (aws.yaml, azure.yaml, fc.yaml, gcp.yaml, hetzner.yaml) are present, since their settings are silently ignored by the new single-file config. Also isolates HOME in TestInitializeOnctlEnv_Coverage, a second instance of the same un-isolated-HOME bug fixed for TestInitializeOnctlEnv_NewDirectory in the previous commit.
1 parent 9fc29f0 commit d5204e7

4 files changed

Lines changed: 161 additions & 2 deletions

File tree

‎cmd/common.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ func ReadConfig() error {
8181
return fmt.Errorf("failed to read onctl.yaml in %s: %w", configDir, err)
8282
}
8383

84+
warnLegacyProviderConfigFiles(configDir)
85+
8486
log.Println("[DEBUG] Loaded Settings:", viper.AllSettings())
8587
return nil
8688
}

‎cmd/final_coverage_test.go‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,21 @@ func TestInitializeOnctlEnv_Coverage(t *testing.T) {
190190
skipInteractivePrompt = true
191191
defer func() { skipInteractivePrompt = originalSkip }()
192192

193+
// Isolate HOME so this doesn't create a real ~/.onctl directory, which
194+
// would leak into other tests that assert no config exists.
195+
originalHome, homeWasSet := os.LookupEnv("HOME")
196+
defer func() {
197+
if homeWasSet {
198+
_ = os.Setenv("HOME", originalHome)
199+
} else {
200+
_ = os.Unsetenv("HOME")
201+
}
202+
}()
203+
tempHome, err := os.MkdirTemp("", "onctl-test-home")
204+
assert.NoError(t, err)
205+
defer func() { _ = os.RemoveAll(tempHome) }()
206+
_ = os.Setenv("HOME", tempHome)
207+
193208
// Test initializeOnctlEnv to improve coverage
194209
tempDir, err := os.MkdirTemp("", "onctl-test")
195210
assert.NoError(t, err)

‎cmd/init.go‎

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,26 @@ import (
55
"log"
66
"os"
77
"path/filepath"
8+
"strings"
89

910
"github.com/cdalar/onctl/internal/files"
1011
"github.com/spf13/cobra"
1112
"golang.org/x/term"
1213
)
1314

1415
const (
15-
onctlDirName = ".onctl"
16-
initDir = "init"
16+
onctlDirName = ".onctl"
17+
initDir = "init"
18+
onctlYamlFile = "onctl.yaml"
1719
)
1820

21+
// legacyProviderConfigFiles lists the per-provider YAML files written by
22+
// onctl releases prior to the single onctl.yaml config. They are no longer
23+
// read by ReadConfig, so any settings in them (e.g. a custom gcp.project or
24+
// azure subscription) are silently ignored once a .onctl directory has an
25+
// onctl.yaml of the current format.
26+
var legacyProviderConfigFiles = []string{"aws.yaml", "azure.yaml", "fc.yaml", "gcp.yaml", "hetzner.yaml"}
27+
1928
// skipInteractivePrompt is used to skip interactive prompts during testing
2029
var skipInteractivePrompt = false
2130

@@ -57,11 +66,26 @@ func initializeOnctlEnv() error {
5766
return fmt.Errorf("failed to check %s: %w", homeOnctlPath, err)
5867
} else {
5968
fmt.Printf("Global onctl environment already initialized in %s\n", homeOnctlPath)
69+
// A directory from before the single onctl.yaml config (e.g. one that
70+
// only had per-provider yaml files) has no onctl.yaml of its own;
71+
// populate it now instead of leaving ReadConfig to fail.
72+
if _, err := os.Stat(filepath.Join(homeOnctlPath, onctlYamlFile)); os.IsNotExist(err) {
73+
if err := populateOnctlEnv(homeOnctlPath); err != nil {
74+
return err
75+
}
76+
}
77+
warnLegacyProviderConfigFiles(homeOnctlPath)
6078
}
6179

6280
// Check if local .onctl already exists
6381
if _, err := os.Stat(localOnctlPath); err == nil {
6482
fmt.Printf("Project-based onctl environment already initialized in %s\n", localOnctlPath)
83+
if _, err := os.Stat(filepath.Join(localOnctlPath, onctlYamlFile)); os.IsNotExist(err) {
84+
if err := populateOnctlEnv(localOnctlPath); err != nil {
85+
return err
86+
}
87+
}
88+
warnLegacyProviderConfigFiles(localOnctlPath)
6589
return nil
6690
} else if !os.IsNotExist(err) {
6791
// Handle other os.Stat errors (not IsNotExist)
@@ -93,6 +117,23 @@ func isInteractive() bool {
93117
return term.IsTerminal(int(os.Stdin.Fd()))
94118
}
95119

120+
// warnLegacyProviderConfigFiles warns when configDir still has per-provider
121+
// YAML files from before the single onctl.yaml config. ReadConfig only reads
122+
// onctl.yaml, so settings left in these files (e.g. a custom gcp.project)
123+
// are silently ignored; surface that instead of failing quietly.
124+
func warnLegacyProviderConfigFiles(configDir string) {
125+
var found []string
126+
for _, name := range legacyProviderConfigFiles {
127+
if _, err := os.Stat(filepath.Join(configDir, name)); err == nil {
128+
found = append(found, name)
129+
}
130+
}
131+
if len(found) > 0 {
132+
fmt.Printf("Warning: %s no longer reads %s. Move any custom settings from these files into the matching provider section of %s.\n",
133+
configDir, strings.Join(found, ", "), filepath.Join(configDir, onctlYamlFile))
134+
}
135+
}
136+
96137
func populateOnctlEnv(targetPath string) error {
97138
embedDir, err := files.EmbededFiles.ReadDir(initDir)
98139
if err != nil {

‎cmd/init_test.go‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"bytes"
45
"os"
56
"path/filepath"
67
"testing"
@@ -295,6 +296,106 @@ func TestInitializeOnctlEnv_ExistingLocalConfig(t *testing.T) {
295296
assert.NoError(t, err)
296297
}
297298

299+
func TestWarnLegacyProviderConfigFiles_WarnsWhenPresent(t *testing.T) {
300+
tempDir, err := os.MkdirTemp("", "onctl-test")
301+
require.NoError(t, err)
302+
defer func() { _ = os.RemoveAll(tempDir) }()
303+
304+
// Simulate a pre-single-yaml .onctl directory: a legacy per-provider file
305+
// alongside the current onctl.yaml. ReadConfig only reads onctl.yaml, so
306+
// settings left in gcp.yaml would otherwise be silently ignored.
307+
require.NoError(t, os.WriteFile(filepath.Join(tempDir, "gcp.yaml"), []byte("project: my-project\n"), 0644))
308+
require.NoError(t, os.WriteFile(filepath.Join(tempDir, onctlYamlFile), []byte("vm:\n name: onctl-vm\n"), 0644))
309+
310+
r, w, err := os.Pipe()
311+
require.NoError(t, err)
312+
originalStdout := os.Stdout
313+
os.Stdout = w
314+
315+
warnLegacyProviderConfigFiles(tempDir)
316+
317+
require.NoError(t, w.Close())
318+
os.Stdout = originalStdout
319+
var buf bytes.Buffer
320+
_, err = buf.ReadFrom(r)
321+
require.NoError(t, err)
322+
323+
output := buf.String()
324+
assert.Contains(t, output, "gcp.yaml")
325+
assert.Contains(t, output, "no longer reads")
326+
}
327+
328+
func TestWarnLegacyProviderConfigFiles_SilentWhenAbsent(t *testing.T) {
329+
tempDir, err := os.MkdirTemp("", "onctl-test")
330+
require.NoError(t, err)
331+
defer func() { _ = os.RemoveAll(tempDir) }()
332+
333+
require.NoError(t, os.WriteFile(filepath.Join(tempDir, onctlYamlFile), []byte("vm:\n name: onctl-vm\n"), 0644))
334+
335+
r, w, err := os.Pipe()
336+
require.NoError(t, err)
337+
originalStdout := os.Stdout
338+
os.Stdout = w
339+
340+
warnLegacyProviderConfigFiles(tempDir)
341+
342+
require.NoError(t, w.Close())
343+
os.Stdout = originalStdout
344+
var buf bytes.Buffer
345+
_, err = buf.ReadFrom(r)
346+
require.NoError(t, err)
347+
348+
assert.Empty(t, buf.String())
349+
}
350+
351+
func TestInitializeOnctlEnv_RepairsMissingOnctlYamlInExistingHomeDir(t *testing.T) {
352+
// Reproduces the migration bug flagged in review: a .onctl directory that
353+
// predates the single-onctl.yaml config (e.g. only had per-provider yaml
354+
// files) was treated as "already initialized" and left without an
355+
// onctl.yaml, so every later command would fail with "no configuration
356+
// directory found". initializeOnctlEnv must populate onctl.yaml in that
357+
// case instead of just printing "already initialized".
358+
originalSkip := skipInteractivePrompt
359+
skipInteractivePrompt = true
360+
defer func() { skipInteractivePrompt = originalSkip }()
361+
362+
originalHome, homeWasSet := os.LookupEnv("HOME")
363+
defer func() {
364+
if homeWasSet {
365+
_ = os.Setenv("HOME", originalHome)
366+
} else {
367+
_ = os.Unsetenv("HOME")
368+
}
369+
}()
370+
tempHome, err := os.MkdirTemp("", "onctl-test-home")
371+
require.NoError(t, err)
372+
defer func() { _ = os.RemoveAll(tempHome) }()
373+
require.NoError(t, os.Setenv("HOME", tempHome))
374+
375+
tempProject, err := os.MkdirTemp("", "onctl-test-project")
376+
require.NoError(t, err)
377+
defer func() { _ = os.RemoveAll(tempProject) }()
378+
originalWd, err := os.Getwd()
379+
require.NoError(t, err)
380+
defer func() { _ = os.Chdir(originalWd) }()
381+
require.NoError(t, os.Chdir(tempProject))
382+
383+
// Pre-create a legacy home .onctl directory with no onctl.yaml, as a
384+
// pre-migration install would have left it.
385+
homeOnctlPath := filepath.Join(tempHome, onctlDirName)
386+
require.NoError(t, os.MkdirAll(homeOnctlPath, os.ModePerm))
387+
require.NoError(t, os.WriteFile(filepath.Join(homeOnctlPath, "gcp.yaml"), []byte("project: my-project\n"), 0644))
388+
389+
_, err = os.Stat(filepath.Join(homeOnctlPath, onctlYamlFile))
390+
require.True(t, os.IsNotExist(err), "onctl.yaml should not exist before repair")
391+
392+
err = initializeOnctlEnv()
393+
require.NoError(t, err)
394+
395+
_, err = os.Stat(filepath.Join(homeOnctlPath, onctlYamlFile))
396+
assert.NoError(t, err, "initializeOnctlEnv should populate onctl.yaml in an already-initialized directory that's missing it")
397+
}
398+
298399
func TestInitCmd_Run(t *testing.T) {
299400
// Skip interactive prompts during testing
300401
originalSkip := skipInteractivePrompt

0 commit comments

Comments
 (0)
⌂ 换网址