Skip to content

Commit

Permalink
Add new aliasFile option
Browse files Browse the repository at this point in the history
The option allows changing the alias file name.
  • Loading branch information
sergystepanov committed Sep 14, 2024
1 parent fd34d5a commit f1ece58
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 16 deletions.
2 changes: 2 additions & 0 deletions pkg/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 19 additions & 16 deletions pkg/games/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package games

import (
"bufio"
"errors"
"fmt"
"io/fs"
"os"
Expand All @@ -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{}
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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 lib.config.aliasFile == "" {
return nil
}

if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
path := filepath.Join(lib.config.path, lib.config.aliasFile)

if _, err := os.Stat(path); os.IsNotExist(err) {
return nil
}

// read
f, err := os.Open(path)
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() }()

m := make(map[string]string)
s := bufio.NewScanner(f)
for s.Scan() {
id, alias, ok := strings.Cut(s.Text(), "=")
if ok {
m[id] = alias
defer func() { _ = file.Close() }()

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() {
Expand Down
49 changes: 49 additions & 0 deletions pkg/games/library_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package games

import (
"os"
"path/filepath"
"reflect"
"testing"

"github.com/giongto35/cloud-game/v3/pkg/config"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f1ece58

Please sign in to comment.