src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

config.go (950B)


      1 package config
      2 
      3 import (
      4 	"context"
      5 	_ "embed"
      6 	"encoding/json"
      7 	"flag"
      8 	"fmt"
      9 	"strconv"
     10 
     11 	"github.com/aws/aws-sdk-go-v2/aws"
     12 	"github.com/aws/aws-sdk-go-v2/config"
     13 )
     14 
     15 var (
     16 	//go:embed config.json
     17 	configuration []byte
     18 	ttl           = flag.String("ttl", "300", "TTL")
     19 )
     20 
     21 type Config struct {
     22 	AWS          *aws.Config `json:"-"`
     23 	HostedZoneId string      `json:"hostedZoneId"`
     24 	TTL          int64       `json:"-"`
     25 }
     26 
     27 func New(ctx context.Context) (*Config, error) {
     28 	flag.Parse()
     29 
     30 	var cfg = &Config{}
     31 	if err := json.Unmarshal(configuration, cfg); err != nil {
     32 		return nil, fmt.Errorf("failed to parse config file: %v", err)
     33 	}
     34 
     35 	awscfg, err := config.LoadDefaultConfig(ctx)
     36 	if err != nil {
     37 		return nil, fmt.Errorf("failed to retrieve AWS config: %v", err)
     38 	}
     39 	cfg.AWS = &awscfg
     40 
     41 	if n, err := strconv.ParseInt(*ttl, 10, 64); err != nil {
     42 		return nil, fmt.Errorf("failed to parse ttl: %v", err)
     43 	} else {
     44 		cfg.TTL = n
     45 	}
     46 
     47 	return cfg, nil
     48 }