Skip to content

Commit

Permalink
Seperate config package
Browse files Browse the repository at this point in the history
  • Loading branch information
tetsuya28 committed Jan 1, 2024
1 parent c2f5ada commit f1c1986
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
16 changes: 16 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -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
}
18 changes: 7 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit f1c1986

Please sign in to comment.