implement applu

This commit is contained in:
2025-08-17 00:30:02 +02:00
parent aa29fae900
commit 851ff89886
4 changed files with 61 additions and 6 deletions

View File

@@ -1,6 +1,7 @@
package apply package apply
import ( import (
"cloudsave/pkg/data"
"context" "context"
"flag" "flag"
"fmt" "fmt"
@@ -11,13 +12,14 @@ import (
type ( type (
ListCmd struct { ListCmd struct {
Service *data.Service
} }
) )
func (*ListCmd) Name() string { return "apply" } func (*ListCmd) Name() string { return "apply" }
func (*ListCmd) Synopsis() string { return "apply a backup" } func (*ListCmd) Synopsis() string { return "apply a backup" }
func (*ListCmd) Usage() string { func (*ListCmd) Usage() string {
return `Usage: cloudsave apply <GAME_ID> <BACKUP_ID> return `Usage: cloudsave apply <GAME_ID> [BACKUP_ID]
Apply a backup Apply a backup
` `
@@ -27,15 +29,26 @@ func (p *ListCmd) SetFlags(f *flag.FlagSet) {
} }
func (p *ListCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { func (p *ListCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
if f.NArg() != 2 { if f.NArg() < 1 {
fmt.Fprintln(os.Stderr, "error: missing game ID and/or backup uuid") fmt.Fprintln(os.Stderr, "error: missing game ID and/or backup uuid")
return subcommands.ExitUsageError return subcommands.ExitUsageError
} }
//gameID := f.Arg(0) gameID := f.Arg(0)
//uuid := f.Arg(1) uuid := f.Arg(1)
panic("not implemented") if len(uuid) == 0 {
if err := p.Service.ApplyCurrent(gameID); err != nil {
fmt.Fprintf(os.Stderr, "error: failed to apply: %s", err)
return subcommands.ExitFailure
}
return subcommands.ExitSuccess
}
if err := p.Service.ApplyBackup(gameID, uuid); err != nil {
fmt.Fprintf(os.Stderr, "error: failed to apply: %s", err)
return subcommands.ExitFailure
}
return subcommands.ExitSuccess return subcommands.ExitSuccess
} }

View File

@@ -2,6 +2,7 @@ package main
import ( import (
"cloudsave/cmd/cli/commands/add" "cloudsave/cmd/cli/commands/add"
"cloudsave/cmd/cli/commands/apply"
"cloudsave/cmd/cli/commands/list" "cloudsave/cmd/cli/commands/list"
"cloudsave/cmd/cli/commands/pull" "cloudsave/cmd/cli/commands/pull"
"cloudsave/cmd/cli/commands/remote" "cloudsave/cmd/cli/commands/remote"
@@ -48,6 +49,8 @@ func main() {
subcommands.Register(&list.ListCmd{Service: s}, "management") subcommands.Register(&list.ListCmd{Service: s}, "management")
subcommands.Register(&remove.RemoveCmd{Service: s}, "management") subcommands.Register(&remove.RemoveCmd{Service: s}, "management")
subcommands.Register(&apply.ListCmd{Service: s}, "restore")
subcommands.Register(&remote.RemoteCmd{Service: s}, "remote") subcommands.Register(&remote.RemoteCmd{Service: s}, "remote")
subcommands.Register(&sync.SyncCmd{Service: s}, "remote") subcommands.Register(&sync.SyncCmd{Service: s}, "remote")
subcommands.Register(&pull.PullCmd{Service: s}, "remote") subcommands.Register(&pull.PullCmd{Service: s}, "remote")

View File

@@ -1,5 +1,5 @@
package constants package constants
const Version = "0.0.4" const Version = "0.0.5"
const ApiVersion = 1 const ApiVersion = 1

View File

@@ -344,3 +344,42 @@ func (l Service) CopyBackup(gameID, backupID string, src io.Reader) error {
return nil return nil
} }
func (l Service) ApplyCurrent(gameID string) error {
id := repository.NewGameIdentifier(gameID)
path := l.repo.DataPath(id)
g, err := l.repo.Metadata(id)
if err != nil {
return err
}
return l.apply(path, g.Path)
}
func (l Service) ApplyBackup(gameID, backupID string) error {
id := repository.NewGameIdentifier(gameID)
fullID := repository.NewBackupIdentifier(gameID, backupID)
path := l.repo.DataPath(fullID)
g, err := l.repo.Metadata(id)
if err != nil {
return err
}
return l.apply(path, g.Path)
}
func (l Service) apply(src, dst string) error {
if err := os.RemoveAll(dst); err != nil {
return fmt.Errorf("failed to remove old save: %w", err)
}
f, err := os.OpenFile(src, os.O_RDONLY, 0)
if err != nil {
return fmt.Errorf("failed to open archive: %w", err)
}
defer f.Close()
return archive.Untar(f, dst)
}