Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: tetsuya28/aws-cost-report
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.1.0
Choose a base ref
...
head repository: tetsuya28/aws-cost-report
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref

Commits on Dec 31, 2023

  1. terraform fmt

    tetsuya28 committed Dec 31, 2023
    Copy the full SHA
    6654221 View commit details
  2. Merge pull request #5 from tetsuya28/fix-release-terraform

    Fix release terraform
    tetsuya28 authored Dec 31, 2023
    Copy the full SHA
    602197c View commit details
  3. Fix Terraform module

    tetsuya28 committed Dec 31, 2023
    Copy the full SHA
    9a110d4 View commit details
  4. Merge pull request #6 from tetsuya28/fix-terraform-module-1

    Fix Terraform module
    tetsuya28 authored Dec 31, 2023
    Copy the full SHA
    fd4abe5 View commit details
  5. Fix Terraform module

    tetsuya28 committed Dec 31, 2023
    Copy the full SHA
    9e191b9 View commit details
  6. Merge pull request #8 from tetsuya28/fix-terraform-module-2

    Fix Terraform module
    tetsuya28 authored Dec 31, 2023
    Copy the full SHA
    fc1f4b8 View commit details
  7. Add AWS services

    tetsuya28 committed Dec 31, 2023
    Copy the full SHA
    e16fe63 View commit details
  8. Merge pull request #9 from tetsuya28/add-aws-services

    Add AWS services
    tetsuya28 authored Dec 31, 2023
    Copy the full SHA
    794d3f0 View commit details
  9. Specify required provider

    tetsuya28 committed Dec 31, 2023
    Copy the full SHA
    290d5ea View commit details
  10. Merge pull request #10 from tetsuya28/update-terraform-provider

    Specify required provider
    tetsuya28 authored Dec 31, 2023
    Copy the full SHA
    62262a1 View commit details
  11. Add GitHub provider owner

    tetsuya28 committed Dec 31, 2023
    Copy the full SHA
    946d3cb View commit details
  12. Merge pull request #11 from tetsuya28/fix-github-provider

    Add GitHub provider owner
    tetsuya28 authored Dec 31, 2023
    Copy the full SHA
    a39ec03 View commit details
  13. Copy the full SHA
    5df04df View commit details
  14. Merge pull request #13 from tetsuya28/fix-terraform-release-download

    Fix Terraform module, download release
    tetsuya28 authored Dec 31, 2023
    Copy the full SHA
    75a444a View commit details

Commits on Jan 1, 2024

  1. Add AWS account full name

    tetsuya28 committed Jan 1, 2024
    Copy the full SHA
    a619dfd View commit details
  2. Merge pull request #14 from tetsuya28/add-aws-account-full-name

    Add AWS account full name
    tetsuya28 authored Jan 1, 2024
    Copy the full SHA
    0eca932 View commit details
  3. Fix Lambda policy

    tetsuya28 committed Jan 1, 2024
    Copy the full SHA
    77b3f5b View commit details
  4. Fix indent

    tetsuya28 committed Jan 1, 2024
    Copy the full SHA
    d1767f0 View commit details
  5. Merge pull request #15 from tetsuya28/fix-lambda-policy

    Fix Lambda policy
    tetsuya28 authored Jan 1, 2024
    Copy the full SHA
    c2f5ada View commit details
  6. Seperate config package

    tetsuya28 committed Jan 1, 2024
    Copy the full SHA
    f1c1986 View commit details
  7. Add i18y

    tetsuya28 committed Jan 1, 2024
    Copy the full SHA
    deb29a6 View commit details
  8. Merge pull request #17 from tetsuya28/i18y

    i18y
    tetsuya28 authored Jan 1, 2024
    Copy the full SHA
    4c6fec0 View commit details

Commits on Jan 3, 2024

  1. Fix collect data days

    tetsuya28 committed Jan 3, 2024
    Copy the full SHA
    52514c5 View commit details
  2. Merge pull request #18 from tetsuya28/fix-collecto-days

    Fix collect data days
    tetsuya28 authored Jan 3, 2024
    Copy the full SHA
    43e5bf4 View commit details

Commits on Jun 2, 2024

  1. fix lambda runtime

    tetsuya28 committed Jun 2, 2024
    Copy the full SHA
    90d3e8b View commit details
  2. Merge pull request #19 from tetsuya28/fix-lambda-runtime

    fix lambda runtime
    tetsuya28 authored Jun 2, 2024
    Copy the full SHA
    c7f3d19 View commit details

