-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
-
67
// SPDX-FileCopyrightText: 2025 Shota FUJI <pockawoooh@gmail.com>
// SPDX-License-Identifier: AGPL-3.0-only
package tofu
import (
"encoding/json"
"fmt"
"log"
"os/exec"
)
var tofuBin = "tofu"
type stringOutput struct {
Value string
}
type rawOutput struct {
CloudfrontDistributionID stringOutput `json:"cloudfront_distribution_id"`
S3BucketName stringOutput `json:"s3_bucket_name"`
}
type Output struct {
CloudfrontDistributionID string
S3BucketName string
}
func NewOutput(tfDir string) (*Output, error) {
cmd := exec.Command(tofuBin, fmt.Sprintf("-chdir=%s", tfDir), "output", "-json")
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
if err := cmd.Start(); err != nil {
return nil, err
}
var raw rawOutput
if err := json.NewDecoder(stdout).Decode(&raw); err != nil {
return nil, fmt.Errorf("Tofu の出力を読み込めませんでした: %s", err)
}
if err := cmd.Wait(); err != nil {
return nil, err
}
output := Output{
CloudfrontDistributionID: raw.CloudfrontDistributionID.Value,
S3BucketName: raw.S3BucketName.Value,
}
if output.CloudfrontDistributionID == "" {
return nil, fmt.Errorf("cloudfront_distribution_id が取得できませんでした")
}
if output.S3BucketName == "" {
return nil, fmt.Errorf("s3_bucket_name が取得できませんでした")
}
log.Printf("cloudfront_distribution_id: %s\n", output.CloudfrontDistributionID)
log.Printf("s3_bucket_name: %s\n", output.S3BucketName)
return &output, nil
}