| 1 |
// Copyright 2018 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
// Package terraform provides a function to read Terraform output. |
| 16 |
package terraform // import "gocloud.dev/internal/testing/terraform" |
| 17 |
|
| 18 |
import ( |
| 19 |
"encoding/json" |
| 20 |
"fmt" |
| 21 |
"os/exec" |
| 22 |
) |
| 23 |
|
| 24 |
// ReadOutput runs `terraform output` on the given directory and returns |
| 25 |
// the parsed result. |
| 26 |
func ReadOutput(dir string) (map[string]Output, error) { |
| 27 |
c := exec.Command("terraform", "output", "-json") |
| 28 |
c.Dir = dir |
| 29 |
data, err := c.Output() |
| 30 |
if err != nil { |
| 31 |
return nil, fmt.Errorf("read terraform output: %v", err) |
| 32 |
} |
| 33 |
var parsed map[string]Output |
| 34 |
if err := json.Unmarshal(data, &parsed); err != nil { |
| 35 |
return nil, fmt.Errorf("read terraform output: %v", err) |
| 36 |
} |
| 37 |
return parsed, nil |
| 38 |
} |
| 39 |
|
| 40 |
// Output describes a single output value. |
| 41 |
type Output struct { |
| 42 |
Type string `json:"type"` // one of "string", "list", or "map" |
| 43 |
Sensitive bool `json:"sensitive"` |
| 44 |
Value any `json:"value"` |
| 45 |
} |