From f1c1986aecd50fa24779933db1579f99b9a560dd Mon Sep 17 00:00:00 2001 From: tetsuya28 Date: Mon, 1 Jan 2024 21:15:43 +0900 Subject: [PATCH] Seperate config package --- config/config.go | 16 ++++++++++++++++ main.go | 18 +++++++----------- 2 files changed, 23 insertions(+), 11 deletions(-) create mode 100644 config/config.go diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..86c9183 --- /dev/null +++ b/config/config.go @@ -0,0 +1,16 @@ +package config + +import "github.com/kelseyhightower/envconfig" + +type Config struct { + SlackToken string `required:"true" envconfig:"SLACK_TOKEN"` + SlackChannel string `required:"true" envconfig:"SLACK_CHANNEL"` +} + +func New() (*Config, error) { + config := Config{} + if err := envconfig.Process("", &config); err != nil { + return nil, err + } + return &Config{}, nil +} diff --git a/main.go b/main.go index bfa6d49..0c0ce89 100644 --- a/main.go +++ b/main.go @@ -9,17 +9,12 @@ import ( "github.com/aws/aws-lambda-go/lambda" "github.com/aws/aws-sdk-go/service/costexplorer" - "github.com/kelseyhightower/envconfig" "github.com/slack-go/slack" + "github.com/tetsuya28/aws-cost-report/config" "github.com/tetsuya28/aws-cost-report/external" "github.com/ucpr/mongo-streamer/pkg/log" ) -type Config struct { - SlackToken string `required:"true" envconfig:"SLACK_TOKEN"` - SlackChannel string `required:"true" envconfig:"SLACK_CHANNEL"` -} - type DailyCost struct { Total float64 Services map[string]ServiceDetail @@ -37,11 +32,12 @@ func main() { } func handler() error { - config := Config{} - if err := envconfig.Process("", &config); err != nil { - panic(err) + cfg, err := config.New() + if err != nil { + return err } - slk := external.NewSlack(config.SlackToken) + + slk := external.NewSlack(cfg.SlackToken) result, err := external.GetCost() if err != nil { @@ -92,7 +88,7 @@ func handler() error { option := slack.MsgOptionText(text, false) attachments := toAttachment(cost) - err = slk.PostMessage(config.SlackChannel, option, slack.MsgOptionAttachments(attachments...)) + err = slk.PostMessage(cfg.SlackChannel, option, slack.MsgOptionAttachments(attachments...)) if err != nil { return err }