fix: allow multiple enviornments

This commit is contained in:
Urko. 2024-05-24 22:07:55 +02:00
parent 3d2cd78b07
commit adf47e4810
2 changed files with 16 additions and 7 deletions

View File

@ -7,13 +7,14 @@ import (
) )
type Config struct { type Config struct {
Secret string `yaml:"secret"` Secret string `yaml:"secret"`
Port int `yaml:"port"` Port int `yaml:"port"`
Projects map[string]map[string]ConfigScript `yaml:"projects"` Projects map[string][]ConfigScript `yaml:"projects"`
} }
type ConfigScript struct { type ConfigScript struct {
BinaryPath string `yaml:"binary"` Environment string `yaml:"environment"`
ScriptPath string `yaml:"script"` BinaryPath string `yaml:"binary"`
ScriptPath string `yaml:"script"`
} }
func LoadConfig(path string) (*Config, error) { func LoadConfig(path string) (*Config, error) {

12
main.go
View File

@ -31,7 +31,7 @@ func main() {
http.ListenAndServe(fmt.Sprintf(":%d", cfg.Port), nil) http.ListenAndServe(fmt.Sprintf(":%d", cfg.Port), nil)
} }
func handlePayload(secret string, projects map[string]map[string]config.ConfigScript) func(w http.ResponseWriter, r *http.Request) { func handlePayload(secret string, projects map[string][]config.ConfigScript) func(w http.ResponseWriter, r *http.Request) {
return (func(w http.ResponseWriter, r *http.Request) { return (func(w http.ResponseWriter, r *http.Request) {
// Read the request body // Read the request body
body, err := io.ReadAll(r.Body) body, err := io.ReadAll(r.Body)
@ -64,8 +64,16 @@ func handlePayload(secret string, projects map[string]map[string]config.ConfigSc
return return
} }
branchName := strings.Split(payload.Ref, "/")[len(strings.Split(payload.Ref, "/"))-1] branchName := strings.Split(payload.Ref, "/")[len(strings.Split(payload.Ref, "/"))-1]
found = false
var scr config.ConfigScript
for i := range proj {
if proj[i].Environment == branchName {
found = true
scr = proj[i]
break
}
scr, found := proj[branchName] }
if !found { if !found {
http.Error(w, "not found", http.StatusNotFound) http.Error(w, "not found", http.StatusNotFound)
return return