better usage prompt + hash opti on server
This commit is contained in:
@@ -1,12 +1,14 @@
|
|||||||
package add
|
package add
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"cloudsave/pkg/remote"
|
||||||
"cloudsave/pkg/repository"
|
"cloudsave/pkg/repository"
|
||||||
"context"
|
"context"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/google/subcommands"
|
"github.com/google/subcommands"
|
||||||
)
|
)
|
||||||
@@ -14,19 +16,24 @@ import (
|
|||||||
type (
|
type (
|
||||||
AddCmd struct {
|
AddCmd struct {
|
||||||
name string
|
name string
|
||||||
|
remote string
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func (*AddCmd) Name() string { return "add" }
|
func (*AddCmd) Name() string { return "add" }
|
||||||
func (*AddCmd) Synopsis() string { return "Add a folder to the sync list" }
|
func (*AddCmd) Synopsis() string { return "add a folder to the sync list" }
|
||||||
func (*AddCmd) Usage() string {
|
func (*AddCmd) Usage() string {
|
||||||
return `add:
|
return `Usage: cloudsave add [-name] [-remote] <PATH>
|
||||||
Add a folder to the sync list
|
|
||||||
|
Add a folder to the track list
|
||||||
|
|
||||||
|
Options:
|
||||||
`
|
`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *AddCmd) SetFlags(f *flag.FlagSet) {
|
func (p *AddCmd) SetFlags(f *flag.FlagSet) {
|
||||||
f.StringVar(&p.name, "name", "", "Override the name of the game")
|
f.StringVar(&p.name, "name", "", "Override the name of the game")
|
||||||
|
f.StringVar(&p.remote, "remote", "", "Defines a remote server to sync with")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *AddCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
|
func (p *AddCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
|
||||||
@@ -50,6 +57,10 @@ func (p *AddCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) s
|
|||||||
return subcommands.ExitFailure
|
return subcommands.ExitFailure
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(strings.TrimSpace(p.remote)) > 0 {
|
||||||
|
remote.Set(m.ID, p.remote)
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Println(m.ID)
|
fmt.Println(m.ID)
|
||||||
|
|
||||||
return subcommands.ExitSuccess
|
return subcommands.ExitSuccess
|
||||||
|
|||||||
@@ -20,7 +20,8 @@ type (
|
|||||||
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 `apply:
|
return `Usage: cloudsave apply <GAME_ID> <BACKUP_ID>
|
||||||
|
|
||||||
Apply a backup
|
Apply a backup
|
||||||
`
|
`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,8 +22,11 @@ type (
|
|||||||
func (*ListCmd) Name() string { return "list" }
|
func (*ListCmd) Name() string { return "list" }
|
||||||
func (*ListCmd) Synopsis() string { return "list all game registered" }
|
func (*ListCmd) Synopsis() string { return "list all game registered" }
|
||||||
func (*ListCmd) Usage() string {
|
func (*ListCmd) Usage() string {
|
||||||
return `list:
|
return `Usage: cloudsave list [-include-backup] [-a]
|
||||||
|
|
||||||
List all game registered
|
List all game registered
|
||||||
|
|
||||||
|
Options:
|
||||||
`
|
`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
package pull
|
package pull
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"cloudsave/cmd/cli/tools/prompt/credentials"
|
||||||
"cloudsave/pkg/remote/client"
|
"cloudsave/pkg/remote/client"
|
||||||
"cloudsave/pkg/repository"
|
"cloudsave/pkg/repository"
|
||||||
"cloudsave/pkg/tools/archive"
|
"cloudsave/pkg/tools/archive"
|
||||||
"cloudsave/cmd/cli/tools/prompt/credentials"
|
|
||||||
"context"
|
"context"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -22,7 +22,8 @@ type (
|
|||||||
func (*PullCmd) Name() string { return "pull" }
|
func (*PullCmd) Name() string { return "pull" }
|
||||||
func (*PullCmd) Synopsis() string { return "pull a game save from the remote" }
|
func (*PullCmd) Synopsis() string { return "pull a game save from the remote" }
|
||||||
func (*PullCmd) Usage() string {
|
func (*PullCmd) Usage() string {
|
||||||
return `list:
|
return `Usage: cloudsave pull <GAME_ID>
|
||||||
|
|
||||||
Pull a game save from the remote
|
Pull a game save from the remote
|
||||||
`
|
`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,10 +20,17 @@ type (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (*RemoteCmd) Name() string { return "remote" }
|
func (*RemoteCmd) Name() string { return "remote" }
|
||||||
func (*RemoteCmd) Synopsis() string { return "manage remote" }
|
func (*RemoteCmd) Synopsis() string { return "add or update the remote url" }
|
||||||
func (*RemoteCmd) Usage() string {
|
func (*RemoteCmd) Usage() string {
|
||||||
return `remote:
|
return `Usage: cloudsave remote <-set|-list>
|
||||||
manage remove
|
|
||||||
|
The -list argument lists all remotes for each registered game.
|
||||||
|
This command performs a connection test.
|
||||||
|
|
||||||
|
The -set argument allow you to set (create or update)
|
||||||
|
the URL to the remote for a game
|
||||||
|
|
||||||
|
Options
|
||||||
`
|
`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,8 +17,10 @@ type (
|
|||||||
func (*RemoveCmd) Name() string { return "remove" }
|
func (*RemoveCmd) Name() string { return "remove" }
|
||||||
func (*RemoveCmd) Synopsis() string { return "unregister a game" }
|
func (*RemoveCmd) Synopsis() string { return "unregister a game" }
|
||||||
func (*RemoveCmd) Usage() string {
|
func (*RemoveCmd) Usage() string {
|
||||||
return `remove:
|
return `Usage: cloudsave remove <GAME_ID>
|
||||||
|
|
||||||
Unregister a game
|
Unregister a game
|
||||||
|
Caution: all the backup are deleted
|
||||||
`
|
`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,11 +20,14 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func (*RunCmd) Name() string { return "run" }
|
func (*RunCmd) Name() string { return "scan" }
|
||||||
func (*RunCmd) Synopsis() string { return "Check and process all the folder" }
|
func (*RunCmd) Synopsis() string { return "check and process all the folder" }
|
||||||
func (*RunCmd) Usage() string {
|
func (*RunCmd) Usage() string {
|
||||||
return `run:
|
return `Usage: cloudsave scan
|
||||||
Check and process all the folder
|
|
||||||
|
Check if the files have been modified. If so,
|
||||||
|
the current archive is moved to the backup list
|
||||||
|
and a new archive is created with a new version number.
|
||||||
`
|
`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -27,8 +27,9 @@ type (
|
|||||||
func (*SyncCmd) Name() string { return "sync" }
|
func (*SyncCmd) Name() string { return "sync" }
|
||||||
func (*SyncCmd) Synopsis() string { return "list all game registered" }
|
func (*SyncCmd) Synopsis() string { return "list all game registered" }
|
||||||
func (*SyncCmd) Usage() string {
|
func (*SyncCmd) Usage() string {
|
||||||
return `add:
|
return `Usage: cloudsave sync
|
||||||
List all game registered
|
|
||||||
|
Synchronize the archives with the server defined for each game.
|
||||||
`
|
`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,6 +48,7 @@ func (p *SyncCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})
|
|||||||
r, err := remote.One(g.ID)
|
r, err := remote.One(g.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, remote.ErrNoRemote) {
|
if errors.Is(err, remote.ErrNoRemote) {
|
||||||
|
fmt.Println(g.Name + ": no remote configured")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
fmt.Fprintln(os.Stderr, "error: failed to load datastore:", err)
|
fmt.Fprintln(os.Stderr, "error: failed to load datastore:", err)
|
||||||
@@ -85,6 +87,7 @@ func (p *SyncCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})
|
|||||||
destroyPg()
|
destroyPg()
|
||||||
slog.Warn("failed to push backup files", "err", err)
|
slog.Warn("failed to push backup files", "err", err)
|
||||||
}
|
}
|
||||||
|
fmt.Println(g.Name + ": pushed")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,7 +139,7 @@ func (p *SyncCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt.Println("already up-to-date")
|
fmt.Println(g.Name + ": already up-to-date")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -148,6 +151,7 @@ func (p *SyncCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})
|
|||||||
return subcommands.ExitFailure
|
return subcommands.ExitFailure
|
||||||
}
|
}
|
||||||
destroyPg()
|
destroyPg()
|
||||||
|
fmt.Println(g.Name + ": pushed")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,6 +172,7 @@ func (p *SyncCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})
|
|||||||
fmt.Fprintln(os.Stderr, "error: failed to synchronize date:", err)
|
fmt.Fprintln(os.Stderr, "error: failed to synchronize date:", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
fmt.Println(g.Name + ": pulled")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
package version
|
package version
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"cloudsave/cmd/cli/tools/prompt/credentials"
|
||||||
"cloudsave/pkg/constants"
|
"cloudsave/pkg/constants"
|
||||||
"cloudsave/pkg/remote/client"
|
"cloudsave/pkg/remote/client"
|
||||||
"cloudsave/cmd/cli/tools/prompt/credentials"
|
|
||||||
"context"
|
"context"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -23,8 +23,11 @@ type (
|
|||||||
func (*VersionCmd) Name() string { return "version" }
|
func (*VersionCmd) Name() string { return "version" }
|
||||||
func (*VersionCmd) Synopsis() string { return "show version and system information" }
|
func (*VersionCmd) Synopsis() string { return "show version and system information" }
|
||||||
func (*VersionCmd) Usage() string {
|
func (*VersionCmd) Usage() string {
|
||||||
return `add:
|
return `Usage: cloudsave version [-a]
|
||||||
Show version and system information
|
|
||||||
|
Print the version of the software
|
||||||
|
|
||||||
|
Options:
|
||||||
`
|
`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,12 +9,50 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
cache map[string]cachedInfo
|
||||||
|
|
||||||
|
cachedInfo struct {
|
||||||
|
MD5 string
|
||||||
|
Version int
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrBackupNotExists error = errors.New("backup not found")
|
ErrBackupNotExists error = errors.New("backup not found")
|
||||||
|
|
||||||
|
// singleton
|
||||||
|
hashCacheMu sync.RWMutex
|
||||||
|
hashCache cache = make(map[string]cachedInfo)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (c cache) Get(gameID string) (cachedInfo, bool) {
|
||||||
|
hashCacheMu.RLock()
|
||||||
|
defer hashCacheMu.RUnlock()
|
||||||
|
|
||||||
|
if v, ok := c[gameID]; ok {
|
||||||
|
return v, true
|
||||||
|
}
|
||||||
|
return cachedInfo{}, false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c cache) Register(gameID string, v cachedInfo) {
|
||||||
|
hashCacheMu.Lock()
|
||||||
|
defer hashCacheMu.Unlock()
|
||||||
|
|
||||||
|
c[gameID] = v
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c cache) Remove(gameID string) {
|
||||||
|
hashCacheMu.Lock()
|
||||||
|
defer hashCacheMu.Unlock()
|
||||||
|
|
||||||
|
delete(c, gameID)
|
||||||
|
}
|
||||||
|
|
||||||
func Write(gameID, documentRoot string, r io.Reader) error {
|
func Write(gameID, documentRoot string, r io.Reader) error {
|
||||||
dataFolderPath := filepath.Join(documentRoot, "data", gameID)
|
dataFolderPath := filepath.Join(documentRoot, "data", gameID)
|
||||||
partPath := filepath.Join(dataFolderPath, "data.tar.gz.part")
|
partPath := filepath.Join(dataFolderPath, "data.tar.gz.part")
|
||||||
@@ -42,6 +80,7 @@ func Write(gameID, documentRoot string, r io.Reader) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hashCache.Remove(gameID)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,6 +136,7 @@ func UpdateMetadata(gameID, documentRoot string, m repository.Metadata) error {
|
|||||||
|
|
||||||
func ArchiveInfo(gameID, documentRoot, uuid string) (repository.Backup, error) {
|
func ArchiveInfo(gameID, documentRoot, uuid string) (repository.Backup, error) {
|
||||||
dataFolderPath := filepath.Join(documentRoot, "data", gameID, "hist", uuid, "data.tar.gz")
|
dataFolderPath := filepath.Join(documentRoot, "data", gameID, "hist", uuid, "data.tar.gz")
|
||||||
|
cacheID := gameID + ":" + uuid
|
||||||
|
|
||||||
finfo, err := os.Stat(dataFolderPath)
|
finfo, err := os.Stat(dataFolderPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -106,11 +146,29 @@ func ArchiveInfo(gameID, documentRoot, uuid string) (repository.Backup, error) {
|
|||||||
return repository.Backup{}, err
|
return repository.Backup{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
v, err := getVersion(gameID, documentRoot)
|
||||||
|
if err != nil {
|
||||||
|
return repository.Backup{}, fmt.Errorf("failed to read game metadata: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if m, ok := hashCache.Get(cacheID); ok {
|
||||||
|
return repository.Backup{
|
||||||
|
CreatedAt: finfo.ModTime(),
|
||||||
|
UUID: uuid,
|
||||||
|
MD5: m.MD5,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
h, err := hash.FileMD5(dataFolderPath)
|
h, err := hash.FileMD5(dataFolderPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return repository.Backup{}, fmt.Errorf("failed to calculate file md5: %w", err)
|
return repository.Backup{}, fmt.Errorf("failed to calculate file md5: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hashCache.Register(cacheID, cachedInfo{
|
||||||
|
Version: v,
|
||||||
|
MD5: h,
|
||||||
|
})
|
||||||
|
|
||||||
return repository.Backup{
|
return repository.Backup{
|
||||||
CreatedAt: finfo.ModTime(),
|
CreatedAt: finfo.ModTime(),
|
||||||
UUID: uuid,
|
UUID: uuid,
|
||||||
@@ -118,6 +176,62 @@ func ArchiveInfo(gameID, documentRoot, uuid string) (repository.Backup, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Hash(gameID, documentRoot string) (string, error) {
|
||||||
|
path := filepath.Clean(filepath.Join(documentRoot, "data", gameID))
|
||||||
|
|
||||||
|
sdir, err := os.Stat(path)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !sdir.IsDir() {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
v, err := getVersion(gameID, documentRoot)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("failed to read game metadata: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if m, ok := hashCache.Get(gameID); ok {
|
||||||
|
if v == m.Version {
|
||||||
|
return m.MD5, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
path = filepath.Join(path, "data.tar.gz")
|
||||||
|
|
||||||
|
h, err := hash.FileMD5(path)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
hashCache.Register(gameID, cachedInfo{
|
||||||
|
Version: v,
|
||||||
|
MD5: h,
|
||||||
|
})
|
||||||
|
|
||||||
|
return h, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getVersion(gameID, documentRoot string) (int, error) {
|
||||||
|
path := filepath.Join(documentRoot, "data", gameID, "metadata.json")
|
||||||
|
|
||||||
|
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0740)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
d := json.NewDecoder(f)
|
||||||
|
var m repository.Metadata
|
||||||
|
if err := d.Decode(&m); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return m.Version, nil
|
||||||
|
}
|
||||||
|
|
||||||
func makeDataFolder(gameID, documentRoot string) error {
|
func makeDataFolder(gameID, documentRoot string) error {
|
||||||
if err := os.MkdirAll(filepath.Join(documentRoot, "data", gameID), 0740); err != nil {
|
if err := os.MkdirAll(filepath.Join(documentRoot, "data", gameID), 0740); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
Reference in New Issue
Block a user