diff --git a/cmd/config.go b/cmd/config.go index 83c935e4c..12de057c0 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -26,9 +26,11 @@ package cmd import ( + "bytes" "fmt" "net/url" "os" + "os/exec" "path/filepath" "slices" "strconv" @@ -929,13 +931,10 @@ func (tui *appContext) buildCachingPage() tview.Primitive { tui.showCacheConfirmationModal(cacheSizeText, // Callback function if the user selects Finish func() { - if err := tui.createYAMLConfig(); err != nil { - tui.showErrorModal( - "[red::b]ERROR:[-::-] Failed to create YAML config:\n"+err.Error(), - func() { - tui.pages.SwitchToPage("page5") - }, - ) + if err := tui.exitConfig(); err != nil { + tui.showErrorModal("[red::b]ERROR:[-::-]\n"+err.Error(), func() { + tui.pages.SwitchToPage("page5") + }) return } tui.showExitModal(func() { @@ -949,13 +948,10 @@ func (tui *appContext) buildCachingPage() tview.Primitive { } else { // If caching is disabled, just finish the configuration - if err := tui.createYAMLConfig(); err != nil { - tui.showErrorModal( - "[red::b]ERROR:[-::-] Failed to create YAML config:\n"+err.Error(), - func() { - tui.pages.SwitchToPage("page5") - }, - ) + if err := tui.exitConfig(); err != nil { + tui.showErrorModal("[red::b]ERROR:[-::-]\n"+err.Error(), func() { + tui.pages.SwitchToPage("page5") + }) return } tui.showExitModal(func() { @@ -1218,6 +1214,56 @@ func (tui *appContext) showExitModal(onConfirm func()) { }() } +// Function to handle exiting the configuration process. +// Creates the YAML config file and executes a dry run. +func (tui *appContext) exitConfig() error { + if err := tui.createYAMLConfig(); err != nil { + return fmt.Errorf("Failed to create YAML config:\n%v", err) + } + if err := tui.dryRun(); err != nil { + return fmt.Errorf("Configuration validation failed:\n%v", err) + } + return nil +} + +// Function to perform a dry run of cloudfuse with the generated config file. +func (tui *appContext) dryRun() error { + // Create /tmp/mnt-test directory if it doesn't exist. Needs to be empty for dry run. + mountPoint := filepath.Join(os.TempDir(), "mnt-test") + if _, err := os.Stat(mountPoint); os.IsNotExist(err) { + if err := os.MkdirAll(mountPoint, 0700); err != nil { + return fmt.Errorf( + "Failed to create test mount point '%s' for dry run: %v", + mountPoint, + err, + ) + } + } + + // Spawn child process to run cloudfuse with the generated config file and --dry-run flag + cmd := exec.Command( + "cloudfuse", + "mount", + mountPoint, + "--config-file", + tui.config.configFilePath, + "--dry-run", + "--passphrase", + tui.config.configEncryptionPassphrase, + ) + + // Capture stdout and stderr + var outBuf, errBuf bytes.Buffer + cmd.Stdout = &outBuf + cmd.Stderr = &errBuf + err := cmd.Run() + if err != nil { + return fmt.Errorf("Dry run failed: %v\n%s", err, errBuf.String()) + } + + return nil +} + // Helper function to center lines of text within a specified width. // It is used to format text views and other UI elements in the TUI. func centerText(text string, width int) string { @@ -1498,8 +1544,8 @@ func (tui *appContext) createYAMLConfig() error { } // Write the encrypted YAML config data to a file - if err := os.WriteFile("config.aes", cipherText, 0600); err != nil { - return fmt.Errorf("Failed to create encrypted config.aes file: %v", err) + if err := os.WriteFile("config.yaml.aes", cipherText, 0600); err != nil { + return fmt.Errorf("Failed to create encrypted config.yaml.aes file: %v", err) } // Update configFilePath member to point to the created config file @@ -1508,7 +1554,7 @@ func (tui *appContext) createYAMLConfig() error { return fmt.Errorf("Failed to get current working directory: %v", err) } - tui.config.configFilePath = filepath.Join(currDir, "config.aes") + tui.config.configFilePath = filepath.Join(currDir, "config.yaml.aes") return nil }