Commits on Jun 9, 2024

  1. update terraform module

    tetsuya28 committed Jun 9, 2024
    Copy the full SHA
    6a28249 View commit details
  2. Merge pull request #21 from tetsuya28/update-terraform-module

    update terraform module
    tetsuya28 authored Jun 9, 2024
    Copy the full SHA
    36fe803 View commit details
Showing with 345 additions and 102 deletions.
  1. +6 −1 Makefile
  2. +29 −2 README.md
  3. +17 −0 config/config.go
  4. BIN docs/notification.jpg
  5. +48 −3 external/aws.go
  6. +16 −2 go.mod
  7. +28 −0 go.sum
  8. +72 −0 i18y/i18y.go
  9. +38 −0 i18y/i18y_test.go
  10. +3 −0 i18y/languages/en.yaml
  11. +3 −0 i18y/languages/ja.yaml
  12. +54 −32 main.go
  13. +3 −1 module/README.md
  14. +14 −27 module/main.tf
  15. +14 −0 module/provider.tf
  16. +0 −34 terraform/main.tf
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -12,11 +12,16 @@ test:

.PHONY: integration-test
integration-test:
go clean -testcache
SLACK_TOKEN=$(SLACK_TOKEN) SLACK_CHANNEL=$(SLACK_CHANNEL) go test -v ./... -tags=integration

.PHONY: build
build:
GOOS=linux GOARCH=amd64 go build -o ./bin/bootstrap main.go

.PHONY: upload
upload: build
zip -j ./bin/main.zip ./bin/main
zip -j ./bin/main.zip ./bin/bootstrap
aws s3 cp ./bin/main.zip s3://tetsuya28-aws-cost-report/$(TAG)/main.zip

.PHONY: terraform/docs
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,46 @@
## What is aws_cost_usage
Notify daily cost usage of an AWS account to slack channel.

![](./docs/notification.jpg)

## How to use this
You can deploy with Terraform resources on your AWS account.
### Execution
#### Lambda
You can deploy with Terraform resources on your AWS account with AWS Lambda.

```hcl
terraform {
required_version = "1.8.1"
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.31.0"
}
github = {
source = "integrations/github"
version = "5.42.0"
}
}
}
module "cost" {
source = "github.com/tetsuya28/aws_cost_report.git//module"
name = "aws-cost-report" # AWS resource name to deploy Lambda, IAM, etc
slack_channel = "#random" # Slack channel to notify
slack_token = "xoxb-xxx" # Slack token
build_version = "v0.1.0" # default : latest
build_version = "v0.2.0" # default : latest
}
```

#### Others
You can download binary from GitHub Release.

### Support multi languages
- English
- Set `LANGUAGE=en`
- Japanese
- Set `LANGUAGE=ja`

