starting 0.0.2 dev

This commit is contained in:
2025-07-30 00:49:22 +02:00
parent c099d3e64f
commit c6edb91f29
16 changed files with 287 additions and 114 deletions

View File

@@ -1,5 +1,5 @@
package constants
const Version = "0.0.1"
const Version = "0.0.2"
const ApiVersion = 1

View File

@@ -2,8 +2,8 @@ package client
import (
"bytes"
"cloudsave/pkg/game"
"cloudsave/pkg/remote/obj"
"cloudsave/pkg/repository"
customtime "cloudsave/pkg/tools/time"
"encoding/json"
"errors"
@@ -117,19 +117,19 @@ func (c *Client) Hash(gameID string) (string, error) {
return "", errors.New("invalid payload sent by the server")
}
func (c *Client) Metadata(gameID string) (game.Metadata, error) {
func (c *Client) Metadata(gameID string) (repository.Metadata, error) {
u, err := url.JoinPath(c.baseURL, "api", "v1", "games", gameID, "metadata")
if err != nil {
return game.Metadata{}, err
return repository.Metadata{}, err
}
o, err := c.get(u)
if err != nil {
return game.Metadata{}, err
return repository.Metadata{}, err
}
if m, ok := (o.Data).(map[string]any); ok {
gm := game.Metadata{
gm := repository.Metadata{
ID: m["id"].(string),
Name: m["name"].(string),
Version: int(m["version"].(float64)),
@@ -138,10 +138,10 @@ func (c *Client) Metadata(gameID string) (game.Metadata, error) {
return gm, nil
}
return game.Metadata{}, errors.New("invalid payload sent by the server")
return repository.Metadata{}, errors.New("invalid payload sent by the server")
}
func (c *Client) Push(gameID, archivePath string, m game.Metadata) error {
func (c *Client) Push(gameID, archivePath string, m repository.Metadata) error {
u, err := url.JoinPath(c.baseURL, "api", "v1", "games", gameID, "data")
if err != nil {
return err
@@ -271,7 +271,7 @@ func (c *Client) Ping() error {
return nil
}
func (c *Client) All() ([]game.Metadata, error) {
func (c *Client) All() ([]repository.Metadata, error) {
u, err := url.JoinPath(c.baseURL, "api", "v1", "games")
if err != nil {
return nil, err
@@ -283,10 +283,10 @@ func (c *Client) All() ([]game.Metadata, error) {
}
if games, ok := (o.Data).([]any); ok {
var res []game.Metadata
var res []repository.Metadata
for _, g := range games {
if v, ok := g.(map[string]any); ok {
gm := game.Metadata{
gm := repository.Metadata{
ID: v["id"].(string),
Name: v["name"].(string),
Version: int(v["version"].(float64)),

View File

@@ -1,4 +1,4 @@
package game
package repository
import (
"cloudsave/pkg/tools/id"
@@ -7,6 +7,7 @@ import (
"encoding/json"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"time"
@@ -135,6 +136,65 @@ func One(gameID string) (Metadata, error) {
return m, nil
}
func Archive(gameID string) error {
path := filepath.Join(datastorepath, gameID, "data.tar.gz")
// open old
f, err := os.OpenFile(path, os.O_RDONLY, 0)
if err != nil {
return fmt.Errorf("failed to open old file: %w", err)
}
defer f.Close()
histDirPath := filepath.Join(datastorepath, gameID, "hist")
if err := os.MkdirAll(histDirPath, 0740); err != nil {
return fmt.Errorf("failed to make 'hist' directory")
}
d, err := os.ReadDir(histDirPath)
if err != nil {
return fmt.Errorf("failed to open 'hist' directory")
}
// keep the dir under 6 files
if len(d) > 5 {
var oldest *fs.FileInfo
for _, hfile := range d {
finfo, err := hfile.Info()
if err != nil {
return fmt.Errorf("failed to read backup file: %w", err)
}
if oldest == nil {
oldest = &finfo
continue
}
if finfo.ModTime().Before((*oldest).ModTime()) {
oldest = &finfo
}
}
if err := os.Remove((*oldest).Name()); err != nil {
return fmt.Errorf("failed to remove the oldest backup file: %w", err)
}
}
// open new
nf, err := os.OpenFile(filepath.Join(datastorepath, gameID, "hist", time.Now().Format("2006-01-02T15-04-05Z07-00")+".data.tar.gz"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0740)
if err != nil {
return fmt.Errorf("failed to open new file: %w", err)
}
defer nf.Close()
// copy
if _, err := io.Copy(nf, f); err != nil {
return fmt.Errorf("failed to copy data: %w", err)
}
return nil
}
func DatastorePath() string {
return datastorepath
}

View File

@@ -3,6 +3,7 @@ package archive
import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"os"
"path/filepath"
@@ -71,3 +72,49 @@ func Untar(file io.Reader, path string) error {
}
}
}
func Tar(file io.Writer, path string) error {
gw := gzip.NewWriter(file)
defer gw.Close()
tw := tar.NewWriter(gw)
defer tw.Close()
// Walk again to add files
err := filepath.Walk(path, func(path string, info os.FileInfo, walkErr error) error {
if walkErr != nil {
return walkErr
}
// Create tar header
header, err := tar.FileInfoHeader(info, path)
if err != nil {
return err
}
// Preserve directory structure relative to srcDir
relPath, err := filepath.Rel(filepath.Dir(path), path)
if err != nil {
return err
}
header.Name = relPath
if err := tw.WriteHeader(header); err != nil {
return err
}
if info.Mode().IsRegular() {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
if _, err := io.Copy(tw, file); err != nil {
return err
}
}
return nil
})
if err != nil {
return fmt.Errorf("writing tar entries: %w", err)
}
return nil
}

View File

@@ -1,26 +0,0 @@
package credentials
import (
"bufio"
"fmt"
"os"
"strings"
"golang.org/x/term"
)
func Read() (string, string, error) {
fmt.Print("Enter username: ")
reader := bufio.NewReader(os.Stdin)
username, _ := reader.ReadString('\n')
username = strings.TrimSpace(username)
fmt.Printf("password for %s: ", username)
password, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return "", "", err
}
fmt.Println()
return username, string(password), nil
}