Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2b748d8

Browse files
committed
chore: replace tfparse with preview
1 parentb4aa643 commit2b748d8

File tree

8 files changed

+307
-128
lines changed

8 files changed

+307
-128
lines changed

‎archive/fs/zip.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package archivefs
2+
3+
import (
4+
"archive/zip"
5+
"io"
6+
"io/fs"
7+
8+
"github.com/spf13/afero"
9+
"github.com/spf13/afero/zipfs"
10+
)
11+
12+
// FromZipReader creates a read-only in-memory FS
13+
funcFromZipReader(r io.ReaderAt,sizeint64) (fs.FS,error) {
14+
zr,err:=zip.NewReader(r,size)
15+
iferr!=nil {
16+
returnnil,err
17+
}
18+
returnafero.NewIOFS(zipfs.New(zr)),nil
19+
}

‎coderd/templateversions.go

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
package coderd
22

33
import (
4+
"bytes"
45
"context"
56
"crypto/sha256"
67
"database/sql"
78
"encoding/hex"
89
"encoding/json"
910
"errors"
1011
"fmt"
12+
"io/fs"
1113
"net/http"
1214
"os"
15+
"strings"
1316

1417
"github.com/go-chi/chi/v5"
1518
"github.com/google/uuid"
@@ -18,6 +21,8 @@ import (
1821
"golang.org/x/xerrors"
1922

2023
"cdr.dev/slog"
24+
archivefs"github.com/coder/coder/v2/archive/fs"
25+
"github.com/coder/preview"
2126

2227
"github.com/coder/coder/v2/coderd/audit"
2328
"github.com/coder/coder/v2/coderd/database"
@@ -1589,36 +1594,51 @@ func (api *API) postTemplateVersionsByOrganization(rw http.ResponseWriter, r *ht
15891594
}
15901595
}()
15911596

1592-
iferr:=tfparse.WriteArchive(file.Data,file.Mimetype,tempDir);err!=nil {
1593-
httpapi.Write(ctx,rw,http.StatusInternalServerError, codersdk.Response{
1594-
Message:"Internal error checking workspace tags",
1595-
Detail:"extract archive to tempdir: "+err.Error(),
1596-
})
1597-
return
1597+
varfiles fs.FS
1598+
switchfile.Mimetype {
1599+
case"application/x-tar":
1600+
files=archivefs.FromTarReader(bytes.NewBuffer(file.Data))
1601+
case"application/zip":
1602+
files,err=archivefs.FromZipReader(bytes.NewReader(file.Data),int64(len(file.Data)))
1603+
iferr!=nil {
1604+
httpapi.Write(ctx,rw,http.StatusInternalServerError, codersdk.Response{
1605+
Message:"Internal error checking workspace tags",
1606+
Detail:"extract archive to tempdir: "+err.Error(),
1607+
})
1608+
return
1609+
}
15981610
}
15991611

1600-
parser,diags:=tfparse.New(tempDir,tfparse.WithLogger(api.Logger.Named("tfparse")))
1612+
output,diags:=preview.Preview(ctx, preview.Input{},files)
16011613
ifdiags.HasErrors() {
16021614
httpapi.Write(ctx,rw,http.StatusInternalServerError, codersdk.Response{
1603-
Message:"Internal errorchecking workspace tags",
1615+
Message:"Internal errorparsing workspace tags",
16041616
Detail:"parse module: "+diags.Error(),
16051617
})
16061618
return
16071619
}
1620+
parsedTags:=output.WorkspaceTags
1621+
1622+
failedTags:=parsedTags.UnusableTags()
1623+
iflen(failedTags)>0 {
1624+
validations:=make([]codersdk.ValidationError,0,len(failedTags))
1625+
names:=make([]string,0,len(failedTags))
1626+
for_,tag:=rangefailedTags {
1627+
validations=append(validations,tfparse.TagValidationResponse(tag))
1628+
}
16081629

1609-
parsedTags,err:=parser.WorkspaceTagDefaults(ctx)
1610-
iferr!=nil {
16111630
httpapi.Write(ctx,rw,http.StatusInternalServerError, codersdk.Response{
1612-
Message:"Internal error checking workspace tags",
1613-
Detail:"evaluate default values of workspace tags: "+err.Error(),
1631+
Message:"Internal error checking workspace tags",
1632+
Detail:fmt.Sprintf("evaluate default values of workspace tags [%s]",strings.Join(names,", ")),
1633+
Validations:validations,
16141634
})
16151635
return
16161636
}
16171637

16181638
// Ensure the "owner" tag is properly applied in addition to request tags and coder_workspace_tags.
16191639
// User-specified tags in the request will take precedence over tags parsed from `coder_workspace_tags`
16201640
// data sources defined in the template file.
1621-
tags:=provisionersdk.MutateTags(apiKey.UserID,parsedTags,req.ProvisionerTags)
1641+
tags:=provisionersdk.MutateTags(apiKey.UserID,parsedTags.Tags(),req.ProvisionerTags)
16221642

16231643
vartemplateVersion database.TemplateVersion
16241644
varprovisionerJob database.ProvisionerJob

‎go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ require (
483483
require (
484484
github.com/coder/agentapi-sdk-gov0.0.0-20250505131810-560d1d88d225
485485
github.com/coder/aisdk-gov0.0.9
486-
github.com/coder/previewv1.0.1
486+
github.com/coder/previewv1.0.3-0.20250627161416-e1ccd88ba6c0
487487
github.com/fsnotify/fsnotifyv1.9.0
488488
github.com/mark3labs/mcp-gov0.32.0
489489
)
@@ -538,3 +538,5 @@ require (
538538
google.golang.org/genaiv1.12.0// indirect
539539
k8s.io/utilsv0.0.0-20241210054802-24370beab758// indirect
540540
)
541+
542+
replacegithub.com/coder/preview =>/home/steven/go/src/github.com/coder/preview/

‎go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -916,8 +916,8 @@ github.com/coder/pq v1.10.5-0.20240813183442-0c420cb5a048 h1:3jzYUlGH7ZELIH4XggX
916916
github.com/coder/pqv1.10.5-0.20240813183442-0c420cb5a048/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
917917
github.com/coder/prettyv0.0.0-20230908205945-e89ba86370e0 h1:3A0ES21Ke+FxEM8CXx9n47SZOKOpgSE1bbJzlE4qPVs=
918918
github.com/coder/prettyv0.0.0-20230908205945-e89ba86370e0/go.mod h1:5UuS2Ts+nTToAMeOjNlnHFkPahrtDkmpydBen/3wgZc=
919-
github.com/coder/previewv1.0.1 h1:f6q+RjNelwnkyXfGbmVlb4dcUOQ0z4mPsb2kuQpFHuU=
920-
github.com/coder/previewv1.0.1/go.mod h1:efDWGlO/PZPrvdt5QiDhMtTUTkPxejXo9c0wmYYLLjM=
919+
github.com/coder/previewv1.0.3-0.20250627161416-e1ccd88ba6c0 h1:6fbDlBk0MjSqkantXyLO1tN4fl40lnh90Q8ZTk9vXVA=
920+
github.com/coder/previewv1.0.3-0.20250627161416-e1ccd88ba6c0/go.mod h1:efDWGlO/PZPrvdt5QiDhMtTUTkPxejXo9c0wmYYLLjM=
921921
github.com/coder/quartzv0.2.1 h1:QgQ2Vc1+mvzewg2uD/nj8MJ9p9gE+QhGJm+Z+NGnrSE=
922922
github.com/coder/quartzv0.2.1/go.mod h1:vsiCc+AHViMKH2CQpGIpFgdHIEQsxwm8yCscqKmzbRA=
923923
github.com/coder/retryv1.5.1 h1:iWu8YnD8YqHs3XwqrqsjoBTAVqT9ml6z9ViJ2wlMiqc=

‎provisioner/terraform/parse.go

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ package terraform
22

33
import (
44
"fmt"
5+
"os"
56
"path/filepath"
67
"strings"
78

8-
"github.com/hashicorp/terraform-config-inspect/tfconfig"
9+
"github.com/hashicorp/hcl/v2"
910
"github.com/mitchellh/go-wordwrap"
1011

1112
"github.com/coder/coder/v2/coderd/tracing"
12-
"github.com/coder/coder/v2/provisioner/terraform/tfparse"
1313
"github.com/coder/coder/v2/provisionersdk"
1414
"github.com/coder/coder/v2/provisionersdk/proto"
15+
"github.com/coder/preview"
1516
)
1617

1718
// Parse extracts Terraform variables from source-code.
@@ -21,50 +22,52 @@ func (s *server) Parse(sess *provisionersdk.Session, _ *proto.ParseRequest, _ <-
2122
deferspan.End()
2223

2324
// Load the module and print any parse errors.
24-
parser,diags:=tfparse.New(sess.WorkDirectory,tfparse.WithLogger(s.logger.Named("tfparse")))
25+
output,diags:=preview.Preview(ctx, preview.Input{},os.DirFS(sess.WorkDirectory))
2526
ifdiags.HasErrors() {
2627
returnprovisionersdk.ParseErrorf("load module: %s",formatDiagnostics(sess.WorkDirectory,diags))
2728
}
2829

29-
workspaceTags,_,err:=parser.WorkspaceTags(ctx)
30-
iferr!=nil {
31-
returnprovisionersdk.ParseErrorf("can't load workspace tags: %v",err)
30+
tags:=output.WorkspaceTags
31+
failedTags:=tags.UnusableTags()
32+
iflen(failedTags)>0 {
33+
returnprovisionersdk.ParseErrorf("can't load workspace tags: %v",failedTags.SafeNames())
3234
}
3335

34-
templateVariables,err:=parser.TemplateVariables()
35-
iferr!=nil {
36-
returnprovisionersdk.ParseErrorf("can't load template variables: %v",err)
37-
}
36+
// TODO: THIS
37+
//templateVariables, err := parser.TemplateVariables()
38+
//if err != nil {
39+
//return provisionersdk.ParseErrorf("can't load template variables: %v", err)
40+
//}
3841

3942
return&proto.ParseComplete{
40-
TemplateVariables:templateVariables,
41-
WorkspaceTags:workspaceTags,
43+
TemplateVariables:nil,// TODO: Handle template variables.
44+
WorkspaceTags:tags.Tags(),
4245
}
4346
}
4447

4548
// FormatDiagnostics returns a nicely formatted string containing all of the
4649
// error details within the tfconfig.Diagnostics. We need to use this because
4750
// the default format doesn't provide much useful information.
48-
funcformatDiagnostics(baseDirstring,diagstfconfig.Diagnostics)string {
51+
funcformatDiagnostics(baseDirstring,diagshcl.Diagnostics)string {
4952
varmsgs strings.Builder
5053
for_,d:=rangediags {
5154
// Convert severity.
5255
severity:="UNKNOWN SEVERITY"
5356
switch {
54-
cased.Severity==tfconfig.DiagError:
57+
cased.Severity==hcl.DiagError:
5558
severity="ERROR"
56-
cased.Severity==tfconfig.DiagWarning:
59+
cased.Severity==hcl.DiagWarning:
5760
severity="WARN"
5861
}
5962

6063
// Determine filepath and line
6164
location:="unknown location"
62-
ifd.Pos!=nil {
63-
filename,err:=filepath.Rel(baseDir,d.Pos.Filename)
65+
ifd.Subject!=nil {
66+
filename,err:=filepath.Rel(baseDir,d.Subject.Filename)
6467
iferr!=nil {
65-
filename=d.Pos.Filename
68+
filename=d.Subject.Filename
6669
}
67-
location=fmt.Sprintf("%s:%d",filename,d.Pos.Line)
70+
location=fmt.Sprintf("%s:%d",filename,d.Subject.Start.Line)
6871
}
6972

7073
_,_=msgs.WriteString(fmt.Sprintf("\n%s: %s (%s)\n",severity,d.Summary,location))
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package tfparse
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"io/fs"
7+
"strings"
8+
9+
"github.com/hashicorp/hcl/v2"
10+
11+
"github.com/coder/coder/v2/codersdk"
12+
"github.com/coder/preview"
13+
"github.com/coder/preview/hclext"
14+
previewtypes"github.com/coder/preview/types"
15+
)
16+
17+
typeParsedstruct {
18+
output*preview.Output
19+
}
20+
21+
//
22+
//// TODO: Maybe swap workdir with an fs.FS interface?
23+
//func New(files fs.FS, opt ...Option) (*Parser, hcl.Diagnostics) {
24+
//return &Parser{
25+
//logger: slog.Logger{},
26+
//workdir: files,
27+
//}, nil
28+
//}
29+
30+
funcNew(ctx context.Context,workdir fs.FS,input preview.Input) (*preview.Output, hcl.Diagnostics) {
31+
output,diags:=preview.Preview(ctx,input,workdir)
32+
33+
ifdiags.HasErrors() {
34+
returnnil,diags
35+
}
36+
returnoutput,nil
37+
}
38+
39+
funcTagValidationResponse(tag previewtypes.Tag) codersdk.ValidationError {
40+
name:=tag.KeyString()
41+
ifname==previewtypes.UnknownStringValue {
42+
name="unknown"
43+
}
44+
45+
const (
46+
key="key"
47+
value="value"
48+
)
49+
50+
diagErr:="Invalid tag %s: %s"
51+
iftag.Key.ValueDiags.HasErrors() {
52+
return codersdk.ValidationError{
53+
Field:name,
54+
Detail:fmt.Sprintf(diagErr,key,tag.Key.ValueDiags.Error()),
55+
}
56+
}
57+
58+
iftag.Value.ValueDiags.HasErrors() {
59+
return codersdk.ValidationError{
60+
Field:name,
61+
Detail:fmt.Sprintf(diagErr,value,tag.Value.ValueDiags.Error()),
62+
}
63+
}
64+
65+
invalidErr:="Tag %s is not valid, it must be a non-null string value."
66+
if!tag.Key.Valid() {
67+
return codersdk.ValidationError{
68+
Field:name,
69+
Detail:fmt.Sprintf(invalidErr,key),
70+
}
71+
}
72+
73+
if!tag.Value.Valid() {
74+
return codersdk.ValidationError{
75+
Field:name,
76+
Detail:fmt.Sprintf(invalidErr,value),
77+
}
78+
}
79+
80+
unknownErr:="Tag %s is not known, it likely refers to a variable that is not set or has no default. References: [%s]"
81+
if!tag.Key.IsKnown() {
82+
vars:=hclext.ReferenceNames(tag.Key.ValueExpr)
83+
84+
return codersdk.ValidationError{
85+
Field:name,
86+
Detail:fmt.Sprintf(unknownErr,key,strings.Join(vars,", ")),
87+
}
88+
}
89+
90+
if!tag.Value.IsKnown() {
91+
vars:=hclext.ReferenceNames(tag.Value.ValueExpr)
92+
93+
return codersdk.ValidationError{
94+
Field:name,
95+
Detail:fmt.Sprintf(unknownErr,value,strings.Join(vars,", ")),
96+
}
97+
}
98+
99+
return codersdk.ValidationError{
100+
Field:name,
101+
Detail:fmt.Sprintf("Tag is invalid for some unknown reason. Please check the tag's value and key."),
102+
}
103+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp