Skip to content

Commit

Permalink
Clean up command handler
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanck committed Apr 29, 2024
1 parent 3a272ab commit dcea7ba
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 45 deletions.
26 changes: 8 additions & 18 deletions internal/langserver/handlers/command/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion internal/langserver/handlers/command/module_callers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions internal/langserver/handlers/command/module_calls.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions internal/langserver/handlers/command/module_providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions internal/langserver/handlers/command/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
7 changes: 4 additions & 3 deletions internal/langserver/handlers/execute_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
17 changes: 0 additions & 17 deletions internal/langserver/handlers/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit dcea7ba

Please sign in to comment.