diff --git a/config.yml b/config.yml new file mode 100644 index 0000000..3757499 --- /dev/null +++ b/config.yml @@ -0,0 +1,13 @@ +listenaddress: 127.0.0.1:8654 +services: + test: + script: + program: "echo" + arguments: + - test + tests: + - program: ./example.sh + appendpayload: true + disablesignatureverification: false + signatureheader: test + secret: thisisasecret diff --git a/config/config.go b/config/config.go index 8342ada..77007ae 100644 --- a/config/config.go +++ b/config/config.go @@ -1,5 +1,12 @@ package config +import ( + "encoding/json" + "io/ioutil" + + "gopkg.in/yaml.v3" +) + // The struct that represents the config.json file type Config struct { ListenAddress string @@ -33,3 +40,23 @@ func (c Config) Validate() error { return nil } + +func (c *Config) Load(config_filename string) error { + + raw_config, err := ioutil.ReadFile(config_filename) + if err != nil { + return err + } + + err = json.Unmarshal(raw_config, &c) + if err == nil { + return c.Validate() + } + + err = yaml.Unmarshal(raw_config, &c) + if err == nil { + return c.Validate() + } + + return err +} diff --git a/example.sh b/example.sh old mode 100755 new mode 100644 diff --git a/go.mod b/go.mod index 8607e46..76afd0f 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module git.alv.cx/alvierahman90/gohookr go 1.16 -require github.com/gorilla/mux v1.8.0 +require ( + github.com/gorilla/mux v1.8.0 + gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b +) diff --git a/go.sum b/go.sum index 5350288..cc0e6db 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,6 @@ github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index bdca2c0..45f0d7b 100644 --- a/main.go +++ b/main.go @@ -4,7 +4,6 @@ import ( "crypto/hmac" "crypto/sha256" "encoding/hex" - "encoding/json" "fmt" "io" "io/ioutil" @@ -33,7 +32,7 @@ func main() { checkSignature = p != "true" } - c, err := loadConfig(config_filename) + var err = c.Load(config_filename) if err != nil { panic(err.Error()) } @@ -49,28 +48,9 @@ func main() { log.Fatal(http.ListenAndServe(c.ListenAddress, r)) } -func loadConfig(config_filename string) (config.Config, error) { - var c config.Config - - raw_config, err := ioutil.ReadFile(config_filename) - if err != nil { - return config.Config{}, err - } - - if err := json.Unmarshal(raw_config, &c); err != nil { - return config.Config{}, err - } - - if err := c.Validate(); err != nil { - return config.Config{}, err - } - - return c, nil - -} - func webhookHandler(w http.ResponseWriter, r *http.Request) { - c, err := loadConfig(config_filename) + var c config.Config + var err = c.Load(config_filename) if err != nil { writeResponse(w, 500, "Unable to read config file") }