From b7555edb6a0537b46b1e762e21dcd0af792ea914 Mon Sep 17 00:00:00 2001 From: Michael Parker Date: Sun, 6 Aug 2023 23:19:28 -0400 Subject: [PATCH] Parse start commands in wings This would replace the need to parse the start commands in the entrypoint so exec could be used instead of eval. As seen in the gist below I have tested what it would do to the ark start command https://gist.githubusercontent.com/parkervcp/db4d2d7b23b48f9aa0eacff79e77d972/raw/377601cd74e4d9c19e58ba0a56086bba438165fe/gistfile1.txt --- server/server.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/server/server.go b/server/server.go index 18c10e2..f640c3e 100644 --- a/server/server.go +++ b/server/server.go @@ -5,6 +5,7 @@ import ( "fmt" "net/http" "os" + "strconv" "strings" "sync" @@ -143,13 +144,30 @@ func (s *Server) Context() context.Context { return s.ctx } +func parseInvocation(invocation string, envvars map[string]interface{}, memory int64, port int, ip string) (parsed string) { + // replace "{{" and "}}" with "${" and "}" respectively + invocation = strings.Replace(invocation, "{{", "${", -1) + invocation = strings.Replace(invocation, "}}", "}", -1) + + // replaces ${varname} with varval + for varname, varval := range envvars { + invocation = strings.Replace(invocation, fmt.Sprintf("${%s}", varname), fmt.Sprint(varval), -1) + } + + invocation = strings.Replace(invocation, "${SERVER_PORT}", strconv.Itoa(port), -1) + invocation = strings.Replace(invocation, "${SERVER_MEMORY}", strconv.Itoa(int(memory)), -1) + invocation = strings.Replace(invocation, "${SERVER_IP}", ip, -1) + + return invocation +} + // Returns all of the environment variables that should be assigned to a running // server instance. func (s *Server) GetEnvironmentVariables() []string { out := []string{ // TODO: allow this to be overridden by the user. fmt.Sprintf("TZ=%s", config.Get().System.Timezone), - fmt.Sprintf("STARTUP=%s", s.Config().Invocation), + fmt.Sprintf("STARTUP=%s", parseInvocation(s.Config().Invocation, s.Config().EnvVars, s.MemoryLimit(), s.Config().Allocations.DefaultMapping.Port, s.Config().Allocations.DefaultMapping.Ip)), fmt.Sprintf("SERVER_MEMORY=%d", s.MemoryLimit()), fmt.Sprintf("SERVER_IP=%s", s.Config().Allocations.DefaultMapping.Ip), fmt.Sprintf("SERVER_PORT=%d", s.Config().Allocations.DefaultMapping.Port),