Move config to separate package

This commit is contained in:
2021-08-04 12:04:01 +01:00
parent 2189ee511c
commit 8677f5bfdd
4 changed files with 77 additions and 68 deletions

19
config/command.go Normal file
View File

@@ -0,0 +1,19 @@
package config
import "os/exec"
type Command struct {
Program string
Arguments []string
AppendPayload bool
}
func (c Command) Execute(payload string) ([]byte, error) {
arguments := make([]string, 0)
copy(c.Arguments, arguments)
if c.AppendPayload {
arguments = append(arguments, payload)
}
return exec.Command(c.Program, arguments...).Output()
}

39
config/config.go Normal file
View File

@@ -0,0 +1,39 @@
package config
import (
"encoding/json"
"fmt"
)
type Config struct {
ListenAddress string
Services map[string]struct {
Script Command
Secret string
SignatureHeader string
Tests []Command
}
}
func (c Config) Validate() error {
if c.ListenAddress == "" {
return requiredFieldError{"ListenAddress", ""}
}
jsonbytes, _ := json.MarshalIndent(c, "", " ")
fmt.Println(string(jsonbytes))
for serviceName, service := range c.Services {
if service.Script.Program == "" {
return requiredFieldError{"Script.Program", serviceName}
}
if service.SignatureHeader == "" {
return requiredFieldError{"SignatureHeader", serviceName}
}
if service.Secret == "" {
return requiredFieldError{"Secret", serviceName}
}
}
return nil
}

12
config/errors.go Normal file
View File

@@ -0,0 +1,12 @@
package config
import "fmt"
type requiredFieldError struct {
fieldName string
serviceName string
}
func (e requiredFieldError) Error() string {
return fmt.Sprintf("%v cannot be empty (%v)", e.fieldName, e.serviceName)
}