Skip to content

Commit

Permalink
(feat) add support for reading from yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
sn123 committed Dec 7, 2023
1 parent 0fb4c74 commit d3f9091
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ require (
github.com/segmentio/kafka-go v0.3.6
github.com/skip2/go-qrcode v0.0.0-20191027152451-9434209cb086
golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,5 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/qntfy/kazaam.v3 v3.4.9/go.mod h1:Wd/NtL7lla1KLeVKvET8CWojg+DmX0tDUC6J+ut61Z0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
42 changes: 32 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"os"

_ "github.com/joho/godotenv/autoload"
"gopkg.in/yaml.v3"

"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
Expand Down Expand Up @@ -52,18 +53,41 @@ func installGist(gistID string) {
gist.Install()
}

func readConfig() []byte {
jsonFile, err := os.Open("./config/config.json")
if err != nil {
func readConfig() configuration {
// if config.yaml is present we use that first
var fileContent *os.File
var byteContent []byte
var result configuration
isYaml := true
_, err := os.Stat("./config/config.yml")
if os.IsNotExist(err) {
fileContent, err = os.Open("./config/config.json")
isYaml = false
} else {
fileContent, err = os.Open("./config/config.yml")
}
if err != nil && !os.IsNotExist(err) {
log.Println(err)
return []byte("[]")
byteContent = []byte("[]")
json.Unmarshal(byteContent, &result)
return result
}
bytes, err := ioutil.ReadAll(jsonFile)
byteContent, err = ioutil.ReadAll(fileContent)
if err != nil {
log.Println(err)
return []byte("[]")
byteContent = []byte("[]")
json.Unmarshal(byteContent, &result)
return result
}
return bytes
if isYaml {
err = yaml.Unmarshal(byteContent, &result)
if err != nil {
log.Println(err)
}
return result
}
json.Unmarshal(byteContent, &result)
return result
}

func main() {
Expand All @@ -86,9 +110,7 @@ func main() {
eventListener := events.EventListener{URL: os.Getenv("KAFKA_URL"), Topic: os.Getenv("KAFKA_TOPIC"), GroupID: "mustard"}
eventListener.Start()
// read config to get widgets
config := readConfig()
var result configuration
json.Unmarshal([]byte(config), &result)
result := readConfig()
mustardcore.GetFactory().Process(result.Jobs)

defer eventsManager.Destroy()
Expand Down

0 comments on commit d3f9091

Please sign in to comment.