-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
195 lines (162 loc) · 5.56 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"context"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
_ "time/tzdata" // TZ info for FROM scratch
"github.com/mahyarmirrashed/github-readme-stats/internal/config"
"github.com/mahyarmirrashed/github-readme-stats/internal/github"
"github.com/mahyarmirrashed/github-readme-stats/internal/stats"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
_ "golang.org/x/crypto/x509roots/fallback" // CA bundle for FROM scratch
)
func main() {
// Initialize logger with console output
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
zerolog.SetGlobalLevel(zerolog.DebugLevel) // Set default log level to Debug
// Fetch arguments excluding the program name
if len(os.Args) != 2 {
log.Error().Msg("Invalid number of arguments provided")
printUsage()
os.Exit(1)
}
argument := os.Args[1]
// Validate and process features
features, err := validate(argument)
if err != nil {
log.Error().Err(err).Msg("Invalid arguments provided")
printUsage()
os.Exit(1)
}
log.Debug().Msgf("Features to include: %v", features)
// Load and validate configuration
cfg := config.LoadConfig()
if cfg.GithubToken == "" {
log.Error().Msg("GITHUB_TOKEN not provided in configuration")
os.Exit(1)
}
log.Debug().Msgf("Timezone: %s", cfg.TimeZone)
// Get the current working directory
cwd, err := os.Getwd()
if err != nil {
log.Error().Err(err).Msg("Failed to get current working directory")
os.Exit(1)
}
readmePath := filepath.Join(cwd, "README.md")
log.Debug().Msgf("Path for README is: %s", readmePath)
// Create GitHub client
client := github.NewClient(cfg.GithubToken)
ctx := context.Background()
// Fetch repositories from user
repositories, err := github.FetchRepositories(ctx, client)
if err != nil {
log.Error().Err(err).Msg("Failed to get repositories from GitHub")
os.Exit(1)
}
// Fetch commits from all repositories
commits, err := github.FetchCommitsFromRepositories(ctx, client, repositories)
if err != nil {
log.Error().Err(err).Msg("Failed to get commits from repositories")
os.Exit(1)
}
// Fetch languages from all repositories
languages, err := github.FetchLanguagesFromRepositories(ctx, client, repositories)
if err != nil {
log.Error().Err(err).Msg("Failed to get languages from repositories")
os.Exit(1)
}
// Build the output content based on the order of features
var contentBuilder strings.Builder
codeBlock := func(content string) string { return "\n```\n" + content + "\n```\n" }
for _, feature := range features {
switch feature {
case "DAY_STATS":
log.Info().Msg("Calculating commit statistics based on time of day")
dailyStats, err := stats.GetDailyCommitData(cfg, commits)
if err != nil {
log.Error().Err(err).Msg("Failed to get daily commit stats")
os.Exit(1)
}
contentBuilder.WriteString(codeBlock(dailyStats))
case "WEEK_STATS":
log.Info().Msg("Calculating commit statistics based on day of week")
weeklyStats, err := stats.GetWeeklyCommitData(cfg, commits)
if err != nil {
log.Error().Err(err).Msg("Failed to get weekly commit stats")
os.Exit(1)
}
contentBuilder.WriteString(codeBlock(weeklyStats))
case "LANGUAGE_STATS":
log.Info().Msg("Calculating language statistics")
languageStats, err := stats.GetLanguageData(cfg, languages)
if err != nil {
log.Error().Err(err).Msg("Failed to get language stats")
os.Exit(1)
}
contentBuilder.WriteString(codeBlock(languageStats))
default:
// Unknown feature, skip or handle error
log.Warn().Msgf("Unknown feature: %s", feature)
contentBuilder.WriteString(fmt.Sprintf("\n\nUnknown feature: %s\n", feature))
}
}
// Append with newline
contentBuilder.WriteString("\n")
// Update the README file
if err := updateReadme(readmePath, contentBuilder.String()); err != nil {
log.Error().Err(err).Msg("Failed to update README.md")
os.Exit(1)
}
log.Info().Msg("README.md successfully updated")
}
// Validate the provided features
func validate(arg string) ([]string, error) {
validFeatures := map[string]bool{
"DAY_STATS": true,
"WEEK_STATS": true,
"LANGUAGE_STATS": true,
}
var features []string
for _, feature := range strings.Split(arg, ",") {
if !validFeatures[strings.ToUpper(strings.TrimSpace(feature))] {
return nil, fmt.Errorf("invalid feature argument: %s", feature)
}
features = append(features, feature)
}
return features, nil
}
// Replace README-STATS block with new content
func updateReadme(filepath string, newContent string) error {
// Read the file content
data, err := os.ReadFile(filepath)
if err != nil {
return fmt.Errorf("failed to read README.md file: %w", err)
}
// Define the regex to find the block between <!-- README-STATS:START --> and <!-- README-STATS:END -->
re := regexp.MustCompile(`(?s)<!--\s*README-STATS:START\s*-->(.*?)<!--\s*README-STATS:END\s*-->`)
if !re.Match(data) {
return fmt.Errorf("could not find README-STATS block in README.md")
}
// Replace the block content
updatedContent := re.ReplaceAllString(string(data), fmt.Sprintf("<!-- README-STATS:START -->\n%s<!-- README-STATS:END -->", newContent))
// Write the updated content back to the file
err = os.WriteFile(filepath, []byte(updatedContent), 0o644)
if err != nil {
return fmt.Errorf("failed to write updated README.md file: %w", err)
}
return nil
}
// Displays the correct usage of the program
func printUsage() {
usage := `
Usage: github-readme-stats <FEATURE_LIST>
FEATURE_LIST: Comma-separated list of features to include (e.g., DAY_STATS,WEEK_STATS,LANGUAGE_STATS)
Example:
github-readme-stats DAY_STATS,WEEK_STATS,LANGUAGE_STATS
`
fmt.Println(usage)
}