package services import ( "bytes" "embed" "fmt" "strings" "text/template" "github.com/Fimeg/RedFlag/aggregator-server/internal/models" ) //go:embed templates/install/scripts/*.tmpl var installScriptTemplates embed.FS // InstallTemplateService renders installation scripts from templates type InstallTemplateService struct{} // NewInstallTemplateService creates a new template service func NewInstallTemplateService() *InstallTemplateService { return &InstallTemplateService{} } // RenderInstallScript renders an installation script for the specified platform func (s *InstallTemplateService) RenderInstallScript(agent *models.Agent, binaryURL, configURL string) (string, error) { // Define template data data := struct { AgentID string BinaryURL string ConfigURL string Platform string Version string }{ AgentID: agent.ID.String(), BinaryURL: binaryURL, ConfigURL: configURL, Platform: agent.OSType, Version: agent.CurrentVersion, } // Choose template based on platform var templateName string if strings.Contains(agent.OSType, "windows") { templateName = "templates/install/scripts/windows.ps1.tmpl" } else { templateName = "templates/install/scripts/linux.sh.tmpl" } // Load and parse template tmpl, err := template.ParseFS(installScriptTemplates, templateName) if err != nil { return "", fmt.Errorf("failed to load template: %w", err) } // Render template var buf bytes.Buffer if err := tmpl.Execute(&buf, data); err != nil { return "", fmt.Errorf("failed to render template: %w", err) } return buf.String(), nil } // RenderInstallScriptFromBuild renders script using build response func (s *InstallTemplateService) RenderInstallScriptFromBuild( agentID string, platform string, version string, binaryURL string, configURL string, ) (string, error) { data := struct { AgentID string BinaryURL string ConfigURL string Platform string Version string }{ AgentID: agentID, BinaryURL: binaryURL, ConfigURL: configURL, Platform: platform, Version: version, } templateName := "templates/install/scripts/linux.sh.tmpl" if strings.Contains(platform, "windows") { templateName = "templates/install/scripts/windows.ps1.tmpl" } tmpl, err := template.ParseFS(installScriptTemplates, templateName) if err != nil { return "", err } var buf bytes.Buffer if err := tmpl.Execute(&buf, data); err != nil { return "", err } return buf.String(), nil }