diff --git a/cli-v2.go b/cli-v2.go index 7eac626..6564869 100644 --- a/cli-v2.go +++ b/cli-v2.go @@ -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")) @@ -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() diff --git a/cmd/analyze.go b/cmd/analyze.go index 1d619dd..758ed0a 100644 --- a/cmd/analyze.go +++ b/cmd/analyze.go @@ -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'") + }, +} diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..3ee262a --- /dev/null +++ b/config/config.go @@ -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() +} \ No newline at end of file