Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

Commit

Permalink
Add env var yaml parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
adrain-cb committed Aug 23, 2023
1 parent f106488 commit a523120
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 7 deletions.
2 changes: 1 addition & 1 deletion alerts-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ alertRoutes:
channel: ""
pagerduty:
high_oncall:
integration_key: ""
integration_key: ${MY_INTEGRATION_KEY}
medium_oncall:
integration_key: ""
6 changes: 3 additions & 3 deletions internal/alert/routing_directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ func (rd *routingDirectory) paramsToRouteDirectory(acc *core.AlertClientCfg, sev
if acc.Slack != nil {
for name, cfg := range acc.Slack {
conf := &client.SlackConfig{
URL: cfg.URL,
Channel: cfg.Channel,
URL: cfg.URL.String(),
Channel: cfg.Channel.String(),
}
client := client.NewSlackClient(conf, name)
rd.slackClients[sev] = append(rd.slackClients[sev], client)
Expand All @@ -83,7 +83,7 @@ func (rd *routingDirectory) paramsToRouteDirectory(acc *core.AlertClientCfg, sev
if acc.PagerDuty != nil {
for name, cfg := range acc.PagerDuty {
conf := &client.PagerDutyConfig{
IntegrationKey: cfg.IntegrationKey,
IntegrationKey: cfg.IntegrationKey.String(),
AlertEventsURL: rd.cfg.PagerdutyAlertEventsURL,
}
client := client.NewPagerDutyClient(conf, name)
Expand Down
6 changes: 3 additions & 3 deletions internal/core/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ type AlertClientCfg struct {

// Config ... The config for an alert client
type Config struct {
URL string `yaml:"url"`
Channel string `yaml:"channel"`
IntegrationKey string `yaml:"integration_key"`
URL StringFromEnv `yaml:"url"`
Channel StringFromEnv `yaml:"channel"`
IntegrationKey StringFromEnv `yaml:"integration_key"`
}
28 changes: 28 additions & 0 deletions internal/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ package core
import (
"encoding/json"
"fmt"
"os"
"regexp"
"time"

"gopkg.in/yaml.v3"

"github.com/ethereum/go-ethereum/common"
)

Expand Down Expand Up @@ -193,3 +197,27 @@ const (
L2ToL1MessagePasser = "l2_to_l1_address" //#nosec G101: False positive, this isn't a credential
L2OutputOracle = "l2_output_address" //#nosec G101: False positive, this isn't a credential
)

// Regexp for parsing yaml files
var reVar = regexp.MustCompile(`^\${(\w+)}$`)

type StringFromEnv string

// UnmarshalYAML implements the yaml.Unmarshaler interface to allow parsing strings from env vars.
func (e *StringFromEnv) UnmarshalYAML(value *yaml.Node) error {
var s string
if err := value.Decode(&s); err != nil {
return err
}
if match := reVar.FindStringSubmatch(s); len(match) > 0 {
*e = StringFromEnv(os.Getenv(match[1]))
} else {
*e = StringFromEnv(s)
}
return nil
}

// String returns the string value, implementing the flag.Value interface.
func (e *StringFromEnv) String() string {
return string(*e)
}
23 changes: 23 additions & 0 deletions internal/core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package core_test
import (
"testing"

"gopkg.in/yaml.v3"

"github.com/base-org/pessimism/internal/core"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -60,3 +62,24 @@ func Test_SessionParams(t *testing.T) {
assert.Equal(t, nestedArgs, []interface{}{"bland(1,2,3)"}, "NestedArgs should return the correct value")

}

func Test_UnmarshalYaml(t *testing.T) {

type test struct {
TestKey core.StringFromEnv `yaml:"test_key"`
TestKey2 core.StringFromEnv `yaml:"test_key2"`
}

t.Setenv("test_key", "test_value")

yml := []byte(`
test_key: ${test_key}
test_key2: "test_value2"
`)

t1 := &test{}
err := yaml.Unmarshal(yml, t1)
assert.Nil(t, err, "Unmarshal should not return an error")
assert.Equal(t, "test_value", t1.TestKey.String(), "Unmarshal should return the correct value")
assert.Equal(t, "test_value2", t1.TestKey2.String(), "Unmarshal should return the correct value")
}

0 comments on commit a523120

Please sign in to comment.