First commit of golang CLI.

This commit is contained in:
2025-08-31 11:51:11 -07:00
parent 4ca06aecb6
commit f0a2098f11
51 changed files with 8840 additions and 0 deletions

View File

@@ -0,0 +1,116 @@
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
}