Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
174 changes: 174 additions & 0 deletions cmd/configsetup/codacy_yaml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package configsetup

Check notice on line 1 in cmd/configsetup/codacy_yaml.go

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

cmd/configsetup/codacy_yaml.go#L1

should have a package comment

import (
"fmt"
"log"
"sort"
"strings"

"codacy/cli-v2/domain"
"codacy/cli-v2/plugins"
)

// RuntimePluginConfig holds the structure of the runtime plugin.yaml file
type RuntimePluginConfig struct {
Name string `yaml:"name"`
Description string `yaml:"description"`
DefaultVersion string `yaml:"default_version"`
}

func ConfigFileTemplate(tools []domain.Tool) string {

Check notice on line 20 in cmd/configsetup/codacy_yaml.go

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

cmd/configsetup/codacy_yaml.go#L20

exported function ConfigFileTemplate should have comment or be unexported
toolsMap := make(map[string]bool)
toolVersions := make(map[string]string)
neededRuntimes := make(map[string]bool)

toolsWithLatestVersion, _, _ := KeepToolsWithLatestVersion(tools)

// Get versions and runtime dependencies
defaultVersions := plugins.GetToolVersions()
runtimeVersions := plugins.GetRuntimeVersions()
runtimeDependencies := plugins.GetToolRuntimeDependencies()

// Process enabled tools
for _, tool := range toolsWithLatestVersion {
toolsMap[tool.Uuid] = true
toolVersions[tool.Uuid] = getToolVersion(tool, defaultVersions)
addRequiredRuntime(tool.Uuid, neededRuntimes, runtimeDependencies)
}

var sb strings.Builder

// Build runtimes section
buildRuntimesSection(&sb, tools, neededRuntimes, runtimeVersions, runtimeDependencies)

// Build tools section
buildToolsSection(&sb, tools, toolsMap, toolVersions, defaultVersions)

return sb.String()
}

// getToolVersion returns the version for a tool, preferring tool.Version over default
func getToolVersion(tool domain.Tool, defaultVersions map[string]string) string {
if tool.Version != "" {
return tool.Version
}
if meta, ok := domain.SupportedToolsMetadata[tool.Uuid]; ok {
if defaultVersion, ok := defaultVersions[meta.Name]; ok {
return defaultVersion
}
}
return ""
}

// addRequiredRuntime adds the runtime requirement for a tool
func addRequiredRuntime(toolUuid string, neededRuntimes map[string]bool, runtimeDependencies map[string]string) {

Check notice on line 64 in cmd/configsetup/codacy_yaml.go

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

cmd/configsetup/codacy_yaml.go#L64

func parameter toolUuid should be toolUUID
if meta, ok := domain.SupportedToolsMetadata[toolUuid]; ok {
if runtime, ok := runtimeDependencies[meta.Name]; ok {
if meta.Name == "dartanalyzer" {
// For dartanalyzer, default to dart runtime
neededRuntimes["dart"] = true
} else {
neededRuntimes[runtime] = true
}
}
}
}

// buildRuntimesSection builds the runtimes section of the configuration
func buildRuntimesSection(sb *strings.Builder, tools []domain.Tool, neededRuntimes map[string]bool, runtimeVersions map[string]string, runtimeDependencies map[string]string) {
sb.WriteString("runtimes:\n")

if len(tools) == 0 {
// In local mode with no tools specified, include all necessary runtimes
addAllSupportedRuntimes(neededRuntimes, runtimeDependencies)
}

writeRuntimesList(sb, neededRuntimes, runtimeVersions)
}

// addAllSupportedRuntimes adds all runtimes needed by supported tools
func addAllSupportedRuntimes(neededRuntimes map[string]bool, runtimeDependencies map[string]string) {
supportedTools, err := plugins.GetSupportedTools()
if err != nil {
log.Printf("Warning: failed to get supported tools: %v", err)
return
}

for toolName := range supportedTools {
if runtime, ok := runtimeDependencies[toolName]; ok {
if toolName == "dartanalyzer" {
neededRuntimes["dart"] = true
} else {
neededRuntimes[runtime] = true
}
}
}
}

// writeRuntimesList writes the sorted runtimes list to the string builder
func writeRuntimesList(sb *strings.Builder, neededRuntimes map[string]bool, runtimeVersions map[string]string) {
var sortedRuntimes []string
for runtime := range neededRuntimes {
sortedRuntimes = append(sortedRuntimes, runtime)
}
sort.Strings(sortedRuntimes)

for _, runtime := range sortedRuntimes {
sb.WriteString(fmt.Sprintf(" - %s@%s\n", runtime, runtimeVersions[runtime]))
}
}

