Skip to content

Commit

Permalink
make the scraper use the new plugin for diags
Browse files Browse the repository at this point in the history
  • Loading branch information
benoittoulme committed Mar 4, 2025
1 parent bb7f437 commit 3783c95
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
26 changes: 13 additions & 13 deletions scraper/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-framework/diag"
)

// FmtCommand is a Command implementation that rewrites Terraform config
Expand Down Expand Up @@ -65,7 +65,7 @@ func (c *FmtCommand) Run() diag.Diagnostics {
if ok {
return nil
} else {
diags = append(diags, diag.Errorf(output.String())...)
diags.AddError("formatting error", output.String())
}
}

Expand All @@ -82,7 +82,7 @@ func (c *FmtCommand) fmt(paths []string, stdout io.Writer) diag.Diagnostics {
for _, path := range paths {
info, err := os.Stat(path)
if err != nil {
diags = append(diags, diag.Errorf("No file or directory at %s", path)...)
diags.AddError(fmt.Sprintf("No file or directory at %s", path), err.Error())
return diags
}
if info.IsDir() {
Expand All @@ -95,15 +95,15 @@ func (c *FmtCommand) fmt(paths []string, stdout io.Writer) diag.Diagnostics {
if err != nil {
// Open does not produce error messages that are end-user-appropriate,
// so we'll need to simplify here.
diags = append(diags, diag.Errorf("Failed to read file %s", path)...)
diags.AddError(fmt.Sprintf("Failed to read file %s", path), err.Error())
continue
}

fileDiags := c.processFile(path, f, stdout, false)
diags = append(diags, fileDiags...)
f.Close()
default:
diags = append(diags, diag.Errorf("Only .tf and .tfvars files can be processed with terraform fmt")...)
diags.AddError("unhandled file type", "Only .tf and .tfvars files can be processed with terraform fmt")
continue
}
}
Expand All @@ -119,7 +119,7 @@ func (c *FmtCommand) processFile(path string, r io.Reader, w io.Writer, isStdout

src, err := io.ReadAll(r)
if err != nil {
diags = append(diags, diag.Errorf("Failed to read %s", path)...)
diags.AddError(fmt.Sprintf("Failed to read %s", path), err.Error())
return diags
}

Expand All @@ -133,7 +133,7 @@ func (c *FmtCommand) processFile(path string, r io.Reader, w io.Writer, isStdout
_, syntaxDiags := hclsyntax.ParseConfig(src, path, hcl.Pos{Line: 1, Column: 1})
if syntaxDiags.HasErrors() {
for _, err := range syntaxDiags.Errs() {
diags = append(diags, diag.FromErr(err)...)
diags.AddError("syntax error", err.Error())
}
return diags
}
Expand All @@ -148,14 +148,14 @@ func (c *FmtCommand) processFile(path string, r io.Reader, w io.Writer, isStdout
if c.write {
err := os.WriteFile(path, result, 0644)
if err != nil {
diags = append(diags, diag.Errorf("Failed to write %s", path)...)
diags.AddError(fmt.Sprintf("Failed to write %s", path), err.Error())
return diags
}
}
if c.diff {
diff, err := bytesDiff(src, result, path)
if err != nil {
diags = append(diags, diag.Errorf("Failed to generate diff for %s: %s", path, err)...)
diags.AddError(fmt.Sprintf("Failed to generate diff for %s", path), err.Error())
return diags
}
w.Write(diff)
Expand All @@ -165,7 +165,7 @@ func (c *FmtCommand) processFile(path string, r io.Reader, w io.Writer, isStdout
if !c.list && !c.write && !c.diff {
_, err = w.Write(result)
if err != nil {
diags = append(diags, diag.Errorf("Failed to write result")...)
diags.AddError("Failed to write result", err.Error())
}
}

Expand All @@ -181,11 +181,11 @@ func (c *FmtCommand) processDir(path string, stdout io.Writer) diag.Diagnostics
if err != nil {
switch {
case os.IsNotExist(err):
diags = append(diags, diag.Errorf("There is no configuration directory at %s", path)...)
diags.AddError(fmt.Sprintf("There is no configuration directory at %s", path), err.Error())
default:
// ReadDir does not produce error messages that are end-user-appropriate,
// so we'll need to simplify here.
diags = append(diags, diag.Errorf("Cannot read directory %s", path)...)
diags.AddError(fmt.Sprintf("Cannot read directory %s", path), err.Error())
}
return diags
}
Expand Down Expand Up @@ -215,7 +215,7 @@ func (c *FmtCommand) processDir(path string, stdout io.Writer) diag.Diagnostics
if err != nil {
// Open does not produce error messages that are end-user-appropriate,
// so we'll need to simplify here.
diags = append(diags, diag.Errorf("Failed to read file %s", subPath)...)
diags.AddError(fmt.Sprintf("Failed to read file %s", subPath), err.Error())
continue
}

Expand Down
15 changes: 8 additions & 7 deletions scraper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -206,7 +207,7 @@ func runTerraformCommand(folder string, args ...string) (errors []error) {

func runGenerateCommand(clientConfig models.ClientConfig, objectType string, logsCh chan Logs, wg *sync.WaitGroup) {
defer wg.Done()
errors := []error{}
errs := []error{}

base := models.NewItsiObj(clientConfig, "", "", objectType)

Expand Down Expand Up @@ -260,7 +261,7 @@ func runGenerateCommand(clientConfig models.ClientConfig, objectType string, log
return
}

errors = append(errors, runTerraformCommand(folder, "init")...)
errs = append(errs, runTerraformCommand(folder, "init")...)

for count, offset := base.GetPageSize(), 0; offset >= 0; offset += count {
ctx := context.Background()
Expand All @@ -270,7 +271,7 @@ func runGenerateCommand(clientConfig models.ClientConfig, objectType string, log
}
items, err := base.Dump(ctx, &models.Parameters{Offset: offset, Count: count, Fields: fields, Filter: ""})
if err != nil {
errors = append(errors, err)
errs = append(errs, err)
break
}
for _, item := range items {
Expand All @@ -280,26 +281,26 @@ func runGenerateCommand(clientConfig models.ClientConfig, objectType string, log
"ObjectType": "itsi_" + objectType,
})
if err != nil {
errors = append(errors, err)
errs = append(errs, err)
}
}

if len(items) < count {
break
}
}
errors = append(errors, runTerraformCommand(folder, "plan", "-generate-config-out=generated.tf")...)
errs = append(errs, runTerraformCommand(folder, "plan", "-generate-config-out=generated.tf")...)
diags := NewFmtCommand([]string{folder + "/generated.tf"}, false).Run()

if diags.HasError() {
for _, diag := range diags {
errors = append(errors, fmt.Errorf(diag.Detail))
errs = append(errs, errors.New(diag.Detail()))
}
}

logsCh <- Logs{
ObjectType: objectType,
Errors: errors,
Errors: errs,
}

}
Expand Down

0 comments on commit 3783c95

Please # to comment.