Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 Extract getConfigVolumes to an interface #372

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 28 additions & 137 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ type ProviderInit struct {
// used for failed provider container retry attempts
isRunning bool
containerName string
provider Provider
}

// kantra analyze flags
Expand Down Expand Up @@ -608,28 +609,33 @@ func (a *analyzeCommand) setProviderInitInfo(foundProviders []string) error {
switch prov {
case javaProvider:
a.providersMap[javaProvider] = ProviderInit{
port: port,
image: Settings.JavaProviderImage,
port: port,
image: Settings.JavaProviderImage,
provider: &JavaProvider{},
}
case goProvider:
a.providersMap[goProvider] = ProviderInit{
port: port,
image: Settings.GenericProviderImage,
port: port,
image: Settings.GenericProviderImage,
provider: &GoProvider{},
}
case pythonProvider:
a.providersMap[pythonProvider] = ProviderInit{
port: port,
image: Settings.GenericProviderImage,
port: port,
image: Settings.GenericProviderImage,
provider: &PythonProvider{},
}
case nodeJSProvider:
a.providersMap[nodeJSProvider] = ProviderInit{
port: port,
image: Settings.GenericProviderImage,
port: port,
image: Settings.GenericProviderImage,
provider: &NodeJsProvider{},
}
case dotnetProvider:
a.providersMap[dotnetProvider] = ProviderInit{
port: port,
image: Settings.DotnetProviderImage,
port: port,
image: Settings.DotnetProviderImage,
provider: &DotNetProvider{},
}
}
}
Expand Down Expand Up @@ -795,143 +801,28 @@ func (a *analyzeCommand) getConfigVolumes() (map[string]string, error) {
a.log.V(1).Info("created directory for provider settings", "dir", tempDir)
a.tempDirs = append(a.tempDirs, tempDir)

otherProvsMountPath := SourceMountPath
// when input is a file, it means it's probably a binary
// only java provider can work with binaries, all others
// continue pointing to the directory instead of file
if a.isFileInput {
SourceMountPath = path.Join(SourceMountPath, filepath.Base(a.input))
}

javaConfig := provider.Config{
Name: javaProvider,
Address: fmt.Sprintf("0.0.0.0:%v", a.providersMap[javaProvider].port),
InitConfig: []provider.InitConfig{
{
Location: SourceMountPath,
AnalysisMode: provider.AnalysisMode(a.mode),
ProviderSpecificConfig: map[string]interface{}{
"lspServerName": javaProvider,
"bundles": JavaBundlesLocation,
"depOpenSourceLabelsFile": "/usr/local/etc/maven.default.index",
provider.LspServerPathConfigKey: "/jdtls/bin/jdtls",
},
},
},
}

if a.mavenSettingsFile != "" {
err := copyFileContents(a.mavenSettingsFile, filepath.Join(tempDir, "settings.xml"))
if err != nil {
a.log.V(1).Error(err, "failed copying maven settings file", "path", a.mavenSettingsFile)
return nil, err
}
javaConfig.InitConfig[0].ProviderSpecificConfig["mavenSettingsFile"] = fmt.Sprintf("%s/%s", ConfigMountPath, "settings.xml")
}
if Settings.JvmMaxMem != "" {
javaConfig.InitConfig[0].ProviderSpecificConfig["jvmMaxMem"] = Settings.JvmMaxMem
}

goConfig := provider.Config{
Name: goProvider,
Address: fmt.Sprintf("0.0.0.0:%v", a.providersMap[goProvider].port),
InitConfig: []provider.InitConfig{
{
AnalysisMode: provider.FullAnalysisMode,
ProviderSpecificConfig: map[string]interface{}{
"lspServerName": "generic",
"workspaceFolders": []string{fmt.Sprintf("file://%s", otherProvsMountPath)},
"dependencyProviderPath": "/usr/local/bin/golang-dependency-provider",
provider.LspServerPathConfigKey: "/root/go/bin/gopls",
},
},
},
}

pythonConfig := provider.Config{
Name: pythonProvider,
Address: fmt.Sprintf("0.0.0.0:%v", a.providersMap[pythonProvider].port),
InitConfig: []provider.InitConfig{
{
AnalysisMode: provider.SourceOnlyAnalysisMode,
ProviderSpecificConfig: map[string]interface{}{
"lspServerName": "generic",
"workspaceFolders": []string{fmt.Sprintf("file://%s", otherProvsMountPath)},
provider.LspServerPathConfigKey: "/usr/local/bin/pylsp",
},
},
},
}

nodeJSConfig := provider.Config{
Name: nodeJSProvider,
Address: fmt.Sprintf("0.0.0.0:%v", a.providersMap[nodeJSProvider].port),
InitConfig: []provider.InitConfig{
{
AnalysisMode: provider.SourceOnlyAnalysisMode,
ProviderSpecificConfig: map[string]interface{}{
"lspServerName": "nodejs",
"workspaceFolders": []string{fmt.Sprintf("file://%s", otherProvsMountPath)},
provider.LspServerPathConfigKey: "/usr/local/bin/typescript-language-server",
},
},
},
}

dotnetConfig := provider.Config{
Name: dotnetProvider,
Address: fmt.Sprintf("0.0.0.0:%v", a.providersMap[dotnetProvider].port),
InitConfig: []provider.InitConfig{
{
Location: SourceMountPath,
AnalysisMode: provider.SourceOnlyAnalysisMode,
ProviderSpecificConfig: map[string]interface{}{
provider.LspServerPathConfigKey: "/opt/app-root/.dotnet/tools/csharp-ls",
},
},
},
}

provConfig := []provider.Config{
{
Name: "builtin",
InitConfig: []provider.InitConfig{
{
Location: otherProvsMountPath,
AnalysisMode: provider.AnalysisMode(a.mode),
},
},
},
}
var provConfig []provider.Config
var builtinProvider = BuiltinProvider{}
var config, _ = builtinProvider.GetConfigVolume(a, tempDir)
provConfig = append(provConfig, config)

settingsVols := map[string]string{
tempDir: ConfigMountPath,
}
if !a.needsBuiltin {
vols, dependencyFolders := a.getDepsFolders()
vols, _ := a.getDepsFolders()
if len(vols) != 0 {
maps.Copy(settingsVols, vols)
}
for prov := range a.providersMap {
switch prov {
case javaProvider:
provConfig = append(provConfig, javaConfig)
case goProvider:
provConfig = append(provConfig, goConfig)
case pythonProvider:
if len(dependencyFolders) != 0 {
pythonConfig.InitConfig[0].ProviderSpecificConfig["dependencyFolders"] = dependencyFolders
}
provConfig = append(provConfig, pythonConfig)
case nodeJSProvider:
if len(dependencyFolders) != 0 {
nodeJSConfig.InitConfig[0].ProviderSpecificConfig["dependencyFolders"] = dependencyFolders
}
provConfig = append(provConfig, nodeJSConfig)
case dotnetProvider:
provConfig = append(provConfig, dotnetConfig)
for _, provInfo := range a.providersMap {
var volConfig, err = provInfo.provider.GetConfigVolume(a, tempDir)
if err != nil {
a.log.V(1).Error(err, "failed creating volume configs")
return nil, err
}
provConfig = append(provConfig, volConfig)
}

// Set proxy to providers
if a.httpProxy != "" || a.httpsProxy != "" {
proxy := provider.Proxy{
Expand Down
22 changes: 22 additions & 0 deletions cmd/builtin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cmd

import (
"github.com/konveyor/analyzer-lsp/provider"
)

type BuiltinProvider struct {
config provider.Config
}

func (p *BuiltinProvider) GetConfigVolume(a *analyzeCommand, tmpDir string) (provider.Config, error) {
p.config = provider.Config{
Name: "builtin",
InitConfig: []provider.InitConfig{
{
Location: SourceMountPath,
AnalysisMode: provider.AnalysisMode(a.mode),
},
},
}
return p.config, nil
}
27 changes: 27 additions & 0 deletions cmd/dotnet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cmd

import (
"fmt"
"github.com/konveyor/analyzer-lsp/provider"
)

type DotNetProvider struct {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about moving the provider files into a folder such as providers?

Copy link
Author

@abrugaro abrugaro Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My initial idea was to place the provider structs in a providers folder. However, the analyzeCommand does not export any parameters, and I didn't want to change that in this PR since I don't have enough context on the code.

Should I change the AnalyzeCommand to export the necessary properties?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There could be a pkg/providers dir, and export necessary params from the analyze cmd. I think it would be good if it could further be refactored so that the providers were agnostic to the analyze command. This would require a bit more work though, as some of the analyze cmd fields would need to be copied into a map, or similar, to pass to the providers (mostly the Java provider). But, I do think this is a good start to that, so am happy to merge and address these things later.

config provider.Config
}

func (p *DotNetProvider) GetConfigVolume(a *analyzeCommand, tmpDir string) (provider.Config, error) {
p.config = provider.Config{
Name: dotnetProvider,
Address: fmt.Sprintf("0.0.0.0:%v", a.providersMap[dotnetProvider].port),
InitConfig: []provider.InitConfig{
{
Location: SourceMountPath,
AnalysisMode: provider.SourceOnlyAnalysisMode,
ProviderSpecificConfig: map[string]interface{}{
provider.LspServerPathConfigKey: "/opt/app-root/.dotnet/tools/csharp-ls",
},
},
},
}
return p.config, nil
}
29 changes: 29 additions & 0 deletions cmd/go.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cmd

import (
"fmt"
"github.com/konveyor/analyzer-lsp/provider"
)

type GoProvider struct {
config provider.Config
}

func (p *GoProvider) GetConfigVolume(a *analyzeCommand, tmpDir string) (provider.Config, error) {
p.config = provider.Config{
Name: goProvider,
Address: fmt.Sprintf("0.0.0.0:%v", a.providersMap[goProvider].port),
InitConfig: []provider.InitConfig{
{
AnalysisMode: provider.FullAnalysisMode,
ProviderSpecificConfig: map[string]interface{}{
"lspServerName": "generic",
"workspaceFolders": []string{fmt.Sprintf("file://%s", SourceMountPath)},
"dependencyProviderPath": "/usr/local/bin/golang-dependency-provider",
provider.LspServerPathConfigKey: "/root/go/bin/gopls",
},
},
},
}
return p.config, nil
}
54 changes: 54 additions & 0 deletions cmd/java.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package cmd

import (
"fmt"
"github.com/konveyor/analyzer-lsp/provider"
"path"
"path/filepath"
)

type JavaProvider struct {
config provider.Config
}

func (p *JavaProvider) GetConfigVolume(a *analyzeCommand, tmpDir string) (provider.Config, error) {

var mountPath = SourceMountPath
// when input is a file, it means it's probably a binary
// only java provider can work with binaries, all others
// continue pointing to the directory instead of file
if a.isFileInput {
mountPath = path.Join(SourceMountPath, filepath.Base(a.input))
}

p.config = provider.Config{
Name: javaProvider,
Address: fmt.Sprintf("0.0.0.0:%v", a.providersMap[javaProvider].port),
InitConfig: []provider.InitConfig{
{
Location: mountPath,
AnalysisMode: provider.AnalysisMode(a.mode),
ProviderSpecificConfig: map[string]interface{}{
"lspServerName": javaProvider,
"bundles": JavaBundlesLocation,
"depOpenSourceLabelsFile": "/usr/local/etc/maven.default.index",
provider.LspServerPathConfigKey: "/jdtls/bin/jdtls",
},
},
},
}

if a.mavenSettingsFile != "" {
err := copyFileContents(a.mavenSettingsFile, filepath.Join(tmpDir, "settings.xml"))
if err != nil {
a.log.V(1).Error(err, "failed copying maven settings file", "path", a.mavenSettingsFile)
return provider.Config{}, err
}
p.config.InitConfig[0].ProviderSpecificConfig["mavenSettingsFile"] = fmt.Sprintf("%s/%s", ConfigMountPath, "settings.xml")
}
if Settings.JvmMaxMem != "" {
p.config.InitConfig[0].ProviderSpecificConfig["jvmMaxMem"] = Settings.JvmMaxMem
}

return p.config, nil
}
32 changes: 32 additions & 0 deletions cmd/nodejs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cmd

import (
"fmt"
"github.com/konveyor/analyzer-lsp/provider"
)

type NodeJsProvider struct {
config provider.Config
}

func (p *NodeJsProvider) GetConfigVolume(a *analyzeCommand, tmpDir string) (provider.Config, error) {
p.config = provider.Config{
Name: nodeJSProvider,
Address: fmt.Sprintf("0.0.0.0:%v", a.providersMap[nodeJSProvider].port),
InitConfig: []provider.InitConfig{
{
AnalysisMode: provider.SourceOnlyAnalysisMode,
ProviderSpecificConfig: map[string]interface{}{
"lspServerName": "nodejs",
"workspaceFolders": []string{fmt.Sprintf("file://%s", SourceMountPath)},
provider.LspServerPathConfigKey: "/usr/local/bin/typescript-language-server",
},
},
},
}
_, dependencyFolders := a.getDepsFolders()
if len(dependencyFolders) != 0 {
p.config.InitConfig[0].ProviderSpecificConfig["dependencyFolders"] = dependencyFolders
}
return p.config, nil
}
7 changes: 7 additions & 0 deletions cmd/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package cmd

import "github.com/konveyor/analyzer-lsp/provider"

type Provider interface {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be extended to include other methods. In particular, setProviders

GetConfigVolume(a *analyzeCommand, tmpDir string) (provider.Config, error)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could probably remove the return err here - I don't notice it being used. We can always add it back later if needed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The java provider can throw an error when copying the maven settings file to the temp dir

}
Loading
Loading