Skip to content

Commit

Permalink
Adding builtin provider running code snip externally
Browse files Browse the repository at this point in the history
Signed-off-by: Shawn Hurley <[email protected]>
  • Loading branch information
shawn-hurley committed Jul 2, 2024
1 parent 269ac50 commit b896017
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 66 deletions.
2 changes: 0 additions & 2 deletions cmd/analyzer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ func AnalysisCmd() *cobra.Command {
builtinConfigs := []provider.InitConfig{}
providerLocations := []string{}
for _, config := range configs {
fmt.Printf("here: %#v\n", config)
config.ContextLines = contextLines
for _, ind := range config.InitConfig {
providerLocations = append(providerLocations, ind.Location)
Expand Down Expand Up @@ -251,7 +250,6 @@ func AnalysisCmd() *cobra.Command {
}

if builtinClient, ok := needProviders["builtin"]; ok {
fmt.Printf("here: %v\n", builtinClient)
if _, err = builtinClient.ProviderInit(ctx, builtinConfigs); err != nil {
errLog.Error(err, "unable to init builtin provider")
os.Exit(1)
Expand Down
4 changes: 2 additions & 2 deletions external-providers/dotnet-external-provider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"os"

"github.com/bombsimon/logrusr/v3"
"github.com/konveyor/analyzer-lsp/provider"
server "github.com/konveyor/analyzer-lsp/provider/server"
"github.com/konveyor/dotnet-external-provider/pkg/dotnet"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -55,7 +55,7 @@ func main() {
secret = *secretKey
}

s := provider.NewServer(client, *port, c, k, secret, log)
s := server.NewServer(client, *port, c, k, secret, 0, log)
ctx := context.TODO()
s.Start(ctx)
}
4 changes: 2 additions & 2 deletions external-providers/generic-external-provider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"os"

"github.com/bombsimon/logrusr/v3"
"github.com/konveyor/analyzer-lsp/provider"
"github.com/konveyor/analyzer-lsp/provider/server"
"github.com/konveyor/generic-external-provider/pkg/generic_external_provider"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -72,7 +72,7 @@ func main() {
secret = *secretKey
}

s := provider.NewServer(client, *port, c, k, secret, log)
s := server.NewServer(client, *port, c, k, secret, 0, log)
ctx := context.TODO()
s.Start(ctx)
}
4 changes: 2 additions & 2 deletions external-providers/yq-external-provider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"os"

"github.com/bombsimon/logrusr/v3"
"github.com/konveyor/analyzer-lsp/provider"
"github.com/konveyor/analyzer-lsp/provider/server"
"github.com/konveyor/yq-external-provider/pkg/yq_provider"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -51,7 +51,7 @@ func main() {
secret = *secretKey
}

s := provider.NewServer(client, *port, c, k, secret, log)
s := server.NewServer(client, *port, c, k, secret, 0, log)
ctx := context.TODO()
s.Start(ctx)
}
59 changes: 57 additions & 2 deletions provider/internal/builtin/provider.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package builtin

import (
"bufio"
"context"
"fmt"
"os"
"strconv"
"strings"

"github.com/go-logr/logr"
"github.com/konveyor/analyzer-lsp/engine"
"github.com/konveyor/analyzer-lsp/provider"
"github.com/konveyor/analyzer-lsp/provider/grpc"
"github.com/swaggest/openapi-go/openapi3"
"go.lsp.dev/uri"
"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -66,7 +71,10 @@ type builtinProvider struct {
tags map[string]bool
provider.UnimplementedDependenciesComponent

clients []provider.ServiceClient
clients []provider.ServiceClient
grpcProviders []engine.CodeSnip

contextLines int
}

func NewBuiltinProvider(config provider.Config, log logr.Logger) *builtinProvider {
Expand Down Expand Up @@ -171,6 +179,8 @@ func (p *builtinProvider) ProviderInit(ctx context.Context, additionalInitConfig
if err != nil {
return nil, err
}
// We know that this should work
p.grpcProviders = append(p.grpcProviders, grpcClient.(engine.CodeSnip))

client, _, err := grpcClient.Init(ctx, p.log, c)
if err != nil {
Expand All @@ -190,10 +200,55 @@ func (p *builtinProvider) ProviderInit(ctx context.Context, additionalInitConfig
}
}

fmt.Printf("%#v", p.clients)
return nil, nil
}

func (p *builtinProvider) GetCodeSnip(u uri.URI, loc engine.Location) (string, error) {
for _, snip := range p.grpcProviders {
s, err := snip.GetCodeSnip(u, loc)
// If we can't find if in any GRPC providers we will try locally
if err != nil {
p.log.Error(err, "unable to get snip")
continue
}
fmt.Printf("here: %v", s)
return s, err
}
if !strings.Contains(string(u), uri.FileScheme) {
return "", fmt.Errorf("invalid file uri")
}
snip, err := p.scanFile(u.Filename(), loc)
if err != nil {
return "", err
}
return snip, nil
}

func (p *builtinProvider) scanFile(path string, loc engine.Location) (string, error) {
readFile, err := os.Open(path)
if err != nil {
p.log.V(5).Error(err, "Unable to read file")
return "", err
}
defer readFile.Close()

scanner := bufio.NewScanner(readFile)
lineNumber := 0
codeSnip := ""
paddingSize := len(strconv.Itoa(loc.EndPosition.Line + p.contextLines))
for scanner.Scan() {
if (lineNumber - p.contextLines) == loc.EndPosition.Line {
codeSnip = codeSnip + fmt.Sprintf("%*d %v", paddingSize, lineNumber+1, scanner.Text())
break
}
if (lineNumber + p.contextLines) >= loc.StartPosition.Line {
codeSnip = codeSnip + fmt.Sprintf("%*d %v\n", paddingSize, lineNumber+1, scanner.Text())
}
lineNumber += 1
}
return codeSnip, nil
}

// We don't need to init anything
func (p *builtinProvider) Init(ctx context.Context, log logr.Logger, config provider.InitConfig) (provider.ServiceClient, provider.InitConfig, error) {
if config.AnalysisMode != provider.AnalysisMode("") {
Expand Down
1 change: 0 additions & 1 deletion provider/internal/builtin/service_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func convertToInterface(s []string) []interface{} {
return n
}
func (p *builtinServiceClient) Evaluate(ctx context.Context, cap string, conditionInfo []byte) (provider.ProviderEvaluateResponse, error) {
fmt.Printf("\nhere: %v\n", cap)
var cond builtinCondition
err := yaml.Unmarshal(conditionInfo, &cond)
if err != nil {
Expand Down
Loading

0 comments on commit b896017

Please sign in to comment.