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
73 changes: 61 additions & 12 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 @@ -921,13 +923,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 @@ -941,8 +940,8 @@ 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() {
if err := tui.exitConfig(); err != nil {
tui.showErrorModal("[red::b]ERROR:[-::-]\n"+err.Error(), func() {
tui.pages.SwitchToPage("page5")
})
return
Expand Down Expand Up @@ -1202,6 +1201,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 +1243 to +1249
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().


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 @@ -1482,8 +1531,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 @@ -1492,7 +1541,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