-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
137 lines (115 loc) · 3.22 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"github.com/mkabdelrahman/coverco/conf"
"github.com/mkabdelrahman/coverco/finder"
"github.com/mkabdelrahman/coverco/printer"
"github.com/mkabdelrahman/coverco/reporter"
"github.com/charmbracelet/log"
)
func main() {
log.SetLevel(log.DebugLevel)
// Extract final configuration
config, err := conf.ExtractFinalConfig()
if err != nil {
log.Errorf("Error extracting final config: %s", err.Error())
return
}
// The first non-flag argument is the directory path
var dirPath string
if flag.NArg() > 0 {
dirPath = flag.Arg(0)
} else {
dirPath = "."
}
// Setup logging
err = setupLogging(config)
if err != nil {
log.Errorf("Error setting up logging: %s", err.Error())
return
}
packages, err := finder.FilterCoveredPackages(config, dirPath)
if err != nil {
fmt.Printf("Failed to create packages list: %v\n", err)
return
}
reporter, err := reporter.NewCoverageReporter(packages, config.DefaultCoverageThreshold, config.CoverageReportsDir, config.CoverageReportsFormat)
if err != nil {
log.Errorf("%s", err.Error())
return
}
coverages := reporter.TestPackages()
// Print coverage results
printer := printer.NewCoveragePrinter(reporter, os.Stdout)
printer.PrintCoverageTable(coverages)
if !config.KeepReports {
err = os.RemoveAll(config.CoverageReportsDir)
if err != nil {
log.Errorf("Error removing coverage reports directory: %s", err.Error())
}
}
if config.KeepReports && config.CoverageReportsFormat == "lcov" {
err = removeFilesWithExtension(config.CoverageReportsDir, ".out")
if err != nil {
log.Errorf("Error removing .lcov files: %s", err.Error())
}
}
if config.KeepReports && config.CoverageReportsFormat == "out" {
err = removeFilesWithExtension(config.CoverageReportsDir, ".lcov")
if err != nil {
log.Errorf("Error removing .lcov files: %s", err.Error())
}
}
}
// setupLogging sets up logging based on the configuration
func setupLogging(cfg conf.Config) error {
logLevel := cfg.Logging.Level
logFile := cfg.Logging.File
var logOutput *os.File
if logFile != "" {
var err error
logOutput, err = os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
return fmt.Errorf("error setting up log file: %w", err)
}
log.SetOutput(logOutput)
} else {
logOutput = os.Stdout
}
switch logLevel {
case "debug":
log.SetLevel(log.DebugLevel)
case "info":
log.SetLevel(log.InfoLevel)
case "warn":
log.SetLevel(log.WarnLevel)
case "error":
log.SetLevel(log.ErrorLevel)
default:
return fmt.Errorf("invalid log level: %s", logLevel)
}
return nil
}
// removeFilesWithExtension removes files with the given extension in the specified directory
func removeFilesWithExtension(dir, ext string) error {
files, err := filepath.Glob(filepath.Join(dir, "*"+ext))
if err != nil {
return fmt.Errorf("error finding files to delete: %w", err)
}
if len(files) > 0 {
log.Infof("Selected files for deletion: %v", files)
} else {
log.Infof("No files to delete with extension %s", ext)
}
for _, file := range files {
if err := os.Remove(file); err != nil {
log.Errorf("Error removing file %s: %s", file, err.Error())
} else {
log.Infof("Removed file %s", file)
}
}
return nil
}