mirror of
https://github.com/alvierahman90/gohookr.git
synced 2025-10-13 07:24:22 +00:00
Move config to separate package
This commit is contained in:
19
config/command.go
Normal file
19
config/command.go
Normal 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
39
config/config.go
Normal 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
12
config/errors.go
Normal 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)
|
||||
}
|
Reference in New Issue
Block a user