Skip to content

Commit

Permalink
Merge pull request #4 from codacy/global-config
Browse files Browse the repository at this point in the history
Mostly global config object thing
  • Loading branch information
machadoit authored May 21, 2024
2 parents 394653b + e7c25c1 commit 4a27ea4
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 22 deletions.
26 changes: 4 additions & 22 deletions cli-v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,27 +205,9 @@ func fetchTools(runtime *config.Runtime, runtimesDirectory string, toolsDirector
}
}

func main() {
homePath, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}

codacyDirectory := filepath.Join(homePath, ".cache", "codacy")
runtimesDirectory := filepath.Join(codacyDirectory, "runtimes")
toolsDirectory := filepath.Join(codacyDirectory, "tools")
fmt.Println("creating: " + codacyDirectory)
if os.MkdirAll(codacyDirectory, 0777) != nil {
log.Fatal(err)
}
fmt.Println("creating: " + runtimesDirectory)
if os.MkdirAll(runtimesDirectory, 0777) != nil {
log.Fatal(err)
}
fmt.Println("creating: " + toolsDirectory)
if os.MkdirAll(toolsDirectory, 0777) != nil {
log.Fatal(err)
}
func main() {
config.Init()

// TODO can use a variable to stored the "local" codacy dir
runtimes, configErr := config.ReadConfigFile(filepath.Join(".codacy", "codacy.yaml"))
Expand All @@ -234,9 +216,9 @@ func main() {
}

// install runtimes
fetchRuntimes(runtimes, runtimesDirectory)
fetchRuntimes(runtimes, config.Config.RuntimesDirectory())
for _, r := range runtimes {
fetchTools(r, runtimesDirectory, toolsDirectory)
fetchTools(r, config.Config.RuntimesDirectory(), config.Config.ToolsDirectory())
}

cmd.Execute()
Expand Down
18 changes: 18 additions & 0 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
package cmd

import (
"fmt"
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(analyzeCmd)
}

var analyzeCmd = &cobra.Command{
Use: "analyze",
Short: "Runs all linters.",
Long: "Runs all tools for all runtimes.",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Hello from 'analyze'")
},
}
61 changes: 61 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package config

import (
"log"
"os"
"path/filepath"
)

type configType struct {
homePath string
codacyDirectory string
runtimesDirectory string
toolsDirectory string
}

var Config = configType{}

func (c *configType) HomePath() string {
return c.homePath
}

func (c *configType) CodacyDirectory() string {
return c.codacyDirectory
}

func (c *configType) RuntimesDirectory() string {
return c.runtimesDirectory
}

func (c *configType) ToolsDirectory() string {
return c.toolsDirectory
}

func (c *configType) initCodacyDirs() {
c.codacyDirectory = filepath.Join(c.homePath, ".cache", "codacy")
c.runtimesDirectory = filepath.Join(c.codacyDirectory, "runtimes")
c.toolsDirectory = filepath.Join(c.codacyDirectory, "tools")

err := os.MkdirAll(c.codacyDirectory, 0777)
if err != nil {
log.Fatal(err)
}
err = os.MkdirAll(c.runtimesDirectory, 0777)
if err != nil {
log.Fatal(err)
}
err = os.MkdirAll(c.toolsDirectory, 0777)
if err != nil {
log.Fatal(err)
}
}

func Init() {
homePath, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}
Config.homePath = homePath

Config.initCodacyDirs()
}

0 comments on commit 4a27ea4

Please sign in to comment.