## Development
- Init
```
17 changes: 17 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package config

import "github.com/kelseyhightower/envconfig"

type Config struct {
SlackToken string `required:"true" envconfig:"SLACK_TOKEN"`
SlackChannel string `required:"true" envconfig:"SLACK_CHANNEL"`
Language string `required:"true" envconfig:"LANGUAGE" default:"ja"`
}

func New() (Config, error) {
config := Config{}
if err := envconfig.Process("", &config); err != nil {
return Config{}, err
}
return config, nil
}
Binary file added docs/notification.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 48 additions & 3 deletions external/aws.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package external

import (
"context"
"time"

"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/account"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/costexplorer"
@@ -24,7 +27,7 @@ func GetIconURL(service string) string {
return "https://github.com/awslabs/aws-icons-for-plantuml/blob/main/dist/BusinessApplications/SimpleEmailService.png?raw=true"
case "Amazon DynamoDB":
return "https://github.com/awslabs/aws-icons-for-plantuml/blob/main/dist/Database/DynamoDB.png?raw=true"
case "Amazon EC2 Container Registry (ECR)":
case "Amazon EC2 Container Registry (ECR)", "Amazon Elastic Container Registry Public":
return "https://github.com/awslabs/aws-icons-for-plantuml/blob/main/dist/Containers/ElasticContainerRegistry.png?raw=true"
case "Amazon Elastic Container Service":
return "https://github.com/awslabs/aws-icons-for-plantuml/blob/main/dist/Containers/ElasticContainerService.png?raw=true"
@@ -41,8 +44,34 @@ func GetIconURL(service string) string {
case "Amazon CloudFront":
return "https://github.com/awslabs/aws-icons-for-plantuml/blob/main/dist/NetworkingContentDelivery/CloudFront.png?raw=true"
case "AWS Amplify":
return ""
return "https://github.com/awslabs/aws-icons-for-plantuml/blob/main/dist/FrontEndWebMobile/AmplifyAWSAmplifyStudio.png?raw=true"
case "AWS Glue":
return "https://github.com/awslabs/aws-icons-for-plantuml/blob/main/dist/Analytics/Glue.png?raw=true"
case "Amazon Simple Notification Service":
return "https://github.com/awslabs/aws-icons-for-plantuml/blob/main/dist/ApplicationIntegration/SimpleNotificationService.png?raw=true"
case "AWS Secrets Manager":
return "https://github.com/awslabs/aws-icons-for-plantuml/blob/main/dist/SecurityIdentityCompliance/SecretsManager.png?raw=true"
case "Amazon Virtual Private Cloud":
return "https://github.com/awslabs/aws-icons-for-plantuml/blob/main/dist/Groups/VPC.png?raw=true"
case "AWS WAF":
return "https://github.com/awslabs/aws-icons-for-plantuml/blob/main/dist/SecurityIdentityCompliance/WAF.png?raw=true"
case "EC2 - Other":
return "https://github.com/awslabs/aws-icons-for-plantuml/blob/main/dist/Compute/EC2.png?raw=true"
case "AWS Step Functions":
return "https://github.com/awslabs/aws-icons-for-plantuml/blob/main/dist/ApplicationIntegration/StepFunctions.png?raw=true"
case "Amazon Elastic Compute Cloud - Compute":
return "https://github.com/awslabs/aws-icons-for-plantuml/blob/main/dist/Compute/EC2.png?raw=true"
case "AWS CloudTrail":
return "https://github.com/awslabs/aws-icons-for-plantuml/blob/main/dist/ManagementGovernance/CloudTrail.png?raw=true"
case "Amazon Simple Queue Service":
return "https://github.com/awslabs/aws-icons-for-plantuml/blob/main/dist/ApplicationIntegration/SimpleQueueService.png?raw=true"
case "Amazon GuardDuty":
return "https://github.com/awslabs/aws-icons-for-plantuml/blob/main/dist/SecurityIdentityCompliance/GuardDuty.png?raw=true"
case "Amazon Elastic Container Service for Kubernetes":
return "https://github.com/awslabs/aws-icons-for-plantuml/blob/main/dist/Containers/EKSCloud.png?raw=true"
case "Amazon Cognito":
return "https://github.com/awslabs/aws-icons-for-plantuml/blob/main/dist/SecurityIdentityCompliance/Cognito.png?raw=true"
case "Tax":
return ""
default:
return ""
@@ -52,7 +81,7 @@ func GetIconURL(service string) string {
func GetCost() (*costexplorer.GetCostAndUsageOutput, error) {
now := time.Now()
end := now.Format("2006-01-02")
twoDaysBefore := now.AddDate(0, 0, -2).Format("2006-01-02")
twoDaysBefore := now.AddDate(0, 0, -3).Format("2006-01-02")

granularity := "DAILY"
metrics := []string{
@@ -97,3 +126,19 @@ func GetCost() (*costexplorer.GetCostAndUsageOutput, error) {
}
return result, nil
}

func GetAccountFullName(ctx context.Context) (string, error) {
cfg, err := config.LoadDefaultConfig(ctx)
if err != nil {
return "", err
}

client := account.NewFromConfig(cfg)

output, err := client.GetContactInformation(ctx, &account.GetContactInformationInput{})
if err != nil {
return "", err
}

return *output.ContactInformation.FullName, nil
}
18 changes: 16 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -5,10 +5,14 @@ go 1.21.1
require (
github.com/aws/aws-lambda-go v1.25.0
github.com/aws/aws-sdk-go v1.40.7
github.com/aws/aws-sdk-go-v2/config v1.26.2
github.com/aws/aws-sdk-go-v2/service/account v1.14.5
github.com/kelseyhightower/envconfig v1.4.0
github.com/slack-go/slack v0.9.3
github.com/stretchr/testify v1.8.4
github.com/ucpr/mongo-streamer v0.0.4
golang.org/x/text v0.14.0
gopkg.in/yaml.v3 v3.0.1
)

require (
@@ -17,6 +21,18 @@ require (
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/logging v1.8.1 // indirect
cloud.google.com/go/longrunning v0.5.0 // indirect
github.com/aws/aws-sdk-go-v2 v1.24.0 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.16.13 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.14.10 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.9 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.5.9 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.7.2 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.10.4 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.10.9 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.18.5 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.21.5 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.26.6 // indirect
github.com/aws/smithy-go v1.19.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
@@ -34,13 +50,11 @@ require (
golang.org/x/oauth2 v0.8.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/api v0.128.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/grpc v1.56.3 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
28 changes: 28 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -18,6 +18,34 @@ github.com/aws/aws-lambda-go v1.25.0 h1:hv0Av6ooQhnqMS2jqeaph0izIwwaV2N6gsT5ad17
github.com/aws/aws-lambda-go v1.25.0/go.mod h1:jJmlefzPfGnckuHdXX7/80O3BvUUi12XOkbv4w9SGLU=
github.com/aws/aws-sdk-go v1.40.7 h1:dD5+UZxedqHeE4WakJHEhTsEARYlq8kHkYEf89R1tEo=
github.com/aws/aws-sdk-go v1.40.7/go.mod h1:585smgzpB/KqRA+K3y/NL/oYRqQvpNJYvLm+LY1U59Q=
github.com/aws/aws-sdk-go-v2 v1.24.0 h1:890+mqQ+hTpNuw0gGP6/4akolQkSToDJgHfQE7AwGuk=
github.com/aws/aws-sdk-go-v2 v1.24.0/go.mod h1:LNh45Br1YAkEKaAqvmE1m8FUx6a5b/V0oAKV7of29b4=
github.com/aws/aws-sdk-go-v2/config v1.26.2 h1:+RWLEIWQIGgrz2pBPAUoGgNGs1TOyF4Hml7hCnYj2jc=
github.com/aws/aws-sdk-go-v2/config v1.26.2/go.mod h1:l6xqvUxt0Oj7PI/SUXYLNyZ9T/yBPn3YTQcJLLOdtR8=
github.com/aws/aws-sdk-go-v2/credentials v1.16.13 h1:WLABQ4Cp4vXtXfOWOS3MEZKr6AAYUpMczLhgKtAjQ/8=
github.com/aws/aws-sdk-go-v2/credentials v1.16.13/go.mod h1:Qg6x82FXwW0sJHzYruxGiuApNo31UEtJvXVSZAXeWiw=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.14.10 h1:w98BT5w+ao1/r5sUuiH6JkVzjowOKeOJRHERyy1vh58=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.14.10/go.mod h1:K2WGI7vUvkIv1HoNbfBA1bvIZ+9kL3YVmWxeKuLQsiw=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.9 h1:v+HbZaCGmOwnTTVS86Fleq0vPzOd7tnJGbFhP0stNLs=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.9/go.mod h1:Xjqy+Nyj7VDLBtCMkQYOw1QYfAEZCVLrfI0ezve8wd4=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.5.9 h1:N94sVhRACtXyVcjXxrwK1SKFIJrA9pOJ5yu2eSHnmls=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.5.9/go.mod h1:hqamLz7g1/4EJP+GH5NBhcUMLjW+gKLQabgyz6/7WAU=
github.com/aws/aws-sdk-go-v2/internal/ini v1.7.2 h1:GrSw8s0Gs/5zZ0SX+gX4zQjRnRsMJDJ2sLur1gRBhEM=
github.com/aws/aws-sdk-go-v2/internal/ini v1.7.2/go.mod h1:6fQQgfuGmw8Al/3M2IgIllycxV7ZW7WCdVSqfBeUiCY=
github.com/aws/aws-sdk-go-v2/service/account v1.14.5 h1:sAXBYGqq4J/cPrtBrzXbEOSiYToW69qVF7heXDzcGKE=
github.com/aws/aws-sdk-go-v2/service/account v1.14.5/go.mod h1:fvSp4SHBg07Gig7K7mEsO1XUK1jnT+BZRg6oWiOMigY=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.10.4 h1:/b31bi3YVNlkzkBrm9LfpaKoaYZUxIAj4sHfOTmLfqw=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.10.4/go.mod h1:2aGXHFmbInwgP9ZfpmdIfOELL79zhdNYNmReK8qDfdQ=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.10.9 h1:Nf2sHxjMJR8CSImIVCONRi4g0Su3J+TSTbS7G0pUeMU=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.10.9/go.mod h1:idky4TER38YIjr2cADF1/ugFMKvZV7p//pVeV5LZbF0=
github.com/aws/aws-sdk-go-v2/service/sso v1.18.5 h1:ldSFWz9tEHAwHNmjx2Cvy1MjP5/L9kNoR0skc6wyOOM=
github.com/aws/aws-sdk-go-v2/service/sso v1.18.5/go.mod h1:CaFfXLYL376jgbP7VKC96uFcU8Rlavak0UlAwk1Dlhc=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.21.5 h1:2k9KmFawS63euAkY4/ixVNsYYwrwnd5fIvgEKkfZFNM=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.21.5/go.mod h1:W+nd4wWDVkSUIox9bacmkBP5NMFQeTJ/xqNabpzSR38=
github.com/aws/aws-sdk-go-v2/service/sts v1.26.6 h1:HJeiuZ2fldpd0WqngyMR6KW7ofkXNLyOaHwEIGm39Cs=
github.com/aws/aws-sdk-go-v2/service/sts v1.26.6/go.mod h1:XX5gh4CB7wAs4KhcF46G6C8a2i7eupU19dcAAE+EydU=
github.com/aws/smithy-go v1.19.0 h1:KWFKQV80DpP3vJrrA9sVAHQ5gc2z8i4EzrLhLlWXcBM=
github.com/aws/smithy-go v1.19.0/go.mod h1:NukqUGpCZIILqqiV0NIjeFh24kd/FAa4beRb6nbIUPE=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
72 changes: 72 additions & 0 deletions i18y/i18y.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package i18y

import (
"embed"
_ "embed"
"fmt"
"io/fs"
"path/filepath"
"strings"

"golang.org/x/text/language"
"golang.org/x/text/message"
"gopkg.in/yaml.v3"
)

const (
LanguagesDir = "languages"
)

var (
languages = []language.Tag{
language.Japanese,
language.English,
}
//go:embed languages/*.yaml
configByte embed.FS
)

type Messages map[string]map[string]string

func Init() error {
files, err := configByte.ReadDir(LanguagesDir)
if err != nil {
return err
}

for _, file := range files {
data, err := fs.ReadFile(configByte, fmt.Sprintf("%s/%s", LanguagesDir, file.Name()))
if err != nil {
return err
}

var m map[string]string
err = yaml.Unmarshal(data, &m)
if err != nil {
return err
}

// Make language tag from file name
l := language.Make(strings.TrimSuffix(file.Name(), filepath.Ext(file.Name())))
for k, v := range m {
err = message.SetString(l, k, v)
if err != nil {
return err
}
}
}

return nil
}

func Translate(acceptLanguage string, msg string, args ...interface{}) string {
t, _, err := language.ParseAcceptLanguage(acceptLanguage)
if err != nil {
return msg
}

matcher := language.NewMatcher(languages)
tag, _, _ := matcher.Match(t...)
p := message.NewPrinter(tag)
return p.Sprintf(msg, args...)
}
38 changes: 38 additions & 0 deletions i18y/i18y_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package i18y

import (
"testing"

"github.com/stretchr/testify/assert"
"golang.org/x/text/language"
)

func TestTranslate(t *testing.T) {
err := Init()
assert.NoError(t, err)

tests := []struct {
name string
acceptLanguage string
key string
out string
}{
{
name: "japanese",
acceptLanguage: language.Japanese.String(),
key: "cost",
out: "料金",
},
{
name: "english",
acceptLanguage: language.English.String(),
key: "cost",
out: "Cost",
},
}

for _, tt := range tests {
out := Translate(tt.acceptLanguage, tt.key)
assert.Equal(t, tt.out, out)
}
}
3 changes: 3 additions & 0 deletions i18y/languages/en.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
title: "%s cost of %s is `$%.3f`"
cost: Cost
usage: Usage
3 changes: 3 additions & 0 deletions i18y/languages/ja.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
title: "%s : %s のコスト : `$%.3f`"
cost: 料金
usage: 利用量
Loading