Skip to content

Commit

Permalink
breaking/wip
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanck committed Aug 8, 2022
1 parent 67d0f41 commit 8897ba4
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 19 deletions.
2 changes: 2 additions & 0 deletions internal/hooks/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
package hooks

import (
"github.com/algolia/algoliasearch-client-go/v3/algolia/search"
"github.com/hashicorp/terraform-ls/internal/registry"
"github.com/hashicorp/terraform-ls/internal/state"
)

type Hooks struct {
ModStore *state.ModuleStore
RegistryClient registry.Client
AlgoliaClient *search.Client
}
22 changes: 7 additions & 15 deletions internal/hooks/module_source_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"strings"

"github.com/algolia/algoliasearch-client-go/v3/algolia/opt"
"github.com/algolia/algoliasearch-client-go/v3/algolia/search"
"github.com/hashicorp/hcl-lang/decoder"
"github.com/hashicorp/hcl-lang/lang"
"github.com/zclconf/go-cty/cty"
Expand All @@ -16,21 +15,10 @@ type RegistryModule struct {
Description string `json:"description"`
}

// Set these using -ldflags
// -X 'github.com/hashicorp/terraform-ls/internal/hooks.AlgoliaAppID=ID'
// -X 'github.com/hashicorp/terraform-ls/internal/hooks.AlgoliaAPIKey=Key'
var AlgoliaAppID = ""
var AlgoliaAPIKey = ""

func fetchModulesFromAlgolia(ctx context.Context, term string) ([]RegistryModule, error) {
func (h *Hooks) fetchModulesFromAlgolia(ctx context.Context, term string) ([]RegistryModule, error) {
modules := make([]RegistryModule, 0)

if AlgoliaAppID == "" || AlgoliaAPIKey == "" {
return modules, nil
}

client := search.NewClient(AlgoliaAppID, AlgoliaAPIKey)
index := client.InitIndex("tf-registry:prod:modules")
index := h.AlgoliaClient.InitIndex("tf-registry:prod:modules")
params := []interface{}{
ctx, // transport.Request will magically extract the context from here
opt.AttributesToRetrieve("full-name", "description"),
Expand Down Expand Up @@ -60,7 +48,11 @@ func (h *Hooks) RegistryModuleSources(ctx context.Context, value cty.Value) ([]d
return candidates, nil
}

modules, err := fetchModulesFromAlgolia(ctx, prefix)
if h.AlgoliaClient == nil {
return candidates, nil
}

modules, err := h.fetchModulesFromAlgolia(ctx, prefix)
path, ok := decoder.PathFromContext(ctx)
if err != nil || !ok {
return candidates, nil
Expand Down
66 changes: 66 additions & 0 deletions internal/hooks/module_source_registry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package hooks

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"

"github.com/hashicorp/hcl-lang/decoder"
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/terraform-ls/internal/registry"
"github.com/hashicorp/terraform-ls/internal/state"
"github.com/zclconf/go-cty/cty"
)

func TestHooks_RegistryModuleSources(t *testing.T) {
ctx := context.Background()
tmpDir := t.TempDir()

ctx = decoder.WithPath(ctx, lang.Path{
Path: tmpDir,
LanguageID: "terraform",
})
s, err := state.NewStateStore()
if err != nil {
t.Fatal(err)
}

regClient := registry.NewClient()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, fmt.Sprintf("unexpected request: %q", r.RequestURI), 400)
}))
regClient.BaseURL = srv.URL
t.Cleanup(srv.Close)

h := &Hooks{
ModStore: s.Modules,
RegistryClient: regClient,
// AlgoliaClient: ,
}

tests := []struct {
name string
value cty.Value
want []decoder.Candidate
wantErr bool
}{
// TODO: Add test cases.
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := h.RegistryModuleSources(ctx, tt.value)

if (err != nil) != tt.wantErr {
t.Errorf("Hooks.RegistryModuleSources() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Hooks.RegistryModuleSources() = %v, want %v", got, tt.want)
}
})
}
}
16 changes: 12 additions & 4 deletions internal/langserver/handlers/completion_hooks.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
package handlers

import (
"github.com/algolia/algoliasearch-client-go/v3/algolia/search"
"github.com/hashicorp/hcl-lang/decoder"
"github.com/hashicorp/terraform-ls/internal/hooks"
)

func (s *service) AppendCompletionHooks(ctx decoder.DecoderContext) {
var AlgoliaAppID = ""
var AlgoliaAPIKey = ""

func (s *service) AppendCompletionHooks(decoderContext decoder.DecoderContext) {
h := hooks.Hooks{
ModStore: s.modStore,
RegistryClient: s.registryClient,
}

ctx.CompletionHooks["CompleteLocalModuleSources"] = h.LocalModuleSources
ctx.CompletionHooks["CompleteRegistryModuleSources"] = h.RegistryModuleSources
ctx.CompletionHooks["CompleteRegistryModuleVersions"] = h.RegistryModuleVersions
if AlgoliaAppID != "" && AlgoliaAPIKey != "" {
h.AlgoliaClient = search.NewClient(AlgoliaAppID, AlgoliaAPIKey)
}

decoderContext.CompletionHooks["CompleteLocalModuleSources"] = h.LocalModuleSources
decoderContext.CompletionHooks["CompleteRegistryModuleSources"] = h.RegistryModuleSources
decoderContext.CompletionHooks["CompleteRegistryModuleVersions"] = h.RegistryModuleVersions
}

0 comments on commit 8897ba4

Please sign in to comment.