From 0c5e3d6c01c029c87e0bac1e8fe37536e4ec35f8 Mon Sep 17 00:00:00 2001 From: Daniel Banck Date: Thu, 25 Apr 2024 10:01:54 +0200 Subject: [PATCH] Clean up command handler --- .../langserver/handlers/command/handler.go | 26 ++++++------------- .../handlers/command/module_callers.go | 2 +- .../handlers/command/module_calls.go | 4 +-- .../handlers/command/module_providers.go | 4 +-- .../langserver/handlers/command/terraform.go | 4 +-- .../langserver/handlers/execute_command.go | 7 ++--- internal/langserver/handlers/service.go | 17 ------------ 7 files changed, 19 insertions(+), 45 deletions(-) diff --git a/internal/langserver/handlers/command/handler.go b/internal/langserver/handlers/command/handler.go index 9ead89dff..0bf088df3 100644 --- a/internal/langserver/handlers/command/handler.go +++ b/internal/langserver/handlers/command/handler.go @@ -6,26 +6,16 @@ package command import ( "log" - "github.com/hashicorp/go-version" + fmodules "github.com/hashicorp/terraform-ls/internal/features/modules" + frootmodules "github.com/hashicorp/terraform-ls/internal/features/rootmodules" "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 - FeatureReader FeatureReader + StateStore *state.StateStore + Logger *log.Logger + // TODO? Can features contribute commands, so we don't have to import + // the features here? + ModulesFeature *fmodules.ModulesFeature + RootModulesFeature *frootmodules.RootModulesFeature } diff --git a/internal/langserver/handlers/command/module_callers.go b/internal/langserver/handlers/command/module_callers.go index c399640cb..3aab686a9 100644 --- a/internal/langserver/handlers/command/module_callers.go +++ b/internal/langserver/handlers/command/module_callers.go @@ -39,7 +39,7 @@ func (h *CmdHandler) ModuleCallersHandler(ctx context.Context, args cmd.CommandA return nil, err } - modCallers, err := h.FeatureReader.CallersOfModule(modPath) + modCallers, err := h.RootModulesFeature.CallersOfModule(modPath) if err != nil { return nil, err } diff --git a/internal/langserver/handlers/command/module_calls.go b/internal/langserver/handlers/command/module_calls.go index 2ce6a2b64..d0d218368 100644 --- a/internal/langserver/handlers/command/module_calls.go +++ b/internal/langserver/handlers/command/module_calls.go @@ -62,11 +62,11 @@ func (h *CmdHandler) ModuleCallsHandler(ctx context.Context, args cmd.CommandArg return response, err } - declared, err := h.FeatureReader.DeclaredModuleCalls(modPath) + declared, err := h.ModulesFeature.DeclaredModuleCalls(modPath) if err != nil { return response, err } - installed, err := h.FeatureReader.InstalledModuleCalls(modPath) + installed, err := h.RootModulesFeature.InstalledModuleCalls(modPath) if err != nil { return response, err } diff --git a/internal/langserver/handlers/command/module_providers.go b/internal/langserver/handlers/command/module_providers.go index e3e3beca5..183b555b1 100644 --- a/internal/langserver/handlers/command/module_providers.go +++ b/internal/langserver/handlers/command/module_providers.go @@ -48,7 +48,7 @@ func (h *CmdHandler) ModuleProvidersHandler(ctx context.Context, args cmd.Comman return response, err } - providerRequirements, err := h.FeatureReader.ProviderRequirements(modPath) + providerRequirements, err := h.ModulesFeature.ProviderRequirements(modPath) if err != nil { return response, err } @@ -64,7 +64,7 @@ func (h *CmdHandler) ModuleProvidersHandler(ctx context.Context, args cmd.Comman } } - installedProviders, err := h.FeatureReader.InstalledProviders(modPath) + installedProviders, err := h.RootModulesFeature.InstalledProviders(modPath) if err != nil { return response, err } diff --git a/internal/langserver/handlers/command/terraform.go b/internal/langserver/handlers/command/terraform.go index 0b6528b19..ae7b326e2 100644 --- a/internal/langserver/handlers/command/terraform.go +++ b/internal/langserver/handlers/command/terraform.go @@ -48,12 +48,12 @@ func (h *CmdHandler) TerraformVersionRequestHandler(ctx context.Context, args cm progress.Report(ctx, "Recording terraform version info ...") - terraformVersion := h.FeatureReader.TerraformVersion(modPath) + terraformVersion := h.RootModulesFeature.TerraformVersion(modPath) if terraformVersion != nil { response.DiscoveredVersion = terraformVersion.String() } - coreRequirements, err := h.FeatureReader.CoreRequirements(modPath) + coreRequirements, err := h.ModulesFeature.CoreRequirements(modPath) if err != nil { return response, err } diff --git a/internal/langserver/handlers/execute_command.go b/internal/langserver/handlers/execute_command.go index 5fd6850a8..ff9d1bb2a 100644 --- a/internal/langserver/handlers/execute_command.go +++ b/internal/langserver/handlers/execute_command.go @@ -16,9 +16,10 @@ import ( func cmdHandlers(svc *service) cmd.Handlers { cmdHandler := &command.CmdHandler{ - StateStore: svc.stateStore, - Logger: svc.logger, - FeatureReader: svc.features.API, + StateStore: svc.stateStore, + Logger: svc.logger, + ModulesFeature: svc.features.Modules, + RootModulesFeature: svc.features.RootModules, } return cmd.Handlers{ cmd.Name("rootmodules"): removedHandler("use module.callers instead"), diff --git a/internal/langserver/handlers/service.go b/internal/langserver/handlers/service.go index 55df0df6d..c152d094b 100644 --- a/internal/langserver/handlers/service.go +++ b/internal/langserver/handlers/service.go @@ -50,15 +50,6 @@ type Features struct { RootModules *frootmodules.RootModulesFeature Stacks *fstacks.StacksFeature Variables *fvariables.VariablesFeature - - API FeaturesAPI -} - -type FeaturesAPI struct { - *fmodules.ModulesFeature - *frootmodules.RootModulesFeature - *fstacks.StacksFeature - *fvariables.VariablesFeature } type service struct { @@ -587,14 +578,6 @@ func (svc *service) configureSessionDependencies(ctx context.Context, cfgOpts *s RootModules: rootModulesFeature, Stacks: stacksFeature, Variables: variablesFeature, - - // WIP - API: FeaturesAPI{ - ModulesFeature: modulesFeature, - RootModulesFeature: rootModulesFeature, - StacksFeature: stacksFeature, - VariablesFeature: variablesFeature, - }, } svc.decoder = decoder.NewDecoder(&idecoder.GlobalPathReader{