Move go-sql-queryrepo package into go-kit/sqr
All checks were successful
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 34s

Signed-off-by: Jan Tytgat <jan.tytgat@corelayer.eu>
This commit is contained in:
Jan Tytgat
2025-05-21 21:04:53 +02:00
parent 1aca178eb3
commit 37f50324fd
7 changed files with 498 additions and 1 deletions

30
sqr/loader.go Normal file
View File

@ -0,0 +1,30 @@
package queryrepo
import (
"embed"
"fmt"
"io/fs"
"path"
"path/filepath"
)
// LoadQueryFromFs retrieves a query from a filesystem.
// It needs the root path to start the search from, as well as a collection name and a query name.
// The collection name equals to a direct directory name in the root path.
// The query name is the file name (without extension) to load the contents from.
// It returns and empty string and an error if the file cannot be found.
func LoadQueryFromFs(f fs.FS, rootPath, collectionName, queryName string) (string, error) {
var err error
var contents []byte
switch f.(type) {
case embed.FS:
if contents, err = fs.ReadFile(f, path.Join(rootPath, collectionName, queryName)+".sql"); err != nil {
return "", fmt.Errorf("failed to read file %s: %w", path.Join(rootPath, collectionName, queryName)+".sql", err)
}
default:
if contents, err = fs.ReadFile(f, filepath.Join(rootPath, collectionName, queryName)+".sql"); err != nil {
return "", fmt.Errorf("failed to read file %s: %w", filepath.Join(rootPath, collectionName, queryName)+".sql", err)
}
}
return string(contents), nil
}