Add new aliasFile option

The option allows changing the alias file name.
This commit is contained in:
Sergey Stepanov 2024-09-14 20:37:43 +03:00
parent fd34d5a972
commit f1ece58c7b
No known key found for this signature in database
GPG key ID: A56B4929BAA8556B
4 changed files with 72 additions and 16 deletions

View file

@ -29,6 +29,8 @@ coordinator:
selector:
# games library
library:
# optional alias file for overriding game names from the basePath path
aliasFile: alias.txt
# root folder for the library (where games are stored)
basePath: assets/games
# an explicit list of supported file extensions

View file

@ -5,6 +5,8 @@ import "flag"
type Version int
type Library struct {
// filename of the alias' file
AliasFile string
// some directory which is going to be
// the root folder for the library
BasePath string

View file

@ -2,7 +2,6 @@ package games
import (
"bufio"
"errors"
"fmt"
"io/fs"
"os"
@ -19,6 +18,7 @@ import (
// libConf is an optimized internal library configuration
type libConf struct {
aliasFile string
path string
supported map[string]struct{}
ignored map[string]struct{}
@ -89,6 +89,7 @@ func NewLib(conf config.Library, emu WithEmulatorInfo, log *logger.Logger) GameL
library := &library{
config: libConf{
aliasFile: conf.AliasFile,
path: dir,
supported: toMap(conf.Supported),
ignored: toMap(conf.Ignored),
@ -128,34 +129,36 @@ func (lib *library) FindGameByName(name string) GameMetadata {
}
func (lib *library) AliasFileMaybe() map[string]string {
dir := lib.config.path
path := dir + "/alias.txt"
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
if lib.config.aliasFile == "" {
return nil
}
// read
f, err := os.Open(path)
path := filepath.Join(lib.config.path, lib.config.aliasFile)
if _, err := os.Stat(path); os.IsNotExist(err) {
return nil
}
file, err := os.Open(path)
if err != nil {
lib.log.Error().Msgf("couldn't open alias file, %v", err)
return nil
}
defer func() { _ = f.Close() }()
defer func() { _ = file.Close() }()
m := make(map[string]string)
s := bufio.NewScanner(f)
for s.Scan() {
id, alias, ok := strings.Cut(s.Text(), "=")
if ok {
m[id] = alias
aliases := make(map[string]string)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if id, alias, found := strings.Cut(scanner.Text(), "="); found {
aliases[id] = alias
}
}
if err = s.Err(); err != nil {
if err := scanner.Err(); err != nil {
lib.log.Error().Msgf("alias file read error, %v", err)
}
return m
return aliases
}
func (lib *library) Scan() {

View file

@ -1,6 +1,9 @@
package games
import (
"os"
"path/filepath"
"reflect"
"testing"
"github.com/giongto35/cloud-game/v3/pkg/config"
@ -60,6 +63,52 @@ func TestLibraryScan(t *testing.T) {
}
}
func TestAliasFileMaybe(t *testing.T) {
lib := &library{
config: libConf{
aliasFile: "alias",
path: os.TempDir(),
},
log: logger.NewConsole(false, "w", false),
}
contents := "a=b\nc=d\n"
path := filepath.Join(lib.config.path, lib.config.aliasFile)
if err := os.WriteFile(path, []byte(contents), 0644); err != nil {
t.Error(err)
}
defer func() {
if err := os.RemoveAll(path); err != nil {
t.Error(err)
}
}()
want := map[string]string{}
want["a"] = "b"
want["c"] = "d"
aliases := lib.AliasFileMaybe()
if !reflect.DeepEqual(aliases, want) {
t.Errorf("AliasFileMaybe() = %v, want %v", aliases, want)
}
}
func TestAliasFileMaybeNot(t *testing.T) {
lib := &library{
config: libConf{
path: os.TempDir(),
},
log: logger.NewConsole(false, "w", false),
}
aliases := lib.AliasFileMaybe()
if aliases != nil {
t.Errorf("should be nil, but %v", aliases)
}
}
func Benchmark(b *testing.B) {
log := logger.Default()
logger.SetGlobalLevel(logger.Disabled)