First commit of golang CLI.
This commit is contained in:
116
wild-cli/cmd/wild/app/backup.go
Normal file
116
wild-cli/cmd/wild/app/backup.go
Normal 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 (
|
||||
backupAll bool
|
||||
)
|
||||
|
||||
func newBackupCommand() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "backup <name>",
|
||||
Short: "Backup application data",
|
||||
Long: `Backup application data to the configured backup storage.
|
||||
|
||||
This command backs up application databases and persistent volume data using restic
|
||||
and the existing backup infrastructure.
|
||||
|
||||
Examples:
|
||||
wild app backup nextcloud
|
||||
wild app backup --all`,
|
||||
Args: cobra.MaximumNArgs(1),
|
||||
RunE: runAppBackup,
|
||||
}
|
||||
|
||||
cmd.Flags().BoolVar(&backupAll, "all", false, "backup all applications")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func runAppBackup(cmd *cobra.Command, args []string) error {
|
||||
if !backupAll && len(args) == 0 {
|
||||
return fmt.Errorf("app name required or use --all flag")
|
||||
}
|
||||
|
||||
var appName string
|
||||
if len(args) > 0 {
|
||||
appName = args[0]
|
||||
}
|
||||
|
||||
if backupAll {
|
||||
output.Header("Backing Up All Applications")
|
||||
} else {
|
||||
output.Header("Backing Up 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")
|
||||
}
|
||||
|
||||
appBackupScript := filepath.Join(wcRoot, "bin", "wild-app-backup")
|
||||
if _, err := os.Stat(appBackupScript); os.IsNotExist(err) {
|
||||
return fmt.Errorf("app backup script not found at %s", appBackupScript)
|
||||
}
|
||||
|
||||
// Execute the app backup 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 backupAll {
|
||||
scriptArgs = []string{appBackupScript, "--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{appBackupScript, appName}
|
||||
}
|
||||
|
||||
output.Info("Running application backup script...")
|
||||
if _, err := bashTool.Execute(cmd.Context(), scriptArgs...); err != nil {
|
||||
return fmt.Errorf("application backup failed: %w", err)
|
||||
}
|
||||
|
||||
if backupAll {
|
||||
output.Success("All applications backed up successfully")
|
||||
} else {
|
||||
output.Success(fmt.Sprintf("Application '%s' backed up successfully", appName))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user