-
Notifications
You must be signed in to change notification settings - Fork 131
/
module.go
70 lines (52 loc) · 1.32 KB
/
module.go
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
68
69
70
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package parser
import (
"path/filepath"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/terraform-ls/internal/terraform/ast"
)
func ParseModuleFiles(fs FS, modPath string) (ast.ModFiles, ast.ModDiags, error) {
files := make(ast.ModFiles, 0)
diags := make(ast.ModDiags, 0)
infos, err := fs.ReadDir(modPath)
if err != nil {
return nil, nil, err
}
for _, info := range infos {
if info.IsDir() {
// We only care about files
continue
}
name := info.Name()
if !ast.IsModuleFilename(name) {
continue
}
// TODO: overrides
fullPath := filepath.Join(modPath, name)
src, err := fs.ReadFile(fullPath)
if err != nil {
// If a file isn't accessible, continue with reading the
// remaining module files
continue
}
filename := ast.ModFilename(name)
f, pDiags := parseFile(src, filename)
diags[filename] = pDiags
if f != nil {
files[filename] = f
}
}
return files, diags, nil
}
func ParseModuleFile(fs FS, filePath string) (*hcl.File, hcl.Diagnostics, error) {
src, err := fs.ReadFile(filePath)
if err != nil {
// If a file isn't accessible, return
return nil, nil, err
}
name := filepath.Base(filePath)
filename := ast.ModFilename(name)
f, pDiags := parseFile(src, filename)
return f, pDiags, nil
}