Skip to content

Commit

Permalink
Allow for variables in the configuration parser filename (#34)
Browse files Browse the repository at this point in the history
* try this first

* fix typo and casing
  • Loading branch information
QuintenQVD0 authored Oct 29, 2024
1 parent 66f2c5f commit 5858856
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions server/config_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,33 @@ package server
import (
"runtime"

"github.com/gammazero/workerpool"
"fmt"
"strings"

"github.com/gammazero/workerpool"
"github.com/pelican-dev/wings/internal/ufs"
)

// Helper function to replace variables in the file path of the configuration parser
func replaceParserConfigPathVariables(filename string, envvars map[string]interface{}) string {
// Check if filename contains at least one '{' and one '}'
// This is here for performance as 99% of the eggs configuration parsers do not have variables in its path
if !strings.Contains(filename, "{") || !strings.Contains(filename, "}") {
return filename
}

// Replace "{{" with "${" and "}}" with "}"
filename = strings.ReplaceAll(filename, "{{", "${")
filename = strings.ReplaceAll(filename, "}}", "}")

// replaces ${varname} with varval
for varname, varval := range envvars {
filename = strings.ReplaceAll(filename, fmt.Sprintf("${%s}", varname), fmt.Sprint(varval))
}

return filename
}

// UpdateConfigurationFiles updates all the defined configuration files for
// a server automatically to ensure that they always use the specified values.
func (s *Server) UpdateConfigurationFiles() {
Expand All @@ -20,7 +42,8 @@ func (s *Server) UpdateConfigurationFiles() {
f := cf

pool.Submit(func() {
file, err := s.Filesystem().UnixFS().Touch(f.FileName, ufs.O_RDWR|ufs.O_CREATE, 0o644)
filename := replaceParserConfigPathVariables(f.FileName, s.Config().EnvVars)
file, err := s.Filesystem().UnixFS().Touch(filename, ufs.O_RDWR|ufs.O_CREATE, 0o644)
if err != nil {
s.Log().WithField("file_name", f.FileName).WithField("error", err).Error("failed to open file for configuration")
return
Expand Down

0 comments on commit 5858856

Please sign in to comment.