Skip to content

Commit b379cc5

Browse files
authored
Merge pull request #70 from coder/stevenmasley/os
cleanup api surface of `preview` package
2 parents d865621 + 0a8d236 commit b379cc5

File tree

7 files changed

+48
-42
lines changed

7 files changed

+48
-42
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
[![godoc](https://pkg.go.dev/badge/github.com/coder/preview.svg)](https://pkg.go.dev/github.com/coder/preview)
2424
[![Go Report Card](https://goreportcard.com/badge/github.com/coder/preview)](https://goreportcard.com/report/github.com/coder/preview)
2525
[![OpenSSF Best Practices](https://www.bestpractices.dev/projects/9511/badge)](https://www.bestpractices.dev/projects/9511)
26-
[![OpenSSF Scorecard](https://api.securityscorecards.dev/projects/github.com/coder/preview/badge)](https://scorecard.dev/viewer/?uri=github.com%2Fcoder%2Fpreview)
2726
[![license](https://img.shields.io/github/license/coder/preview)](./LICENSE)
2827

2928
</div>

owner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"golang.org/x/xerrors"
1111
)
1212

13-
func WorkspaceOwnerHook(dfs fs.FS, input Input) (func(ctx *tfcontext.Context, blocks terraform.Blocks, inputVars map[string]cty.Value), error) {
13+
func workspaceOwnerHook(dfs fs.FS, input Input) (func(ctx *tfcontext.Context, blocks terraform.Blocks, inputVars map[string]cty.Value), error) {
1414
if input.Owner.Groups == nil {
1515
input.Owner.Groups = []string{}
1616
}

parameter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/coder/preview/types"
1212
)
1313

14-
func RichParameters(modules terraform.Modules) ([]types.Parameter, hcl.Diagnostics) {
14+
func parameters(modules terraform.Modules) ([]types.Parameter, hcl.Diagnostics) {
1515
diags := make(hcl.Diagnostics, 0)
1616
params := make([]types.Parameter, 0)
1717
exists := make(map[string][]types.Parameter)

paramhook.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import (
88
"github.com/coder/preview/hclext"
99
)
1010

11-
// ParameterContextsEvalHook is called in a loop, so if parameters affect
11+
// parameterContextsEvalHook is called in a loop, so if parameters affect
1212
// other parameters, this can solve the problem 1 "evaluation" at a time.
1313
//
1414
// Omitting to set a default value is OK, as long as at least 1 parameter
1515
// is resolvable. The resolvable parameter will be accessible on the next
1616
// iteration.
17-
func ParameterContextsEvalHook(input Input) func(ctx *tfcontext.Context, blocks terraform.Blocks, inputVars map[string]cty.Value) {
17+
func parameterContextsEvalHook(input Input) func(ctx *tfcontext.Context, blocks terraform.Blocks, inputVars map[string]cty.Value) {
1818
return func(ctx *tfcontext.Context, blocks terraform.Blocks, inputVars map[string]cty.Value) {
1919
data := blocks.OfType("data")
2020
for _, block := range data {

plan.go

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"slices"
1111
"strings"
1212

13-
"github.com/aquasecurity/trivy/pkg/iac/scanners/terraformplan/tfjson/parser"
1413
"github.com/aquasecurity/trivy/pkg/iac/terraform"
1514
tfcontext "github.com/aquasecurity/trivy/pkg/iac/terraform/context"
1615
tfjson "github.com/hashicorp/terraform-json"
@@ -20,7 +19,7 @@ import (
2019
"github.com/coder/preview/hclext"
2120
)
2221

23-
func PlanJSONHook(dfs fs.FS, input Input) (func(ctx *tfcontext.Context, blocks terraform.Blocks, inputVars map[string]cty.Value), error) {
22+
func planJSONHook(dfs fs.FS, input Input) (func(ctx *tfcontext.Context, blocks terraform.Blocks, inputVars map[string]cty.Value), error) {
2423
var contents io.Reader = bytes.NewReader(input.PlanJSON)
2524
// Also accept `{}` as an empty plan. If this is stored in postgres or another json
2625
// type, then `{}` is the "empty" value.
@@ -36,7 +35,7 @@ func PlanJSONHook(dfs fs.FS, input Input) (func(ctx *tfcontext.Context, blocks t
3635
}
3736
}
3837

39-
plan, err := ParsePlanJSON(contents)
38+
plan, err := parsePlanJSON(contents)
4039
if err != nil {
4140
return nil, fmt.Errorf("unable to parse plan JSON: %w", err)
4241
}
@@ -216,28 +215,15 @@ func toCtyValue(a any) (cty.Value, error) {
216215
}
217216
}
218217

219-
// ParsePlanJSON can parse the JSON output of a Terraform plan.
218+
// parsePlanJSON can parse the JSON output of a Terraform plan.
220219
// terraform plan out.plan
221220
// terraform show -json out.plan
222-
func ParsePlanJSON(reader io.Reader) (*tfjson.Plan, error) {
221+
func parsePlanJSON(reader io.Reader) (*tfjson.Plan, error) {
223222
plan := new(tfjson.Plan)
224223
plan.FormatVersion = tfjson.PlanFormatVersionConstraints
225224
return plan, json.NewDecoder(reader).Decode(plan)
226225
}
227226

228-
// ParsePlanJSON can parse the JSON output of a Terraform plan.
229-
// terraform plan out.plan
230-
// terraform show -json out.plan
231-
func TrivyParsePlanJSON(reader io.Reader) (*tfjson.Plan, error) {
232-
p := parser.New()
233-
plan, err := p.Parse(reader)
234-
var _ = plan
235-
236-
plan.ToFS()
237-
238-
return nil, err
239-
}
240-
241227
func keyMatcher(key cty.Value) func(to any) bool {
242228
switch {
243229
case key.Type().Equals(cty.Number):

preview.go

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@ import (
55
"encoding/json"
66
"fmt"
77
"io/fs"
8-
"log/slog"
9-
"os"
108
"path/filepath"
119

1210
"github.com/aquasecurity/trivy/pkg/iac/scanners/terraform/parser"
13-
"github.com/aquasecurity/trivy/pkg/log"
1411
"github.com/hashicorp/hcl/v2"
1512
"github.com/zclconf/go-cty/cty"
1613

@@ -28,16 +25,40 @@ type Input struct {
2825
}
2926

3027
type Output struct {
31-
ModuleOutput cty.Value
28+
// ModuleOutput is any 'output' values from the terraform files. This has 0
29+
// effect on the parameters, tags, etc. It can be helpful for debugging, as it
30+
// allows exporting some terraform values to the caller to review.
31+
ModuleOutput cty.Value
32+
3233
Parameters []types.Parameter
3334
WorkspaceTags types.TagBlocks
34-
Files map[string]*hcl.File
35+
// Files is included for printing diagnostics.
36+
// TODO: Is the memory impact of this too much? Should we render diagnostic source code
37+
// into the diagnostics up front? and remove this?
38+
Files map[string]*hcl.File
3539
}
3640

37-
func Preview(ctx context.Context, input Input, dir fs.FS) (*Output, hcl.Diagnostics) {
38-
// TODO: FIX LOGGING
39-
slog.SetLogLoggerLevel(slog.LevelDebug)
40-
slog.SetDefault(slog.New(log.NewHandler(os.Stderr, nil)))
41+
func Preview(ctx context.Context, input Input, dir fs.FS) (output *Output, diagnostics hcl.Diagnostics) {
42+
// The trivy package works with `github.com/zclconf/go-cty`. This package is
43+
// similar to `reflect` in its usage. This package can panic if types are
44+
// misused. To protect the caller, a general `recover` is used to catch any
45+
// mistakes. If this happens, there is a developer bug that needs to be resolved.
46+
defer func() {
47+
if r := recover(); r != nil {
48+
diagnostics = hcl.Diagnostics{
49+
{
50+
Severity: hcl.DiagError,
51+
Summary: "Panic occurred in preview. This should not happen, please report this to Coder.",
52+
Detail: fmt.Sprintf("panic in preview: %+v", r),
53+
},
54+
}
55+
}
56+
}()
57+
58+
// TODO: Fix logging. There is no way to pass in an instanced logger to
59+
// the parser.
60+
//slog.SetLogLoggerLevel(slog.LevelDebug)
61+
//slog.SetDefault(slog.New(log.NewHandler(os.Stderr, nil)))
4162

4263
varFiles, err := tfVarFiles("", dir)
4364
if err != nil {
@@ -50,7 +71,7 @@ func Preview(ctx context.Context, input Input, dir fs.FS) (*Output, hcl.Diagnost
5071
}
5172
}
5273

53-
planHook, err := PlanJSONHook(dir, input)
74+
planHook, err := planJSONHook(dir, input)
5475
if err != nil {
5576
return nil, hcl.Diagnostics{
5677
{
@@ -61,7 +82,7 @@ func Preview(ctx context.Context, input Input, dir fs.FS) (*Output, hcl.Diagnost
6182
}
6283
}
6384

64-
ownerHook, err := WorkspaceOwnerHook(dir, input)
85+
ownerHook, err := workspaceOwnerHook(dir, input)
6586
if err != nil {
6687
return nil, hcl.Diagnostics{
6788
{
@@ -81,7 +102,7 @@ func Preview(ctx context.Context, input Input, dir fs.FS) (*Output, hcl.Diagnost
81102
parser.OptionWithTFVarsPaths(varFiles...),
82103
parser.OptionWithEvalHook(planHook),
83104
parser.OptionWithEvalHook(ownerHook),
84-
parser.OptionWithEvalHook(ParameterContextsEvalHook(input)),
105+
parser.OptionWithEvalHook(parameterContextsEvalHook(input)),
85106
)
86107

87108
err = p.ParseFS(ctx, ".")
@@ -107,8 +128,8 @@ func Preview(ctx context.Context, input Input, dir fs.FS) (*Output, hcl.Diagnost
107128
}
108129

109130
diags := make(hcl.Diagnostics, 0)
110-
rp, rpDiags := RichParameters(modules)
111-
tags, tagDiags := WorkspaceTags(modules, p.Files())
131+
rp, rpDiags := parameters(modules)
132+
tags, tagDiags := workspaceTags(modules, p.Files())
112133

113134
// Add warnings
114135
diags = diags.Extend(warnings(modules))

workspacetags.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/coder/preview/types"
1111
)
1212

13-
func WorkspaceTags(modules terraform.Modules, files map[string]*hcl.File) (types.TagBlocks, hcl.Diagnostics) {
13+
func workspaceTags(modules terraform.Modules, files map[string]*hcl.File) (types.TagBlocks, hcl.Diagnostics) {
1414
diags := make(hcl.Diagnostics, 0)
1515
tagBlocks := make(types.TagBlocks, 0)
1616

@@ -65,7 +65,7 @@ func WorkspaceTags(modules terraform.Modules, files map[string]*hcl.File) (types
6565
var tags []types.Tag
6666
tagsValue.ForEachElement(func(key cty.Value, val cty.Value) (stop bool) {
6767
r := tagsAttr.HCLAttribute().Expr.Range()
68-
tag, tagDiag := NewTag(&r, files, key, val)
68+
tag, tagDiag := newTag(&r, files, key, val)
6969
if tagDiag != nil {
7070
diags = diags.Append(tagDiag)
7171
return false
@@ -76,7 +76,7 @@ func WorkspaceTags(modules terraform.Modules, files map[string]*hcl.File) (types
7676
return false
7777
})
7878
//for _, item := range tagsObj.Items {
79-
// tag, tagDiag := NewTag(tagsObj, files, item, evCtx)
79+
// tag, tagDiag := newTag(tagsObj, files, item, evCtx)
8080
// if tagDiag != nil {
8181
// diags = diags.Append(tagDiag)
8282
// continue
@@ -94,8 +94,8 @@ func WorkspaceTags(modules terraform.Modules, files map[string]*hcl.File) (types
9494
return tagBlocks, diags
9595
}
9696

97-
// NewTag creates a workspace tag from its hcl expression.
98-
func NewTag(srcRange *hcl.Range, files map[string]*hcl.File, key, val cty.Value) (types.Tag, *hcl.Diagnostic) {
97+
// newTag creates a workspace tag from its hcl expression.
98+
func newTag(srcRange *hcl.Range, files map[string]*hcl.File, key, val cty.Value) (types.Tag, *hcl.Diagnostic) {
9999
//key, kdiags := expr.KeyExpr.Value(evCtx)
100100
//val, vdiags := expr.ValueExpr.Value(evCtx)
101101

0 commit comments

Comments
 (0)