Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 63 additions & 17 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
package cmd

import (
"bytes"
"fmt"
"net/url"
"os"
"os/exec"
"path/filepath"
"slices"
"strconv"
Expand Down Expand Up @@ -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() {
Expand All @@ -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() {
Expand Down Expand Up @@ -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())
}
Comment on lines +1256 to +1262
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to capture stdout too?
Maybe you could simplify this and use CombinedOutput instead of Run().

Copy link
Contributor Author

@brayan-trejo brayan-trejo Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stdout is captured but I don't do anything with it for now because at least with the current implementation of --dry-run nothing is printed to stdout when successful, but I figured it's still good practice to capture it.

Copy link
Contributor Author

@brayan-trejo brayan-trejo Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked into it a bit more. With CombinedOutput(), a combined byte slice is returned and you cannot reliably tell where stdout ends and where stderr begins. The intent here is to cleanly separate these two, and print stderr only when an error occurs. Even though stdout doesn't print anything useful now, in the future if that changes, it'd be nice to have this template to conditionally do something with stdout.


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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Loading