// buildToolsSection builds the tools section of the configuration
func buildToolsSection(sb *strings.Builder, tools []domain.Tool, toolsMap map[string]bool, toolVersions map[string]string, defaultVersions map[string]string) {
sb.WriteString("tools:\n")

if len(tools) > 0 {
writeEnabledTools(sb, toolsMap, toolVersions)
} else {
writeAllSupportedTools(sb, defaultVersions)
}
}

// writeEnabledTools writes the enabled tools to the string builder
func writeEnabledTools(sb *strings.Builder, toolsMap map[string]bool, toolVersions map[string]string) {
var sortedTools []string
for uuid, meta := range domain.SupportedToolsMetadata {
if toolsMap[uuid] {
sortedTools = append(sortedTools, meta.Name)
}
}
sort.Strings(sortedTools)

for _, name := range sortedTools {
for uuid, meta := range domain.SupportedToolsMetadata {
if meta.Name == name && toolsMap[uuid] {
version := toolVersions[uuid]
sb.WriteString(fmt.Sprintf(" - %s@%s\n", name, version))
break
}
}
}
}

// writeAllSupportedTools writes all supported tools to the string builder
func writeAllSupportedTools(sb *strings.Builder, defaultVersions map[string]string) {
supportedTools, err := plugins.GetSupportedTools()
if err != nil {
log.Printf("Warning: failed to get supported tools: %v", err)
return
}

var sortedTools []string
for toolName := range supportedTools {
if version, ok := defaultVersions[toolName]; ok && version != "" {
sortedTools = append(sortedTools, toolName)
}
}
sort.Strings(sortedTools)

for _, toolName := range sortedTools {
if version, ok := defaultVersions[toolName]; ok {
sb.WriteString(fmt.Sprintf(" - %s@%s\n", toolName, version))
}
}
}
65 changes: 65 additions & 0 deletions cmd/configsetup/config_creators.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package configsetup

Check notice on line 1 in cmd/configsetup/config_creators.go

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

cmd/configsetup/config_creators.go#L1

should have a package comment

import (
"fmt"
"path/filepath"

"codacy/cli-v2/config"
"codacy/cli-v2/constants"
"codacy/cli-v2/domain"
"codacy/cli-v2/tools"

"gopkg.in/yaml.v3"
)

func CreateLanguagesConfigFileLocal(toolsConfigDir string) error {

Check notice on line 15 in cmd/configsetup/config_creators.go

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

cmd/configsetup/config_creators.go#L15

exported function CreateLanguagesConfigFileLocal should have comment or be unexported
// Build tool language configurations from API
configTools, err := tools.BuildLanguagesConfigFromAPI()
if err != nil {
return fmt.Errorf("failed to build languages config from API: %w", err)
}

// Create the config structure
config := domain.LanguagesConfig{
Tools: configTools,
}

// Marshal to YAML
data, err := yaml.Marshal(config)
if err != nil {
return fmt.Errorf("failed to marshal languages config to YAML: %w", err)
}

return writeConfigFile(filepath.Join(toolsConfigDir, constants.LanguagesConfigFileName), data)
}

func CreateGitIgnoreFile() error {

Check notice on line 36 in cmd/configsetup/config_creators.go

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

cmd/configsetup/config_creators.go#L36

exported function CreateGitIgnoreFile should have comment or be unexported
gitIgnorePath := filepath.Join(config.Config.LocalCodacyDirectory(), constants.GitIgnoreFileName)
content := "# Codacy CLI\ntools-configs/\n.gitignore\ncli-config.yaml\nlogs/\n"
return writeConfigFile(gitIgnorePath, []byte(content))
}

func CreateConfigurationFiles(tools []domain.Tool, cliLocalMode bool, flags domain.InitFlags) error {

Check notice on line 42 in cmd/configsetup/config_creators.go

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

cmd/configsetup/config_creators.go#L42

exported function CreateConfigurationFiles should have comment or be unexported
// Create project config file
configContent := ConfigFileTemplate(tools)
if err := writeConfigFile(config.Config.ProjectConfigFile(), []byte(configContent)); err != nil {
return fmt.Errorf("failed to write project config file: %w", err)
}

// Create CLI config file
cliConfigContent := buildCliConfigContent(cliLocalMode, flags)
if err := writeConfigFile(config.Config.CliConfigFile(), []byte(cliConfigContent)); err != nil {
return fmt.Errorf("failed to write CLI config file: %w", err)
}

return nil
}

// buildCliConfigContent creates the CLI configuration content
func buildCliConfigContent(cliLocalMode bool, initFlags domain.InitFlags) string {
if cliLocalMode {
return fmt.Sprintf("mode: local")
} else {

Check notice on line 62 in cmd/configsetup/config_creators.go

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

cmd/configsetup/config_creators.go#L62

if block ends with a return statement, so drop this else and outdent its block
return fmt.Sprintf("mode: remote\nprovider: %s\norganization: %s\nrepository: %s", initFlags.Provider, initFlags.Organization, initFlags.Repository)
}
}
Loading
Loading