117 lines
2.9 KiB
Go
117 lines
2.9 KiB
Go
package app
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/wild-cloud/wild-cli/internal/environment"
|
|
"github.com/wild-cloud/wild-cli/internal/external"
|
|
"github.com/wild-cloud/wild-cli/internal/output"
|
|
)
|
|
|
|
var (
|
|
restoreAll bool
|
|
)
|
|
|
|
func newRestoreCommand() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "restore <name>",
|
|
Short: "Restore application data",
|
|
Long: `Restore application data from the configured backup storage.
|
|
|
|
This command restores application databases and persistent volume data using restic
|
|
and the existing backup infrastructure.
|
|
|
|
Examples:
|
|
wild app restore nextcloud
|
|
wild app restore --all`,
|
|
Args: cobra.MaximumNArgs(1),
|
|
RunE: runAppRestore,
|
|
}
|
|
|
|
cmd.Flags().BoolVar(&restoreAll, "all", false, "restore all applications")
|
|
|
|
return cmd
|
|
}
|
|
|
|
func runAppRestore(cmd *cobra.Command, args []string) error {
|
|
if !restoreAll && len(args) == 0 {
|
|
return fmt.Errorf("app name required or use --all flag")
|
|
}
|
|
|
|
var appName string
|
|
if len(args) > 0 {
|
|
appName = args[0]
|
|
}
|
|
|
|
if restoreAll {
|
|
output.Header("Restoring All Applications")
|
|
} else {
|
|
output.Header("Restoring Application")
|
|
output.Info("App: " + appName)
|
|
}
|
|
|
|
// Initialize environment
|
|
env := environment.New()
|
|
if err := env.RequiresProject(); err != nil {
|
|
return err
|
|
}
|
|
|
|
// For now, delegate to the existing bash script to maintain compatibility
|
|
wcRoot := env.WCRoot()
|
|
if wcRoot == "" {
|
|
return fmt.Errorf("WC_ROOT not set. Wild Cloud installation not found")
|
|
}
|
|
|
|
appRestoreScript := filepath.Join(wcRoot, "bin", "wild-app-restore")
|
|
if _, err := os.Stat(appRestoreScript); os.IsNotExist(err) {
|
|
return fmt.Errorf("app restore script not found at %s", appRestoreScript)
|
|
}
|
|
|
|
// Execute the app restore script
|
|
bashTool := external.NewBaseTool("bash", "bash")
|
|
|
|
// Set environment variables needed by the script
|
|
oldWCRoot := os.Getenv("WC_ROOT")
|
|
oldWCHome := os.Getenv("WC_HOME")
|
|
defer func() {
|
|
if oldWCRoot != "" {
|
|
_ = os.Setenv("WC_ROOT", oldWCRoot)
|
|
}
|
|
if oldWCHome != "" {
|
|
_ = os.Setenv("WC_HOME", oldWCHome)
|
|
}
|
|
}()
|
|
|
|
_ = os.Setenv("WC_ROOT", wcRoot)
|
|
_ = os.Setenv("WC_HOME", env.WCHome())
|
|
|
|
var scriptArgs []string
|
|
if restoreAll {
|
|
scriptArgs = []string{appRestoreScript, "--all"}
|
|
} else {
|
|
// Check if app exists in project
|
|
appDir := filepath.Join(env.AppsDir(), appName)
|
|
if _, err := os.Stat(appDir); os.IsNotExist(err) {
|
|
return fmt.Errorf("app '%s' not found in project. Run 'wild app add %s' first", appName, appName)
|
|
}
|
|
scriptArgs = []string{appRestoreScript, appName}
|
|
}
|
|
|
|
output.Info("Running application restore script...")
|
|
if _, err := bashTool.Execute(cmd.Context(), scriptArgs...); err != nil {
|
|
return fmt.Errorf("application restore failed: %w", err)
|
|
}
|
|
|
|
if restoreAll {
|
|
output.Success("All applications restored successfully")
|
|
} else {
|
|
output.Success(fmt.Sprintf("Application '%s' restored successfully", appName))
|
|
}
|
|
|
|
return nil
|
|
}
|