70 lines
1.8 KiB
Go
70 lines
1.8 KiB
Go
|
|
package cv
|
||
|
|
|
||
|
|
import (
|
||
|
|
"encoding/json"
|
||
|
|
"fmt"
|
||
|
|
"os"
|
||
|
|
"strings"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
// LoadCV loads CV data from a JSON file for the specified language
|
||
|
|
func LoadCV(lang string) (*CV, error) {
|
||
|
|
if lang != "en" && lang != "es" {
|
||
|
|
return nil, fmt.Errorf("unsupported language: %s", lang)
|
||
|
|
}
|
||
|
|
|
||
|
|
filename := fmt.Sprintf("data/cv-%s.json", lang)
|
||
|
|
filepath, err := findDataFile(filename)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
data, err := os.ReadFile(filepath)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("error reading file %s: %w", filename, err)
|
||
|
|
}
|
||
|
|
|
||
|
|
var cvData CV
|
||
|
|
if err := json.Unmarshal(data, &cvData); err != nil {
|
||
|
|
return nil, fmt.Errorf("error parsing JSON: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Replace {{YEAR}} placeholder in reference URLs with current year
|
||
|
|
currentYear := fmt.Sprintf("%d", time.Now().Year())
|
||
|
|
for i := range cvData.References {
|
||
|
|
cvData.References[i].URL = replaceYearPlaceholder(cvData.References[i].URL, currentYear)
|
||
|
|
}
|
||
|
|
|
||
|
|
return &cvData, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// findDataFile locates a data file by searching up the directory tree
|
||
|
|
func findDataFile(filename string) (string, error) {
|
||
|
|
// Try current directory first
|
||
|
|
if _, err := os.Stat(filename); err == nil {
|
||
|
|
return filename, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// Try parent directories (for tests running from subdirectories)
|
||
|
|
paths := []string{
|
||
|
|
filename, // Current dir
|
||
|
|
"../" + filename, // One level up
|
||
|
|
"../../" + filename, // Two levels up (for tests in internal/handlers)
|
||
|
|
"../../../" + filename, // Three levels up
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, path := range paths {
|
||
|
|
if _, err := os.Stat(path); err == nil {
|
||
|
|
return path, nil
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return "", fmt.Errorf("file not found: %s (searched: current dir, ../, ../../, ../../../)", filename)
|
||
|
|
}
|
||
|
|
|
||
|
|
// replaceYearPlaceholder replaces {{YEAR}} with the current year
|
||
|
|
func replaceYearPlaceholder(url string, year string) string {
|
||
|
|
return strings.ReplaceAll(url, "{{YEAR}}", year)
|
||
|
|
}
|