Skip to content

Commit e7c25c1

Browse files
Mostly global config object thing
1 parent 1763280 commit e7c25c1

File tree

3 files changed

+83
-22
lines changed

3 files changed

+83
-22
lines changed

cli-v2.go

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -205,27 +205,9 @@ func fetchTools(runtime *config.Runtime, runtimesDirectory string, toolsDirector
205205
}
206206
}
207207

208-
func main() {
209-
homePath, err := os.UserHomeDir()
210-
if err != nil {
211-
log.Fatal(err)
212-
}
213208

214-
codacyDirectory := filepath.Join(homePath, ".cache", "codacy")
215-
runtimesDirectory := filepath.Join(codacyDirectory, "runtimes")
216-
toolsDirectory := filepath.Join(codacyDirectory, "tools")
217-
fmt.Println("creating: " + codacyDirectory)
218-
if os.MkdirAll(codacyDirectory, 0777) != nil {
219-
log.Fatal(err)
220-
}
221-
fmt.Println("creating: " + runtimesDirectory)
222-
if os.MkdirAll(runtimesDirectory, 0777) != nil {
223-
log.Fatal(err)
224-
}
225-
fmt.Println("creating: " + toolsDirectory)
226-
if os.MkdirAll(toolsDirectory, 0777) != nil {
227-
log.Fatal(err)
228-
}
209+
func main() {
210+
config.Init()
229211

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

236218
// install runtimes
237-
fetchRuntimes(runtimes, runtimesDirectory)
219+
fetchRuntimes(runtimes, config.Config.RuntimesDirectory())
238220
for _, r := range runtimes {
239-
fetchTools(r, runtimesDirectory, toolsDirectory)
221+
fetchTools(r, config.Config.RuntimesDirectory(), config.Config.ToolsDirectory())
240222
}
241223

242224
cmd.Execute()

cmd/analyze.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,19 @@
11
package cmd
2+
3+
import (
4+
"fmt"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
func init() {
9+
rootCmd.AddCommand(analyzeCmd)
10+
}
11+
12+
var analyzeCmd = &cobra.Command{
13+
Use: "analyze",
14+
Short: "Runs all linters.",
15+
Long: "Runs all tools for all runtimes.",
16+
Run: func(cmd *cobra.Command, args []string) {
17+
fmt.Println("Hello from 'analyze'")
18+
},
19+
}

config/config.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package config
2+
3+
import (
4+
"log"
5+
"os"
6+
"path/filepath"
7+
)
8+
9+
type configType struct {
10+
homePath string
11+
codacyDirectory string
12+
runtimesDirectory string
13+
toolsDirectory string
14+
}
15+
16+
var Config = configType{}
17+
18+
func (c *configType) HomePath() string {
19+
return c.homePath
20+
}
21+
22+
func (c *configType) CodacyDirectory() string {
23+
return c.codacyDirectory
24+
}
25+
26+
func (c *configType) RuntimesDirectory() string {
27+
return c.runtimesDirectory
28+
}
29+
30+
func (c *configType) ToolsDirectory() string {
31+
return c.toolsDirectory
32+
}
33+
34+
func (c *configType) initCodacyDirs() {
35+
c.codacyDirectory = filepath.Join(c.homePath, ".cache", "codacy")
36+
c.runtimesDirectory = filepath.Join(c.codacyDirectory, "runtimes")
37+
c.toolsDirectory = filepath.Join(c.codacyDirectory, "tools")
38+
39+
err := os.MkdirAll(c.codacyDirectory, 0777)
40+
if err != nil {
41+
log.Fatal(err)
42+
}
43+
err = os.MkdirAll(c.runtimesDirectory, 0777)
44+
if err != nil {
45+
log.Fatal(err)
46+
}
47+
err = os.MkdirAll(c.toolsDirectory, 0777)
48+
if err != nil {
49+
log.Fatal(err)
50+
}
51+
}
52+
53+
func Init() {
54+
homePath, err := os.UserHomeDir()
55+
if err != nil {
56+
log.Fatal(err)
57+
}
58+
Config.homePath = homePath
59+
60+
Config.initCodacyDirs()
61+
}

0 commit comments

Comments
 (0)