Skip to content

Commit

Permalink
Expose some data from feature for commands
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanck committed Apr 23, 2024
1 parent f5c3857 commit f09b92c
Show file tree
Hide file tree
Showing 13 changed files with 351 additions and 296 deletions.
23 changes: 23 additions & 0 deletions internal/features/modules/modules_feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"path/filepath"

"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-version"
"github.com/hashicorp/hcl-lang/decoder"
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/terraform-ls/internal/document"
Expand Down Expand Up @@ -413,3 +414,25 @@ func (f *ModulesFeature) Paths(ctx context.Context) []lang.Path {

return paths
}

func (f *ModulesFeature) DeclaredModuleCalls(modPath string) (map[string]tfmod.DeclaredModuleCall, error) {
return f.store.DeclaredModuleCalls(modPath)
}

func (f *ModulesFeature) ProviderRequirements(modPath string) (tfmod.ProviderRequirements, error) {
mod, err := f.store.ModuleRecordByPath(modPath)
if err != nil {
return nil, err
}

return mod.Meta.ProviderRequirements, nil
}

func (f *ModulesFeature) CoreRequirements(modPath string) (version.Constraints, error) {
mod, err := f.store.ModuleRecordByPath(modPath)
if err != nil {
return nil, err
}

return mod.Meta.CoreRequirements, nil
}
14 changes: 14 additions & 0 deletions internal/features/rootmodules/root_modules_feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
globalState "github.com/hashicorp/terraform-ls/internal/state"
"github.com/hashicorp/terraform-ls/internal/terraform/exec"
op "github.com/hashicorp/terraform-ls/internal/terraform/module/operation"
tfaddr "github.com/hashicorp/terraform-registry-address"
tfmod "github.com/hashicorp/terraform-schema/module"
)

Expand Down Expand Up @@ -142,6 +143,19 @@ func (f *RootModulesFeature) TerraformVersion(modPath string) *version.Version {
return version.TerraformVersion
}

func (f *RootModulesFeature) InstalledProviders(modPath string) (map[tfaddr.Provider]*version.Version, error) {
record, err := f.store.RootRecordByPath(modPath)
if err != nil {
return nil, err
}

return record.InstalledProviders, nil
}

func (f *RootModulesFeature) CallersOfModule(modPath string) ([]string, error) {
return f.store.CallersOfModule(modPath)
}

