Skip to content

Commit a65f9cd

Browse files
feature: add verbose file logging (#100)
* feature: add verbose file logging - Add custom text formatter for more readable log output - Include file location and line numbers in log entries - Change log format from JSON to human-readable text - Add comprehensive logging to the install command - Configure log rotation with lumberjack
1 parent f6c5164 commit a65f9cd

File tree

7 files changed

+206
-8
lines changed

7 files changed

+206
-8
lines changed

.cursor/rules/cursor.mdc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ alwaysApply: true
66

77
# Your rule content
88

9+
## Key Rules
10+
- run go build after each code modification to see if app compiles
11+
912
## Code Style Guidelines
1013
- **Imports**: Standard lib first, external packages second, internal last
1114
- **Naming**: PascalCase for exported (public), camelCase for unexported (private)
@@ -19,4 +22,4 @@ alwaysApply: true
1922
- `cmd/`: CLI command implementations
2023
- `config/`: Configuration handling
2124
- `tools/`: Tool-specific implementations
22-
- `utils/`: Utility functions
25+
- `utils/`: Utility functions and static - look for static like default file permisson here

cmd/init.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ func createGitIgnoreFile() error {
9494
tools-configs/
9595
.gitignore
9696
cli-config.yaml
97+
logs/
9798
`
9899
if _, err := gitIgnoreFile.WriteString(content); err != nil {
99100
return fmt.Errorf("failed to write to .gitignore file: %w", err)

cmd/install.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"codacy/cli-v2/config"
55
cfg "codacy/cli-v2/config"
66
config_file "codacy/cli-v2/config-file"
7+
"codacy/cli-v2/utils/logger"
78
"fmt"
89
"io"
910
"log"
@@ -12,6 +13,7 @@ import (
1213

1314
"github.com/fatih/color"
1415
"github.com/schollz/progressbar/v3"
16+
"github.com/sirupsen/logrus"
1517
"github.com/spf13/cobra"
1618
)
1719

@@ -32,11 +34,17 @@ var installCmd = &cobra.Command{
3234

3335
// Create necessary directories
3436
if err := config.Config.CreateCodacyDirs(); err != nil {
37+
logger.Error("Failed to create Codacy directories", logrus.Fields{
38+
"error": err.Error(),
39+
})
3540
log.Fatal(err)
3641
}
3742

3843
// Load config file
3944
if err := config_file.ReadConfigFile(cfg.Config.ProjectConfigFile()); err != nil {
45+
logger.Warn("Configuration file not found", logrus.Fields{
46+
"error": err.Error(),
47+
})
4048
fmt.Println()
4149
color.Red("⚠️ Warning: Could not find configuration file!")
4250
fmt.Println("Please run 'codacy-cli init' first to create a configuration file.")
@@ -62,11 +70,13 @@ var installCmd = &cobra.Command{
6270
}
6371

6472
if !needsInstallation {
73+
logger.Info("All components are already installed", nil)
6574
fmt.Println()
6675
bold.Println("✅ All components are already installed!")
6776
return
6877
}
6978

79+
logger.Info("Starting installation process", nil)
7080
fmt.Println()
7181
bold.Println("🚀 Starting installation process...")
7282
fmt.Println()
@@ -85,6 +95,7 @@ var installCmd = &cobra.Command{
8595
}
8696

8797
if totalItems == 0 {
98+
logger.Info("All components are already installed", nil)
8899
fmt.Println()
89100
bold.Println("✅ All components are already installed!")
90101
return
@@ -94,11 +105,19 @@ var installCmd = &cobra.Command{
94105
fmt.Println("📦 Items to install:")
95106
for name, runtime := range cfg.Config.Runtimes() {
96107
if !cfg.Config.IsRuntimeInstalled(name, runtime) {
108+
logger.Info("Runtime scheduled for installation", logrus.Fields{
109+
"runtime": name,
110+
"version": runtime.Version,
111+
})
97112
fmt.Printf(" • Runtime: %s v%s\n", name, runtime.Version)
98113
}
99114
}
100115
for name, tool := range cfg.Config.Tools() {
101116
if !cfg.Config.IsToolInstalled(name, tool) {
117+
logger.Info("Tool scheduled for installation", logrus.Fields{
118+
"tool": name,
119+
"version": tool.Version,
120+
})
102121
fmt.Printf(" • Tool: %s v%s\n", name, tool.Version)
103122
}
104123
}
@@ -136,10 +155,23 @@ var installCmd = &cobra.Command{
136155
for name, runtime := range cfg.Config.Runtimes() {
137156
if !cfg.Config.IsRuntimeInstalled(name, runtime) {
138157
progressBar.Describe(fmt.Sprintf("Installing runtime: %s v%s...", name, runtime.Version))
158+
logger.Info("Installing runtime", logrus.Fields{
159+
"runtime": name,
160+
"version": runtime.Version,
161+
})
139162
err := cfg.InstallRuntime(name, runtime)
140163
if err != nil {
164+
logger.Error("Failed to install runtime", logrus.Fields{
165+
"runtime": name,
166+
"version": runtime.Version,
167+
"error": err.Error(),
168+
})
141169
log.Fatal(err)
142170
}
171+
logger.Info("Successfully installed runtime", logrus.Fields{
172+
"runtime": name,
173+
"version": runtime.Version,
174+
})
143175
progressBar.Add(1)
144176
}
145177
}
@@ -148,10 +180,23 @@ var installCmd = &cobra.Command{
148180
for name, tool := range cfg.Config.Tools() {
149181
if !cfg.Config.IsToolInstalled(name, tool) {
150182
progressBar.Describe(fmt.Sprintf("Installing tool: %s v%s...", name, tool.Version))
183+
logger.Info("Installing tool", logrus.Fields{
184+
"tool": name,
185+
"version": tool.Version,
186+
})
151187
err := cfg.InstallTool(name, tool, registry)
152188
if err != nil {
189+
logger.Error("Failed to install tool", logrus.Fields{
190+
"tool": name,
191+
"version": tool.Version,
192+
"error": err.Error(),
193+
})
153194
log.Fatal(err)
154195
}
196+
logger.Info("Successfully installed tool", logrus.Fields{
197+
"tool": name,
198+
"version": tool.Version,
199+
})
155200
progressBar.Add(1)
156201
}
157202
}
@@ -174,13 +219,17 @@ var installCmd = &cobra.Command{
174219
}
175220
}
176221
fmt.Println()
222+
logger.Info("Installation completed successfully", nil)
177223
bold.Println("✅ Installation completed successfully!")
178224
},
179225
}
180226

181227
func installRuntimes(config *cfg.ConfigType) {
182228
err := cfg.InstallRuntimes(config)
183229
if err != nil {
230+
logger.Error("Failed to install runtimes", logrus.Fields{
231+
"error": err.Error(),
232+
})
184233
log.Fatal(err)
185234
}
186235
}
@@ -189,6 +238,9 @@ func installTools(config *cfg.ConfigType, registry string) {
189238
// Use the new tools-installer instead of manual installation
190239
err := cfg.InstallTools(config, registry)
191240
if err != nil {
241+
logger.Error("Failed to install tools", logrus.Fields{
242+
"error": err.Error(),
243+
})
192244
log.Fatal(err)
193245
}
194246
}

cmd/root.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import (
55
"os"
66
"path/filepath"
77

8+
"codacy/cli-v2/config"
9+
"codacy/cli-v2/utils/logger"
10+
811
"github.com/fatih/color"
912
"github.com/spf13/cobra"
1013
)
@@ -14,6 +17,12 @@ var rootCmd = &cobra.Command{
1417
Short: "Codacy CLI - A command line interface for Codacy",
1518
Long: "",
1619
Example: getExampleText(),
20+
PersistentPreRun: func(cmd *cobra.Command, args []string) {
21+
// Initialize logger before any command runs
22+
if err := logger.Initialize(&config.Config); err != nil {
23+
fmt.Printf("Warning: Failed to initialize file logger: %v\n", err)
24+
}
25+
},
1726
Run: func(cmd *cobra.Command, args []string) {
1827
// Check if .codacy directory exists
1928
if _, err := os.Stat(".codacy"); os.IsNotExist(err) {

go.mod

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@ module codacy/cli-v2
22

33
go 1.22.3
44

5-
require github.com/spf13/cobra v1.8.0
5+
require (
6+
github.com/fatih/color v1.18.0
7+
github.com/schollz/progressbar/v3 v3.18.0
8+
github.com/sirupsen/logrus v1.9.3
9+
github.com/spf13/cobra v1.8.0
10+
gopkg.in/natefinch/lumberjack.v2 v2.2.1
11+
)
612

713
require (
814
github.com/davecgh/go-spew v1.1.1 // indirect
9-
github.com/fatih/color v1.18.0 // indirect
1015
github.com/mattn/go-colorable v0.1.13 // indirect
1116
github.com/mattn/go-isatty v0.0.20 // indirect
1217
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
1318
github.com/pmezard/go-difflib v1.0.0 // indirect
1419
github.com/rivo/uniseg v0.4.7 // indirect
15-
github.com/schollz/progressbar/v3 v3.18.0 // indirect
1620
golang.org/x/sys v0.29.0 // indirect
1721
golang.org/x/term v0.28.0 // indirect
1822
)

go.sum

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ github.com/bodgit/sevenzip v1.3.0/go.mod h1:omwNcgZTEooWM8gA/IJ2Nk/+ZQ94+GsytRzO
2626
github.com/bodgit/windows v1.0.0 h1:rLQ/XjsleZvx4fR1tB/UxQrK+SJ2OFHzfPjLWWOhDIA=
2727
github.com/bodgit/windows v1.0.0/go.mod h1:a6JLwrB4KrTR5hBpp8FI9/9W9jJfeQ2h4XDXU74ZCdM=
2828
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
29+
github.com/chengxilo/virtualterm v1.0.4 h1:Z6IpERbRVlfB8WkOmtbHiDbBANU7cimRIof7mk9/PwM=
30+
github.com/chengxilo/virtualterm v1.0.4/go.mod h1:DyxxBZz/x1iqJjFxTFcr6/x+jSpqN0iwWCOK1q10rlY=
2931
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
3032
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
3133
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
@@ -102,6 +104,8 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
102104
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
103105
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
104106
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
107+
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
108+
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
105109
github.com/mholt/archiver/v4 v4.0.0-alpha.8 h1:tRGQuDVPh66WCOelqe6LIGh0gwmfwxUrSSDunscGsRM=
106110
github.com/mholt/archiver/v4 v4.0.0-alpha.8/go.mod h1:5f7FUYGXdJWUjESffJaYR4R60VhnHxb2X3T1teMyv5A=
107111
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
@@ -120,15 +124,16 @@ github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD
120124
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd/go.mod h1:hPqNNc0+uJM6H+SuU8sEs5K5IQeKccPqeSjfgcKGgPk=
121125
github.com/schollz/progressbar/v3 v3.18.0 h1:uXdoHABRFmNIjUfte/Ex7WtuyVslrw2wVPQmCN62HpA=
122126
github.com/schollz/progressbar/v3 v3.18.0/go.mod h1:IsO3lpbaGuzh8zIMzgY3+J8l4C8GjO0Y9S69eFvNsec=
127+
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
128+
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
123129
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
124130
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
125131
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
126132
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
127133
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
128134
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
135+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
129136
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
130-
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
131-
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
132137
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
133138
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
134139
github.com/therootcompany/xz v1.0.1 h1:CmOtsn1CbtmyYiusbfmhmkpAAETj0wBIH6kCYaX+xzw=
@@ -209,10 +214,9 @@ golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7w
209214
golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
210215
golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
211216
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
217+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
212218
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
213219
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
214-
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
215-
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
216220
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
217221
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
218222
golang.org/x/term v0.28.0 h1:/Ts8HFuMR2E6IP/jlo7QVLZHggjKQbhu/7H0LJFr3Gg=
@@ -289,6 +293,8 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
289293
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
290294
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
291295
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
296+
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
297+
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
292298
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
293299
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
294300
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

0 commit comments

Comments
 (0)