Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b4811fba4a | |||
| b314a683c9 | |||
| 8e18f2ce76 | |||
| 1c89df0673 | |||
| 1239c5ed6b | |||
| 03818e20e5 | |||
| b36142c309 | |||
| 57fc77755e | |||
| 5f7ca22b8f | |||
| d15de3c6a1 | |||
| f56d3c5857 | |||
| 7e5d8855d4 | |||
| e6ca29a7aa |
13
.dockerignore
Normal file
13
.dockerignore
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
/cli
|
||||||
|
/server
|
||||||
|
/web
|
||||||
|
/gui
|
||||||
|
/env/
|
||||||
|
/build/
|
||||||
|
*.exe
|
||||||
|
/config.json
|
||||||
|
/.codex
|
||||||
|
/.vscode
|
||||||
|
|
||||||
|
# for docker-compose.yml
|
||||||
|
/data
|
||||||
8
.gitignore
vendored
8
.gitignore
vendored
@@ -1,7 +1,13 @@
|
|||||||
/cli
|
/cli
|
||||||
/server
|
/server
|
||||||
/web
|
/web
|
||||||
|
/gui
|
||||||
/env/
|
/env/
|
||||||
/build/
|
/build/
|
||||||
*.exe
|
*.exe
|
||||||
/config.json
|
/config.json
|
||||||
|
/.codex
|
||||||
|
/.vscode
|
||||||
|
|
||||||
|
# for docker-compose.yml
|
||||||
|
/data
|
||||||
260
README.md
260
README.md
@@ -1,58 +1,270 @@
|
|||||||
# CloudSave
|
# CloudSave
|
||||||
|
|
||||||
The software is still in alpha.
|
CloudSave is a small client/server tool to keep save folders in sync across multiple computers.
|
||||||
|
It is aimed at games that do not provide their own cloud sync, such as emulators, old games, or any title that stores progress in a local directory.
|
||||||
|
|
||||||
A client/server that allows unsynchronized games (such as emulators, old games, etc.) to be kept up to date on multiple computers.
|
The project is still in alpha.
|
||||||
|
|
||||||
|
## What Is In The Repository
|
||||||
|
|
||||||
|
This repository currently contains three Go binaries:
|
||||||
|
|
||||||
|
- `cmd/cli`: the end-user CLI (`cloudsave`)
|
||||||
|
- `cmd/server`: the HTTP API server
|
||||||
|
- `cmd/web`: a small read-only web UI that talks to the API server
|
||||||
|
|
||||||
## Build
|
## Build
|
||||||
|
|
||||||
You need go1.24
|
The module targets Go `1.24` in [go.mod](/home/aurelie/src/cloudsave/go.mod), while the container image builds with Go `1.26.3` from [dockerfile](/home/aurelie/src/cloudsave/dockerfile). In practice, using a recent Go toolchain is recommended.
|
||||||
|
|
||||||
After downloading the go toolchain, just run the script `./build.sh`
|
To build all binaries for the platforms configured in the project:
|
||||||
|
|
||||||
## Usage
|
```bash
|
||||||
|
./build.sh
|
||||||
### Server
|
|
||||||
|
|
||||||
The server needs an empty directory. After creating this directory, you need to make a file that contains your credential. The format is "username:password". The server only understand bcrypt password hash for now.
|
|
||||||
|
|
||||||
e.g.:
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Artifacts are written to `./build`.
|
||||||
|
|
||||||
|
If you only want one binary, you can also build it directly:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
go build -o cloudsave ./cmd/cli
|
||||||
|
go build -o cloudsave_server ./cmd/server
|
||||||
|
go build -o cloudsave_web ./cmd/web
|
||||||
|
```
|
||||||
|
|
||||||
|
## Server
|
||||||
|
|
||||||
|
The server exposes an authenticated HTTP API on port `8080` by default.
|
||||||
|
|
||||||
|
### Data Directory
|
||||||
|
|
||||||
|
By default, the server uses:
|
||||||
|
|
||||||
|
```text
|
||||||
|
/var/lib/cloudsave
|
||||||
|
```
|
||||||
|
|
||||||
|
You can override it with:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cloudsave_server -document-root /path/to/cloudsave-data
|
||||||
|
```
|
||||||
|
|
||||||
|
Inside this directory, the server expects:
|
||||||
|
|
||||||
|
- `.htpasswd`: credentials file
|
||||||
|
- `data/`: stored save archives and metadata
|
||||||
|
|
||||||
|
### Authentication
|
||||||
|
|
||||||
|
The API uses HTTP Basic Auth. Credentials are read from `.htpasswd`.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
```text
|
||||||
test:$2y$10$uULsuyROe3LVdTzFoBH7HO0zhvyKp6CX2FDNl7quXMFYqzitU0kc.
|
test:$2y$10$uULsuyROe3LVdTzFoBH7HO0zhvyKp6CX2FDNl7quXMFYqzitU0kc.
|
||||||
```
|
```
|
||||||
|
|
||||||
To generate bcrypt password, I recommand [hash_utils](https://git.thelilfrog.com/thelilfrog/hash_utils), which is offline and secure
|
The code currently expects bcrypt hashes when validating passwords.
|
||||||
|
|
||||||
The default path to this directory is `/var/lib/cloudsave`, this can be changed with the `-document-root` argument
|
### Start The Server
|
||||||
|
|
||||||
### Client
|
```bash
|
||||||
|
cloudsave_server
|
||||||
|
```
|
||||||
|
|
||||||
#### Register a game
|
Useful flags from [cmd/server/runner.go](/home/aurelie/src/cloudsave/cmd/server/runner.go):
|
||||||
|
|
||||||
|
- `-document-root`: change the storage directory
|
||||||
|
- `-port`: change the listening port
|
||||||
|
- `-no-cache`: use the lazy repository instead of the eager cache
|
||||||
|
- `-verbose`: enable more logs
|
||||||
|
|
||||||
|
On non-Windows systems, sending `SIGHUP` reloads the eager cache and the `.htpasswd` file.
|
||||||
|
|
||||||
|
## Docker
|
||||||
|
|
||||||
|
The repository contains a server-only container setup:
|
||||||
|
|
||||||
|
- [dockerfile](/home/aurelie/src/cloudsave/dockerfile)
|
||||||
|
- [docker-compose.yml](/home/aurelie/src/cloudsave/docker-compose.yml)
|
||||||
|
|
||||||
|
Run it with:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker compose up --build
|
||||||
|
```
|
||||||
|
|
||||||
|
This maps:
|
||||||
|
|
||||||
|
- port `8080`
|
||||||
|
- local `./data` to `/var/lib/cloudsave`
|
||||||
|
|
||||||
|
Before starting the container, you still need to create `./data/.htpasswd`.
|
||||||
|
|
||||||
|
## Client
|
||||||
|
|
||||||
|
The CLI stores its local database in the user config directory under `cloudsave/data`.
|
||||||
|
|
||||||
|
It keeps:
|
||||||
|
|
||||||
|
- per-game metadata
|
||||||
|
- current local archive
|
||||||
|
- backup archives
|
||||||
|
- `remote.json` per game when a remote is configured
|
||||||
|
|
||||||
|
Saved credentials are stored separately in `credential.json`.
|
||||||
|
|
||||||
|
Important: the `login` command stores credentials in plain text. This is also stated in the code.
|
||||||
|
|
||||||
|
## Typical Workflow
|
||||||
|
|
||||||
|
### 1. Register a game
|
||||||
|
|
||||||
You can register a game with the verb `add`
|
|
||||||
```bash
|
```bash
|
||||||
cloudsave add /home/user/gamedata
|
cloudsave add /home/user/gamedata
|
||||||
```
|
```
|
||||||
|
|
||||||
You can also change the name of the registration and add a remote
|
You can override the displayed name:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
cloudsave add -name "My Game" -remote "http://localhost:8080" /home/user/gamedata
|
cloudsave add -name "My Game" /home/user/gamedata
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Make an archive of the current state
|
Note: the `-remote` flag exists on `add`, but the current implementation does not persist it. Use `cloudsave remote -set` after `add`.
|
||||||
|
|
||||||
This is a command line tool, it cannot auto detect changes.
|
### 2. List registered games
|
||||||
Run this command to start the scan, if needed, the tool will create a new archive
|
|
||||||
|
```bash
|
||||||
|
cloudsave list
|
||||||
|
```
|
||||||
|
|
||||||
|
To include local backup IDs:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cloudsave list -include-backup
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. Create or refresh the local archive
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
cloudsave scan
|
cloudsave scan
|
||||||
```
|
```
|
||||||
#### Send everything on the server
|
|
||||||
|
|
||||||
This will pull and push data to the server.
|
This scans all registered folders. If a folder changed since the last scan, the current archive is moved to the backup history and a new `data.tar.gz` archive is created.
|
||||||
|
|
||||||
Note: If multiple computers are pushing to this server, a conflict may be generated. If so, the tool will ask for the version to keep
|
### 4. Configure the remote server for a game
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cloudsave remote -set GAME_ID http://localhost:8080
|
||||||
|
```
|
||||||
|
|
||||||
|
To list configured remotes:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cloudsave remote -list
|
||||||
|
```
|
||||||
|
|
||||||
|
### 5. Save credentials locally
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cloudsave login http://localhost:8080
|
||||||
|
```
|
||||||
|
|
||||||
|
This verifies the credentials against the server and then stores them locally in plain text.
|
||||||
|
|
||||||
|
### 6. Synchronize with the server
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
cloudsave sync
|
cloudsave sync
|
||||||
```
|
```
|
||||||
|
|
||||||
|
The sync command:
|
||||||
|
|
||||||
|
- groups games by remote URL
|
||||||
|
- authenticates once per remote
|
||||||
|
- compares local and remote metadata
|
||||||
|
- pushes or pulls as needed
|
||||||
|
- asks for a resolution if versions conflict
|
||||||
|
|
||||||
|
### 7. Restore a save locally
|
||||||
|
|
||||||
|
Apply the latest local archive for a game:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cloudsave apply GAME_ID
|
||||||
|
```
|
||||||
|
|
||||||
|
Apply a specific backup:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cloudsave apply GAME_ID BACKUP_ID
|
||||||
|
```
|
||||||
|
|
||||||
|
## Other CLI Commands
|
||||||
|
|
||||||
|
Show metadata for one game:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cloudsave show GAME_ID
|
||||||
|
```
|
||||||
|
|
||||||
|
Pull one game and its backups from a remote into a local path:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cloudsave pull http://localhost:8080 GAME_ID /path/to/restore
|
||||||
|
```
|
||||||
|
|
||||||
|
Show local version information:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cloudsave version
|
||||||
|
```
|
||||||
|
|
||||||
|
Show remote version information:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cloudsave version -a http://localhost:8080
|
||||||
|
```
|
||||||
|
|
||||||
|
Remove a registered game and its local backups:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cloudsave remove GAME_ID
|
||||||
|
```
|
||||||
|
|
||||||
|
## Web UI
|
||||||
|
|
||||||
|
The repository also contains a small web frontend in `cmd/web`.
|
||||||
|
|
||||||
|
It uses a JSON config file, for example:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"server": {
|
||||||
|
"port": 8081
|
||||||
|
},
|
||||||
|
"remote": {
|
||||||
|
"url": "http://localhost:8080"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Then start it with:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cloudsave_web -config /path/to/config.json
|
||||||
|
```
|
||||||
|
|
||||||
|
The web UI itself does not manage users. It forwards HTTP Basic Auth credentials to the configured API server.
|
||||||
|
|
||||||
|
## Current Caveats
|
||||||
|
|
||||||
|
These points are worth knowing in the current state of the project:
|
||||||
|
|
||||||
|
- the software is still alpha
|
||||||
|
- credentials saved by `cloudsave login` are stored in plain text
|
||||||
|
- the Docker setup only runs the API server, not the web UI
|
||||||
|
- `add -remote` is exposed by the CLI but is not currently persisted by the service layer
|
||||||
|
|
||||||
|
|||||||
24
api.Dockerfile
Normal file
24
api.Dockerfile
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
FROM golang:1.26.3-trixie AS build
|
||||||
|
|
||||||
|
ENV GOOS=linux
|
||||||
|
ENV CGO_ENABLED=0
|
||||||
|
|
||||||
|
ENV GOAMD64=v3
|
||||||
|
ENV GORISCV64=rva22u64
|
||||||
|
ENV GOARM64=v8.2
|
||||||
|
|
||||||
|
COPY . /src
|
||||||
|
|
||||||
|
RUN cd /src \
|
||||||
|
&& go build -ldflags="-s -w" -o server ./cmd/server \
|
||||||
|
&& chown 0:0 server \
|
||||||
|
&& chmod ugo+x server
|
||||||
|
|
||||||
|
FROM busybox:1.37.0 AS prod
|
||||||
|
|
||||||
|
COPY --from=build /etc/passwd /etc/passwd
|
||||||
|
COPY --from=build /etc/shadow /etc/shadow
|
||||||
|
COPY --from=build /src/server /server
|
||||||
|
|
||||||
|
VOLUME [ "/var/lib/cloudsave" ]
|
||||||
|
ENTRYPOINT [ "/server" ]
|
||||||
@@ -32,7 +32,8 @@ func (p *LoginCmd) SetFlags(f *flag.FlagSet) {
|
|||||||
|
|
||||||
func (p *LoginCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
|
func (p *LoginCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
|
||||||
if f.NArg() != 1 {
|
if f.NArg() != 1 {
|
||||||
fmt.Fprintf(os.Stderr, "error: this command take 1 argument")
|
fmt.Fprintln(os.Stderr, "error: this command take 1 argument")
|
||||||
|
|
||||||
return subcommands.ExitUsageError
|
return subcommands.ExitUsageError
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,12 +47,12 @@ func (p *LoginCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})
|
|||||||
|
|
||||||
cli := client.New(server, username, password)
|
cli := client.New(server, username, password)
|
||||||
if _, err := cli.Version(); err != nil {
|
if _, err := cli.Version(); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "error: failed to login: %s", err)
|
fmt.Fprintf(os.Stderr, "error: failed to login: %s\n", err)
|
||||||
return subcommands.ExitFailure
|
return subcommands.ExitFailure
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := credentials.Login(username, password, server); err != nil {
|
if err := credentials.Login(username, password, server); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "error: failed to save login: %s", err)
|
fmt.Fprintf(os.Stderr, "error: failed to save login: %s\n", err)
|
||||||
return subcommands.ExitFailure
|
return subcommands.ExitFailure
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -27,9 +27,11 @@ func (*RemoteCmd) Usage() string {
|
|||||||
|
|
||||||
The -list argument lists all remotes for each registered game.
|
The -list argument lists all remotes for each registered game.
|
||||||
This command performs a connection test.
|
This command performs a connection test.
|
||||||
|
Usage: cloudsave remote -list
|
||||||
|
|
||||||
The -set argument allow you to set (create or update)
|
The -set argument allow you to set (create or update)
|
||||||
the URL to the remote for a game
|
the URL to the remote for a game
|
||||||
|
Usage: cloudsave remote -set GAME_ID REMOTE_URL
|
||||||
|
|
||||||
Options
|
Options
|
||||||
`
|
`
|
||||||
|
|||||||
@@ -7,11 +7,10 @@ import (
|
|||||||
"cloudsave/pkg/remote"
|
"cloudsave/pkg/remote"
|
||||||
"cloudsave/pkg/remote/client"
|
"cloudsave/pkg/remote/client"
|
||||||
"cloudsave/pkg/repository"
|
"cloudsave/pkg/repository"
|
||||||
|
"cloudsave/pkg/sync"
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -38,247 +37,92 @@ func (p *SyncCmd) SetFlags(f *flag.FlagSet) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *SyncCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
|
func (p *SyncCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
|
||||||
games, err := p.Service.AllGames()
|
|
||||||
|
remoteCred := make(map[string]map[string]string)
|
||||||
|
rs, err := remote.All()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr, "error: failed to load datastore:", err)
|
fmt.Fprintln(os.Stderr, "error: failed to connect to the remote:", err)
|
||||||
return subcommands.ExitFailure
|
return subcommands.ExitFailure
|
||||||
}
|
}
|
||||||
|
|
||||||
remoteCred := make(map[string]map[string]string)
|
done := make(map[string]struct{})
|
||||||
for _, g := range games {
|
for _, r := range rs {
|
||||||
r, err := remote.One(g.ID)
|
if _, ok := done[r.URL]; ok {
|
||||||
if err != nil {
|
continue
|
||||||
if errors.Is(err, remote.ErrNoRemote) {
|
|
||||||
fmt.Println("⬛", g.Name+": no remote configured")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
fmt.Fprintln(os.Stderr, "error: failed to load datastore:", err)
|
|
||||||
return subcommands.ExitFailure
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cli, err := connect(remoteCred, r)
|
cli, err := connect(remoteCred, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr, "error: failed to connect to the remote:", err)
|
fmt.Fprintln(os.Stderr, "error: failed to connect to the remote:", err)
|
||||||
return subcommands.ExitFailure
|
return subcommands.ExitFailure
|
||||||
}
|
}
|
||||||
|
|
||||||
pg := progressbar.New(-1)
|
fmt.Println()
|
||||||
|
|
||||||
|
done[r.URL] = struct{}{}
|
||||||
|
syncer := sync.NewSyncer(cli, p.Service)
|
||||||
|
|
||||||
|
var pg *progressbar.ProgressBar
|
||||||
destroyPg := func() {
|
destroyPg := func() {
|
||||||
if err := pg.Finish(); err != nil {
|
pg.Finish()
|
||||||
slog.Error("failed to finish progressbar", "err", err)
|
pg.Clear()
|
||||||
}
|
pg.Close()
|
||||||
if err := pg.Clear(); err != nil {
|
|
||||||
slog.Error("failed to clear progressbar", "err", err)
|
|
||||||
}
|
|
||||||
if err := pg.Close(); err != nil {
|
|
||||||
slog.Error("failed to close progressbar", "err", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pg.Describe(fmt.Sprintf("[%s] Checking status...", g.Name))
|
syncer.SetStateCallback(func(s sync.State, g repository.Metadata) {
|
||||||
exists, err := cli.Exists(r.GameID)
|
switch s {
|
||||||
if err != nil {
|
case sync.FetchingMetdata:
|
||||||
slog.Error(err.Error())
|
pg = progressbar.New(-1)
|
||||||
continue
|
pg.Describe(fmt.Sprintf("%s: fetching metadata from repository", g.Name))
|
||||||
}
|
case sync.Pushing:
|
||||||
|
pg.Describe(fmt.Sprintf("%s: pushing data to the server", g.Name))
|
||||||
if !exists {
|
case sync.Pulling:
|
||||||
pg.Describe(fmt.Sprintf("[%s] Pushing data...", g.Name))
|
pg.Describe(fmt.Sprintf("%s: pull data from the server", g.Name))
|
||||||
if err := p.push(g, cli); err != nil {
|
case sync.UpToDate:
|
||||||
destroyPg()
|
destroyPg()
|
||||||
fmt.Fprintln(os.Stderr, "failed to push:", err)
|
fmt.Println("🆗", g.Name+": already up-to-date")
|
||||||
return subcommands.ExitFailure
|
case sync.Pushed:
|
||||||
}
|
|
||||||
pg.Describe(fmt.Sprintf("[%s] Pushing backup...", g.Name))
|
|
||||||
if err := p.pushBackup(g, cli); err != nil {
|
|
||||||
destroyPg()
|
destroyPg()
|
||||||
slog.Warn("failed to push backup files", "err", err)
|
fmt.Println("⬆️", g.Name+": pushed")
|
||||||
}
|
case sync.Pulled:
|
||||||
destroyPg()
|
|
||||||
fmt.Println("⬆️", g.Name+": pushed")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
pg.Describe(fmt.Sprintf("[%s] Fetching metadata...", g.Name))
|
|
||||||
|
|
||||||
remoteMetadata, err := cli.Metadata(r.GameID)
|
|
||||||
if err != nil {
|
|
||||||
destroyPg()
|
|
||||||
fmt.Fprintln(os.Stderr, "error: failed to get the game metadata from the remote:", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
pg.Describe(fmt.Sprintf("[%s] Pulling backup...", g.Name))
|
|
||||||
if err := p.pullBackup(g, cli); err != nil {
|
|
||||||
slog.Warn("failed to pull backup files", "err", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
pg.Describe(fmt.Sprintf("[%s] Pushing backup...", g.Name))
|
|
||||||
if err := p.pushBackup(g, cli); err != nil {
|
|
||||||
slog.Warn("failed to push backup files", "err", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if g.MD5 == remoteMetadata.MD5 {
|
|
||||||
destroyPg()
|
|
||||||
if g.Version != remoteMetadata.Version {
|
|
||||||
slog.Debug("version is not the same, but the hash is equal. Updating local database")
|
|
||||||
if err := p.Service.SetVersion(r.GameID, remoteMetadata.Version); err != nil {
|
|
||||||
fmt.Fprintln(os.Stderr, "error: failed to synchronize version number:", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fmt.Println("🆗", g.Name+": already up-to-date")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if g.Version > remoteMetadata.Version {
|
|
||||||
pg.Describe(fmt.Sprintf("[%s] Pushing data...", g.Name))
|
|
||||||
if err := p.push(g, cli); err != nil {
|
|
||||||
destroyPg()
|
destroyPg()
|
||||||
fmt.Fprintln(os.Stderr, "failed to push:", err)
|
fmt.Println("⬇️", g.Name+": pulled")
|
||||||
return subcommands.ExitFailure
|
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
syncer.SetErrorCallback(func(err error, g repository.Metadata) {
|
||||||
destroyPg()
|
destroyPg()
|
||||||
fmt.Println("⬆️", g.Name+": pushed")
|
fmt.Println("❌", g.Name+": "+err.Error())
|
||||||
continue
|
})
|
||||||
}
|
|
||||||
|
|
||||||
if g.Version < remoteMetadata.Version {
|
syncer.SetConflictCallback(func(a, b repository.Metadata) sync.ConflictResolution {
|
||||||
destroyPg()
|
fmt.Println()
|
||||||
if err := p.pull(g, cli); err != nil {
|
fmt.Println("--- ⚠️ CONFLICT ---")
|
||||||
destroyPg()
|
fmt.Println(a.Name, "(", a.Path, ")")
|
||||||
fmt.Fprintln(os.Stderr, "failed to push:", err)
|
fmt.Println("----")
|
||||||
return subcommands.ExitFailure
|
fmt.Println("Your version:", a.Date.Format(time.RFC1123))
|
||||||
|
fmt.Println("Their version:", b.Date.Format(time.RFC1123))
|
||||||
|
fmt.Println()
|
||||||
|
|
||||||
|
res := prompt.Conflict()
|
||||||
|
|
||||||
|
switch res {
|
||||||
|
case prompt.Their:
|
||||||
|
return sync.Their
|
||||||
|
case prompt.My:
|
||||||
|
return sync.Mine
|
||||||
}
|
}
|
||||||
|
|
||||||
g.Version = remoteMetadata.Version
|
return sync.None
|
||||||
g.Date = remoteMetadata.Date
|
})
|
||||||
|
|
||||||
if err := p.Service.UpdateMetadata(g.ID, g); err != nil {
|
syncer.Sync()
|
||||||
destroyPg()
|
|
||||||
fmt.Fprintln(os.Stderr, "failed to push:", err)
|
|
||||||
return subcommands.ExitFailure
|
|
||||||
}
|
|
||||||
fmt.Println("⬇️", g.Name+": pulled")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
destroyPg()
|
|
||||||
|
|
||||||
if g.Version == remoteMetadata.Version {
|
|
||||||
if err := p.conflict(r.GameID, g, remoteMetadata, cli); err != nil {
|
|
||||||
fmt.Fprintln(os.Stderr, "error: failed to resolve conflict:", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("done.")
|
fmt.Println("done.")
|
||||||
return subcommands.ExitSuccess
|
return subcommands.ExitSuccess
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *SyncCmd) conflict(gameID string, m, remoteMetadata repository.Metadata, cli *client.Client) error {
|
|
||||||
g, err := p.Service.One(gameID)
|
|
||||||
if err != nil {
|
|
||||||
slog.Warn("a conflict was found but the game is not found in the database")
|
|
||||||
slog.Debug("debug info", "gameID", gameID)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
fmt.Println()
|
|
||||||
fmt.Println("--- ⚠️ CONFLICT ---")
|
|
||||||
fmt.Println(g.Name, "(", g.Path, ")")
|
|
||||||
fmt.Println("----")
|
|
||||||
fmt.Println("Your version:", g.Date.Format(time.RFC1123))
|
|
||||||
fmt.Println("Their version:", remoteMetadata.Date.Format(time.RFC1123))
|
|
||||||
fmt.Println()
|
|
||||||
|
|
||||||
res := prompt.Conflict()
|
|
||||||
|
|
||||||
switch res {
|
|
||||||
case prompt.My:
|
|
||||||
{
|
|
||||||
if err := p.push(m, cli); err != nil {
|
|
||||||
return fmt.Errorf("failed to push: %w", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
case prompt.Their:
|
|
||||||
{
|
|
||||||
if err := p.pull(g, cli); err != nil {
|
|
||||||
return fmt.Errorf("failed to push: %w", err)
|
|
||||||
}
|
|
||||||
g.Version = remoteMetadata.Version
|
|
||||||
g.Date = remoteMetadata.Date
|
|
||||||
|
|
||||||
if err := p.Service.UpdateMetadata(g.ID, g); err != nil {
|
|
||||||
return fmt.Errorf("failed to push: %w", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *SyncCmd) push(m repository.Metadata, cli *client.Client) error {
|
|
||||||
return p.Service.PushArchive(m.ID, "", cli)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *SyncCmd) pushBackup(m repository.Metadata, cli *client.Client) error {
|
|
||||||
bs, err := p.Service.AllBackups(m.ID)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, b := range bs {
|
|
||||||
binfo, err := cli.ArchiveInfo(m.ID, b.UUID)
|
|
||||||
if err != nil {
|
|
||||||
if !errors.Is(err, client.ErrNotFound) {
|
|
||||||
return fmt.Errorf("failed to get remote information about the backup file: %w", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if binfo.MD5 != b.MD5 {
|
|
||||||
if err := cli.PushBackup(b, m); err != nil {
|
|
||||||
return fmt.Errorf("failed to push backup: %w", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *SyncCmd) pullBackup(m repository.Metadata, cli *client.Client) error {
|
|
||||||
bs, err := cli.ListArchives(m.ID)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, uuid := range bs {
|
|
||||||
rinfo, err := cli.ArchiveInfo(m.ID, uuid)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
linfo, err := p.Service.Backup(m.ID, uuid)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if linfo.MD5 != rinfo.MD5 {
|
|
||||||
if err := p.Service.PullBackup(m.ID, uuid, cli); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *SyncCmd) pull(g repository.Metadata, cli *client.Client) error {
|
|
||||||
if err := p.Service.PullArchive(g.ID, "", cli); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return p.Service.ApplyCurrent(g.ID)
|
|
||||||
}
|
|
||||||
|
|
||||||
func connect(remoteCred map[string]map[string]string, r remote.Remote) (*client.Client, error) {
|
func connect(remoteCred map[string]map[string]string, r remote.Remote) (*client.Client, error) {
|
||||||
var cli *client.Client
|
var cli *client.Client
|
||||||
|
|
||||||
|
|||||||
@@ -44,7 +44,6 @@ func main() {
|
|||||||
|
|
||||||
subcommands.Register(subcommands.HelpCommand(), "help")
|
subcommands.Register(subcommands.HelpCommand(), "help")
|
||||||
subcommands.Register(subcommands.FlagsCommand(), "help")
|
subcommands.Register(subcommands.FlagsCommand(), "help")
|
||||||
subcommands.Register(subcommands.CommandsCommand(), "help")
|
|
||||||
subcommands.Register(&version.VersionCmd{}, "help")
|
subcommands.Register(&version.VersionCmd{}, "help")
|
||||||
|
|
||||||
subcommands.Register(&add.AddCmd{Service: s}, "management")
|
subcommands.Register(&add.AddCmd{Service: s}, "management")
|
||||||
|
|||||||
@@ -35,6 +35,17 @@ func init() {
|
|||||||
datastorePath = filepath.Join(roaming, "cloudsave")
|
datastorePath = filepath.Join(roaming, "cloudsave")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Get(server string) (string, string, error) {
|
||||||
|
var err error
|
||||||
|
store, err := load()
|
||||||
|
if err == nil {
|
||||||
|
if c, ok := store[server]; ok {
|
||||||
|
return c.Username, c.Password, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "","",fmt.Errorf("not found")
|
||||||
|
}
|
||||||
|
|
||||||
func Read(server string) (string, string, error) {
|
func Read(server string) (string, string, error) {
|
||||||
var err error
|
var err error
|
||||||
store, err := load()
|
store, err := load()
|
||||||
|
|||||||
@@ -30,6 +30,14 @@ func run(updateChan <-chan struct{}) {
|
|||||||
slog.SetLogLoggerLevel(slog.LevelDebug)
|
slog.SetLogLoggerLevel(slog.LevelDebug)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !filepath.IsAbs(documentRoot) {
|
||||||
|
if v, err := filepath.Abs(documentRoot); err == nil {
|
||||||
|
documentRoot = v
|
||||||
|
} else {
|
||||||
|
fatal("failed to get absolute path from document-root flag: "+err.Error(), 2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
slog.Info("loading .htpasswd")
|
slog.Info("loading .htpasswd")
|
||||||
h, err := htpasswd.Open(filepath.Join(documentRoot, ".htpasswd"))
|
h, err := htpasswd.Open(filepath.Join(documentRoot, ".htpasswd"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
63
docker-compose.yml
Normal file
63
docker-compose.yml
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
services:
|
||||||
|
api:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: api.Dockerfile
|
||||||
|
volumes:
|
||||||
|
- "./data:/var/lib/cloudsave"
|
||||||
|
networks:
|
||||||
|
- cloudsave_net
|
||||||
|
healthcheck:
|
||||||
|
test: wget --no-verbose --tries=1 --spider http://localhost:8080/heartbeat || exit 1
|
||||||
|
interval: 3s
|
||||||
|
timeout: 2s
|
||||||
|
retries: 3
|
||||||
|
start_period: 10s
|
||||||
|
labels:
|
||||||
|
- "traefik.enable=true"
|
||||||
|
- "traefik.http.routers.api-router.rule=Host(`${DOMAIN:-localhost}`) && PathPrefix(`/api`)"
|
||||||
|
- "traefik.http.routers.api-router.entrypoints=web"
|
||||||
|
- "traefik.http.services.cloudsave-api.loadbalancer.server.port=8080"
|
||||||
|
|
||||||
|
web:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: web.Dockerfile
|
||||||
|
volumes:
|
||||||
|
- "./config.json:/var/lib/cloudsave/config.json"
|
||||||
|
networks:
|
||||||
|
- cloudsave_net
|
||||||
|
depends_on:
|
||||||
|
api:
|
||||||
|
condition: service_healthy
|
||||||
|
labels:
|
||||||
|
- "traefik.enable=true"
|
||||||
|
- "traefik.http.routers.web-router.rule=Host(`${DOMAIN:-localhost}`) && PathPrefix(`/web`)"
|
||||||
|
- "traefik.http.routers.web-router.entrypoints=web"
|
||||||
|
- "traefik.http.services.cloudsave-web.loadbalancer.server.port=8080"
|
||||||
|
|
||||||
|
proxy:
|
||||||
|
image: traefik:3.7.0
|
||||||
|
ports:
|
||||||
|
- 127.0.0.1:80:80
|
||||||
|
- 127.0.0.1:8080:8080
|
||||||
|
networks:
|
||||||
|
- cloudsave_net
|
||||||
|
depends_on:
|
||||||
|
api:
|
||||||
|
condition: service_healthy
|
||||||
|
web:
|
||||||
|
condition: service_started
|
||||||
|
volumes:
|
||||||
|
- /var/run/docker.sock:/var/run/docker.sock:ro
|
||||||
|
command:
|
||||||
|
- --api.dashboard=true
|
||||||
|
- --api.insecure=true
|
||||||
|
- --providers.docker=true
|
||||||
|
- --providers.docker.exposedbydefault=false
|
||||||
|
- --entrypoints.web.address=:80
|
||||||
|
- --log.level=DEBUG
|
||||||
|
- --accesslog=true
|
||||||
|
|
||||||
|
networks:
|
||||||
|
cloudsave_net:
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
package constants
|
package constants
|
||||||
|
|
||||||
const Version = "0.0.4e"
|
const Version = "0.0.5"
|
||||||
|
|
||||||
const ApiVersion = 1
|
const ApiVersion = 1
|
||||||
|
|||||||
@@ -384,6 +384,10 @@ func (c *Client) All() ([]repository.Metadata, error) {
|
|||||||
return nil, errors.New("invalid payload sent by the server")
|
return nil, errors.New("invalid payload sent by the server")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) BaseURL() string {
|
||||||
|
return c.baseURL
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Client) get(url string) (obj.HTTPObject, error) {
|
func (c *Client) get(url string) (obj.HTTPObject, error) {
|
||||||
cli := http.Client{}
|
cli := http.Client{}
|
||||||
|
|
||||||
|
|||||||
@@ -57,6 +57,28 @@ func One(gameID string) (Remote, error) {
|
|||||||
return r, nil
|
return r, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func All() ([]Remote, error) {
|
||||||
|
d, err := os.ReadDir(filepath.Clean(datastorepath))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to load datastore: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var res []Remote
|
||||||
|
for _, g := range d {
|
||||||
|
r, err := One(g.Name())
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, ErrNoRemote) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("failed to load remote: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
res = append(res, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
|
||||||
func Set(gameID, url string) error {
|
func Set(gameID, url string) error {
|
||||||
r := Remote{
|
r := Remote{
|
||||||
URL: url,
|
URL: url,
|
||||||
|
|||||||
237
pkg/sync/sync.go
Normal file
237
pkg/sync/sync.go
Normal file
@@ -0,0 +1,237 @@
|
|||||||
|
package sync
|
||||||
|
|
||||||
|
import (
|
||||||
|
"cloudsave/pkg/data"
|
||||||
|
"cloudsave/pkg/remote"
|
||||||
|
"cloudsave/pkg/remote/client"
|
||||||
|
"cloudsave/pkg/repository"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
ConflictResolution int
|
||||||
|
State int
|
||||||
|
decision int
|
||||||
|
|
||||||
|
Syncer struct {
|
||||||
|
cli *client.Client
|
||||||
|
service *data.Service
|
||||||
|
|
||||||
|
stateCallback func(s State, g repository.Metadata)
|
||||||
|
errorCallback func(err error, g repository.Metadata)
|
||||||
|
conflictCallback func(a, b repository.Metadata) ConflictResolution
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
Their ConflictResolution = iota
|
||||||
|
Mine
|
||||||
|
None
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ignore decision = iota
|
||||||
|
push
|
||||||
|
pull
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
FetchingMetdata State = iota
|
||||||
|
Pushing
|
||||||
|
Pulling
|
||||||
|
Pushed
|
||||||
|
Pulled
|
||||||
|
UpToDate
|
||||||
|
Done
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrFetching error = errors.New("failed to fetch the metadata")
|
||||||
|
ErrPushing error = errors.New("failed to push data")
|
||||||
|
ErrPulling error = errors.New("failed to pull data")
|
||||||
|
ErrDatastore error = errors.New("failed to get data from local datastore")
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewSyncer(cli *client.Client, service *data.Service) *Syncer {
|
||||||
|
return &Syncer{
|
||||||
|
cli: cli,
|
||||||
|
service: service,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Syncer) SetStateCallback(fn func(s State, g repository.Metadata)) {
|
||||||
|
s.stateCallback = fn
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Syncer) SetErrorCallback(fn func(err error, g repository.Metadata)) {
|
||||||
|
s.errorCallback = fn
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Syncer) SetConflictCallback(fn func(a, b repository.Metadata) ConflictResolution) {
|
||||||
|
s.conflictCallback = fn
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Syncer) Sync() {
|
||||||
|
games, err := s.service.AllGames()
|
||||||
|
if err != nil {
|
||||||
|
s.errorCallback(fmt.Errorf("failed to get all games: %w", err), repository.Metadata{})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, g := range games {
|
||||||
|
r, err := remote.One(g.ID)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if r.URL != s.cli.BaseURL() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if err := s.sync(g); err != nil {
|
||||||
|
s.errorCallback(err, g)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s.stateCallback(Done, repository.Metadata{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Syncer) sync(g repository.Metadata) error {
|
||||||
|
s.stateCallback(FetchingMetdata, g)
|
||||||
|
|
||||||
|
remoteMetadata, err := s.cli.Metadata(g.ID)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, client.ErrNotFound) {
|
||||||
|
s.stateCallback(Pushing, g)
|
||||||
|
if err := s.push(g); err != nil {
|
||||||
|
return fmt.Errorf("%w: %s", ErrPushing, err)
|
||||||
|
}
|
||||||
|
s.stateCallback(Pushed, g)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return fmt.Errorf("%w: %s", ErrFetching, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if g.MD5 == remoteMetadata.MD5 {
|
||||||
|
s.stateCallback(UpToDate, g)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
d := ignore
|
||||||
|
|
||||||
|
if g.Version > remoteMetadata.Version {
|
||||||
|
d = push
|
||||||
|
}
|
||||||
|
|
||||||
|
if g.Version < remoteMetadata.Version {
|
||||||
|
d = pull
|
||||||
|
}
|
||||||
|
|
||||||
|
if g.Version == remoteMetadata.Version {
|
||||||
|
r := s.conflictCallback(g, remoteMetadata)
|
||||||
|
switch r {
|
||||||
|
case Mine:
|
||||||
|
{
|
||||||
|
d = push
|
||||||
|
}
|
||||||
|
case Their:
|
||||||
|
{
|
||||||
|
d = pull
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
switch d {
|
||||||
|
case push:
|
||||||
|
{
|
||||||
|
s.stateCallback(Pushing, g)
|
||||||
|
if err := s.push(g); err != nil {
|
||||||
|
return fmt.Errorf("%w: %s", ErrPushing, err)
|
||||||
|
}
|
||||||
|
s.stateCallback(Pushed, g)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
case pull:
|
||||||
|
{
|
||||||
|
s.stateCallback(Pulling, g)
|
||||||
|
if err := s.pull(g, remoteMetadata); err != nil {
|
||||||
|
return fmt.Errorf("%w: %s", ErrPulling, err)
|
||||||
|
}
|
||||||
|
s.stateCallback(Pulled, g)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Syncer) push(g repository.Metadata) error {
|
||||||
|
if err := s.service.PushArchive(g.ID, "", s.cli); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// manage backup
|
||||||
|
bs, err := s.service.AllBackups(g.ID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, b := range bs {
|
||||||
|
binfo, err := s.cli.ArchiveInfo(g.ID, b.UUID)
|
||||||
|
if err != nil {
|
||||||
|
if !errors.Is(err, client.ErrNotFound) {
|
||||||
|
return fmt.Errorf("failed to get remote information about the backup file: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if binfo.MD5 != b.MD5 {
|
||||||
|
if err := s.cli.PushBackup(b, g); err != nil {
|
||||||
|
return fmt.Errorf("failed to push backup: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Syncer) pull(g, r repository.Metadata) error {
|
||||||
|
g.Version = r.Version
|
||||||
|
g.Date = r.Date
|
||||||
|
|
||||||
|
if err := s.service.UpdateMetadata(g.ID, g); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.service.PullArchive(g.ID, "", s.cli); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.service.ApplyCurrent(g.ID); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// manage backup
|
||||||
|
bs, err := s.cli.ListArchives(g.ID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, uuid := range bs {
|
||||||
|
rinfo, err := s.cli.ArchiveInfo(g.ID, uuid)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
linfo, err := s.service.Backup(g.ID, uuid)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if linfo.MD5 != rinfo.MD5 {
|
||||||
|
if err := s.service.PullBackup(g.ID, uuid, s.cli); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
40
pkg/tools/iterator/iterator.go
Normal file
40
pkg/tools/iterator/iterator.go
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
package iterator
|
||||||
|
|
||||||
|
type (
|
||||||
|
Iterator[T any] struct {
|
||||||
|
index int
|
||||||
|
values []T
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func New[T any](values []T) *Iterator[T] {
|
||||||
|
return &Iterator[T]{
|
||||||
|
values: values,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (it *Iterator[T]) Next() bool {
|
||||||
|
if len(it.values) == it.index {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
it.index += 1
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (it *Iterator[T]) Value() T {
|
||||||
|
if len(it.values) == 0 {
|
||||||
|
var zero T
|
||||||
|
return zero
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(it.values) == it.index {
|
||||||
|
var zero T
|
||||||
|
return zero
|
||||||
|
}
|
||||||
|
|
||||||
|
return it.values[it.index]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (it *Iterator[T]) IsEmpty() bool {
|
||||||
|
return len(it.values) == 0
|
||||||
|
}
|
||||||
24
web.Dockerfile
Normal file
24
web.Dockerfile
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
FROM golang:1.26.3-trixie AS build
|
||||||
|
|
||||||
|
ENV GOOS=linux
|
||||||
|
ENV CGO_ENABLED=0
|
||||||
|
|
||||||
|
ENV GOAMD64=v3
|
||||||
|
ENV GORISCV64=rva22u64
|
||||||
|
ENV GOARM64=v8.2
|
||||||
|
|
||||||
|
COPY . /src
|
||||||
|
|
||||||
|
RUN cd /src \
|
||||||
|
&& go build -ldflags="-s -w" -o web ./cmd/web \
|
||||||
|
&& chown 0:0 web \
|
||||||
|
&& chmod ugo+x web
|
||||||
|
|
||||||
|
FROM scratch AS prod
|
||||||
|
|
||||||
|
COPY --from=build /etc/passwd /etc/passwd
|
||||||
|
COPY --from=build /etc/shadow /etc/shadow
|
||||||
|
COPY --from=build /src/web /web
|
||||||
|
|
||||||
|
VOLUME [ "/var/lib/cloudsave" ]
|
||||||
|
ENTRYPOINT [ "/web" ]
|
||||||
Reference in New Issue
Block a user