func (f *RootModulesFeature) ModuleManifestChanged(ctx context.Context, modHandle document.DirHandle) (job.IDs, error) {
ids := make(job.IDs, 0)

Expand Down
6 changes: 3 additions & 3 deletions internal/features/rootmodules/state/root_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,22 +375,22 @@ func (s *RootStore) UpdateTerraformAndProviderVersions(modPath string, tfVer *ve
return nil
}

func (s *RootStore) CallersOfModule(modPath string) ([]*RootRecord, error) {
func (s *RootStore) CallersOfModule(modPath string) ([]string, error) {
txn := s.db.Txn(false)
it, err := txn.Get(s.tableName, "id")
if err != nil {
return nil, err
}

callers := make([]*RootRecord, 0)
callers := make([]string, 0)
for item := it.Next(); item != nil; item = it.Next() {
record := item.(*RootRecord)

if record.ModManifest == nil {
continue
}
if record.ModManifest.ContainsLocalModule(modPath) {
callers = append(callers, record)
callers = append(callers, record.path)
}
}

Expand Down
20 changes: 18 additions & 2 deletions internal/langserver/handlers/command/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,26 @@ package command
import (
"log"

"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-ls/internal/state"
tfaddr "github.com/hashicorp/terraform-registry-address"
tfmod "github.com/hashicorp/terraform-schema/module"
)

type FeatureReader interface {
// ModulesFeature
DeclaredModuleCalls(modPath string) (map[string]tfmod.DeclaredModuleCall, error)
ProviderRequirements(modPath string) (tfmod.ProviderRequirements, error)
CoreRequirements(modPath string) (version.Constraints, error)
// RootModulesFeature
CallersOfModule(modPath string) ([]string, error)
InstalledModuleCalls(modPath string) (map[string]tfmod.InstalledModuleCall, error)
InstalledProviders(modPath string) (map[tfaddr.Provider]*version.Version, error)
TerraformVersion(modPath string) *version.Version
}

type CmdHandler struct {
StateStore *state.StateStore
Logger *log.Logger
StateStore *state.StateStore
Logger *log.Logger
FeatureReader FeatureReader
}
20 changes: 1 addition & 19 deletions internal/langserver/handlers/command/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/hashicorp/terraform-ls/internal/langserver/cmd"
"github.com/hashicorp/terraform-ls/internal/langserver/errors"
"github.com/hashicorp/terraform-ls/internal/langserver/progress"
"github.com/hashicorp/terraform-ls/internal/state"
"github.com/hashicorp/terraform-ls/internal/terraform/module"
"github.com/hashicorp/terraform-ls/internal/uri"
)
Expand All @@ -28,24 +27,7 @@ func (h *CmdHandler) TerraformInitHandler(ctx context.Context, args cmd.CommandA
}

dirHandle := document.DirHandleFromURI(dirUri)

mod, err := h.StateStore.Roots.RootRecordByPath(dirHandle.Path())
if err != nil {
if state.IsRecordNotFound(err) {
err = h.StateStore.Roots.Add(dirHandle.Path())
if err != nil {
return nil, err
}
mod, err = h.StateStore.Roots.RootRecordByPath(dirHandle.Path())
if err != nil {
return nil, err
}
} else {
return nil, err
}
}

tfExec, err := module.TerraformExecutorForModule(ctx, mod.Path())
tfExec, err := module.TerraformExecutorForModule(ctx, dirHandle.Path())
if err != nil {
return nil, errors.EnrichTfExecError(err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/langserver/handlers/command/module_callers.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ func (h *CmdHandler) ModuleCallersHandler(ctx context.Context, args cmd.CommandA
return nil, err
}

modCallers, err := h.StateStore.Roots.CallersOfModule(modPath)
modCallers, err := h.FeatureReader.CallersOfModule(modPath)
if err != nil {
return nil, err
}

callers := make([]moduleCaller, 0)
for _, caller := range modCallers {
callers = append(callers, moduleCaller{
URI: uri.FromPath(caller.Path()),
URI: uri.FromPath(caller),
})
}
sort.SliceStable(callers, func(i, j int) bool {
Expand Down
12 changes: 6 additions & 6 deletions internal/langserver/handlers/command/module_calls.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,16 @@ func (h *CmdHandler) ModuleCallsHandler(ctx context.Context, args cmd.CommandArg
return response, err
}

// declared, err := h.StateStore.Modules.DeclaredModuleCalls(modPath)
// if err != nil {
// return response, err
// }
installed, err := h.StateStore.Roots.InstalledModuleCalls(modPath)
declared, err := h.FeatureReader.DeclaredModuleCalls(modPath)
if err != nil {
return response, err
}
installed, err := h.FeatureReader.InstalledModuleCalls(modPath)
if err != nil {
return response, err
}
moduleCalls := tfmod.ModuleCalls{
// Declared: declared,
Declared: declared,
Installed: installed,
}

Expand Down
54 changes: 28 additions & 26 deletions internal/langserver/handlers/command/module_providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,32 +43,34 @@ func (h *CmdHandler) ModuleProvidersHandler(ctx context.Context, args cmd.Comman
return response, fmt.Errorf("URI %q is not valid", modUri)
}

// modPath, err := uri.PathFromURI(modUri)
// if err != nil {
// return response, err
// }

// mod, _ := h.StateStore.Modules.ModuleByPath(modPath)
// if mod == nil {
// return response, nil
// }

// for provider, version := range mod.Meta.ProviderRequirements {
// docsLink, err := getProviderDocumentationLink(ctx, provider)
// if err != nil {
// return response, err
// }
// response.ProviderRequirements[provider.String()] = providerRequirement{
// DisplayName: provider.ForDisplay(),
// VersionConstraint: version.String(),
// DocsLink: docsLink,
// }
// }

// TODO!
// for provider, version := range mod.InstalledProviders {
// response.InstalledProviders[provider.String()] = version.String()
// }
modPath, err := uri.PathFromURI(modUri)
if err != nil {
return response, err
}

providerRequirements, err := h.FeatureReader.ProviderRequirements(modPath)
if err != nil {
return response, err
}
for provider, version := range providerRequirements {
docsLink, err := getProviderDocumentationLink(ctx, provider)
if err != nil {
return response, err
}
response.ProviderRequirements[provider.String()] = providerRequirement{
DisplayName: provider.ForDisplay(),
VersionConstraint: version.String(),
DocsLink: docsLink,
}
}

installedProviders, err := h.FeatureReader.InstalledProviders(modPath)
if err != nil {
return response, err
}
for provider, version := range installedProviders {
response.InstalledProviders[provider.String()] = version.String()
}

return response, nil
}
Expand Down
20 changes: 11 additions & 9 deletions internal/langserver/handlers/command/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,20 @@ func (h *CmdHandler) TerraformVersionRequestHandler(ctx context.Context, args cm
return response, err
}

mod, _ := h.StateStore.Roots.RootRecordByPath(modPath)
if mod == nil {
return response, nil
progress.Report(ctx, "Recording terraform version info ...")

terraformVersion := h.FeatureReader.TerraformVersion(modPath)
if terraformVersion != nil {
response.DiscoveredVersion = terraformVersion.String()
}

progress.Report(ctx, "Recording terraform version info ...")
if mod.TerraformVersion != nil {
response.DiscoveredVersion = mod.TerraformVersion.String()
coreRequirements, err := h.FeatureReader.CoreRequirements(modPath)
if err != nil {
return response, err
}
if coreRequirements != nil {
response.RequiredVersion = coreRequirements.String()
}
// if mod.Meta.CoreRequirements != nil {
// response.RequiredVersion = mod.Meta.CoreRequirements.String()
// }

progress.Report(ctx, "Sending response ...")

Expand Down
Loading

0 comments on commit f09b92c

Please sign in to comment.