This commit is contained in:
2025-07-26 12:51:12 +02:00
parent f31a19beab
commit 49baf33e92
9 changed files with 149 additions and 26 deletions

View File

@@ -13,6 +13,7 @@ import (
"time"
"github.com/google/subcommands"
"github.com/schollz/progressbar/v3"
)
type (
@@ -37,15 +38,22 @@ func (p *RunCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) s
return subcommands.ExitFailure
}
pg := progressbar.New(len(datastore))
defer pg.Close()
for _, metadata := range datastore {
pg.Describe("Scanning " + metadata.Name + "...")
metadataPath := filepath.Join(game.DatastorePath(), metadata.ID)
err := archiveIfChanged(metadata.ID, metadata.Path, filepath.Join(metadataPath, "data.tar.gz"), filepath.Join(metadataPath, ".last_run"))
if err != nil {
fmt.Fprintf(os.Stderr, "error: cannot process the data of %s: %s\n", metadata.ID, err)
return subcommands.ExitFailure
}
pg.Add(1)
}
pg.Finish()
return subcommands.ExitSuccess
}
@@ -141,7 +149,5 @@ func archiveIfChanged(id, srcDir, destTarGz, stateFile string) error {
return fmt.Errorf("updating state file: %w", err)
}
fmt.Println(id)
return nil
}

View File

@@ -69,7 +69,11 @@ func (p *SyncCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})
continue
}
hremote, _ := client.Hash(r.GameID)
hremote, err := client.Hash(r.GameID)
if err != nil {
fmt.Fprintln(os.Stderr, "error: failed to get the file hash from the remote:", err)
continue
}
vlocal, err := game.Version(r.GameID)
if err != nil {
@@ -77,18 +81,21 @@ func (p *SyncCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})
continue
}
vremote, _ := client.Version(r.GameID)
if hlocal == hremote {
fmt.Println("already up-to-date")
vremote, err := client.Version(r.GameID)
if err != nil {
fmt.Fprintln(os.Stderr, "error: failed to get the file version from the remote:", err)
continue
}
if vremote == 0 {
if err := push(r.GameID, m, client); err != nil {
fmt.Fprintln(os.Stderr, "failed to push:", err)
return subcommands.ExitFailure
if hlocal == hremote {
if vlocal != vremote {
slog.Debug("version is not the same, but the hash is equal. Updating local database")
if err := game.SetVersion(r.GameID, vremote); err != nil {
fmt.Fprintln(os.Stderr, "error: failed to synchronize version number:", err)
continue
}
}
fmt.Println("already up-to-date")
continue
}
@@ -101,7 +108,14 @@ func (p *SyncCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})
}
if vlocal < vremote {
fmt.Println("pull")
if err := push(r.GameID, m, client); err != nil {
fmt.Fprintln(os.Stderr, "failed to push:", err)
return subcommands.ExitFailure
}
if err := game.SetVersion(r.GameID, vremote); err != nil {
fmt.Fprintln(os.Stderr, "error: failed to synchronize version number:", err)
continue
}
continue
}
@@ -109,6 +123,7 @@ func (p *SyncCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})
fmt.Println("conflict")
continue
}
}
return subcommands.ExitSuccess
@@ -119,3 +134,9 @@ func push(gameID string, m game.Metadata, cli *client.Client) error {
return cli.Push(gameID, archivePath, m)
}
func pull(gameID string, cli *client.Client) error {
archivePath := filepath.Join(game.DatastorePath(), gameID, "data.tar.gz")
return cli.Pull(gameID, archivePath)
}

View File

@@ -10,6 +10,7 @@ import (
"cloudsave/cmd/cli/commands/version"
"context"
"flag"
"fmt"
"os"
"github.com/google/subcommands"
@@ -31,5 +32,9 @@ func main() {
flag.Parse()
ctx := context.Background()
os.Exit(int(subcommands.Execute(ctx)))
exitCode := subcommands.Execute(ctx)
fmt.Println()
os.Exit(int(exitCode))
}

View File

@@ -4,6 +4,7 @@ import (
"cloudsave/cmd/server/data"
"cloudsave/pkg/game"
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
"io"
@@ -227,7 +228,7 @@ func (s HTTPServer) hash(w http.ResponseWriter, r *http.Request) {
// Get checksum result
sum := hasher.Sum(nil)
ok(sum, w, r)
ok(hex.EncodeToString(sum), w, r)
}
func (s HTTPServer) version(w http.ResponseWriter, r *http.Request) {
@@ -269,7 +270,7 @@ func (s HTTPServer) version(w http.ResponseWriter, r *http.Request) {
func parseFormMetadata(gameID string, values map[string][]string) (game.Metadata, error) {
var name string
if v, ok := values["name"]; ok {
if len(v) != 0 {
if len(v) == 0 {
return game.Metadata{}, fmt.Errorf("error: corrupted metadata")
}
@@ -280,7 +281,7 @@ func parseFormMetadata(gameID string, values map[string][]string) (game.Metadata
var version int
if v, ok := values["version"]; ok {
if len(v) != 0 {
if len(v) == 0 {
return game.Metadata{}, fmt.Errorf("error: corrupted metadata")
}
if v, err := strconv.Atoi(v[0]); err == nil {

View File

@@ -14,7 +14,7 @@ func Write(gameID, documentRoot string, r io.Reader) error {
partPath := filepath.Join(dataFolderPath, "data.tar.gz.part")
finalFilePath := filepath.Join(dataFolderPath, "data.tar.gz")
if err := os.MkdirAll(dataFolderPath, 0740); err != nil {
if err := makeDataFolder(gameID, documentRoot); err != nil {
return err
}
@@ -40,6 +40,9 @@ func Write(gameID, documentRoot string, r io.Reader) error {
}
func UpdateMetadata(gameID, documentRoot string, m game.Metadata) error {
if err := makeDataFolder(gameID, documentRoot); err != nil {
return err
}
path := filepath.Join(documentRoot, "data", gameID, "metadata.json")
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0740)
@@ -51,3 +54,7 @@ func UpdateMetadata(gameID, documentRoot string, m game.Metadata) error {
e := json.NewEncoder(f)
return e.Encode(m)
}
func makeDataFolder(gameID, documentRoot string) error {
return os.MkdirAll(filepath.Join(documentRoot, "data", gameID), 0740)
}