mirror of
https://github.com/giongto35/cloud-game.git
synced 2026-01-23 02:34:42 +00:00
Add new aliasFile option
The option allows changing the alias file name.
This commit is contained in:
parent
fd34d5a972
commit
f1ece58c7b
4 changed files with 72 additions and 16 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue