-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
310 lines (256 loc) · 9.34 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"gopkg.in/yaml.v2"
)
type Project struct {
Version int `json:"version" yaml:"version"`
Name string `json:"name" yaml:"name"`
DisplayName string `json:"displayName" yaml:"display_name"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
Websites []URL `json:"websites,omitempty" yaml:"websites,omitempty"`
Github []URL `json:"github,omitempty" yaml:"github,omitempty"`
Social *Social `json:"social,omitempty" yaml:"social,omitempty"`
}
type URL struct {
Url string `json:"url" yaml:"url"`
}
type Social struct {
Twitter []URL `json:"twitter,omitempty" yaml:"twitter,omitempty"`
Telegram []URL `json:"telegram,omitempty" yaml:"telegram,omitempty"`
Mirror []URL `json:"mirror,omitempty" yaml:"mirror,omitempty"`
Discord []URL `json:"discord,omitempty" yaml:"discord,omitempty"`
}
type Response struct {
Message string `json:"message"`
Error string `json:"error,omitempty"`
LatestFile string `json:"latestFile,omitempty"`
}
var (
latestFile string
addedFiles []string
mutex sync.Mutex
)
func main() {
if err := pullFromUpstream(); err != nil {
log.Printf("Warning: Failed to pull from upstream: %v", err)
// Continue with server startup even if pull fails
}
http.HandleFunc("/createProject", createProjectHandler)
http.HandleFunc("/getLatestFile", getLatestFileHandler)
http.HandleFunc("/getCurrentBranch", getCurrentBranchHandler)
http.HandleFunc("/changeBranch", changeBranchHandler)
http.HandleFunc("/getAddedFiles", getAddedFilesHandler)
http.HandleFunc("/getFileContent", getFileContentHandler)
log.Println("Server started on :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
func createProjectHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodOptions {
setCorsHeaders(w)
w.WriteHeader(http.StatusOK)
return
}
if r.Method != http.MethodPost {
writeErrorResponse(w, http.StatusMethodNotAllowed, "Invalid request method")
return
}
setCorsHeaders(w)
var project Project
if err := json.NewDecoder(r.Body).Decode(&project); err != nil {
writeErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("Error decoding JSON: %v", err))
return
}
project.Version = 7
data, err := yaml.Marshal(&project)
if err != nil {
writeErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Error marshalling YAML: %v", err))
return
}
gitDir := "/Users/ahoura/oss-directory"
firstChar := strings.ToLower(string(project.Name[0]))
dirPath := filepath.Join(gitDir, "data/projects", firstChar)
if err := os.MkdirAll(dirPath, 0755); err != nil {
writeErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Error creating directory: %v", err))
return
}
filePath := filepath.Join(dirPath, fmt.Sprintf("%s.yaml", project.Name))
// Check if file already exists
if _, err := os.Stat(filePath); err == nil {
writeErrorResponse(w, http.StatusConflict, fmt.Sprintf("File %s already exists", filePath))
return
}
if err := os.WriteFile(filePath, data, 0644); err != nil {
writeErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Error writing file: %v", err))
return
}
// Update the latest file name
mutex.Lock()
latestFile = fmt.Sprintf("%s.yaml", project.Name)
addedFiles = append(addedFiles, latestFile)
mutex.Unlock()
log.Printf("Project created: %+v\n", project)
log.Printf("Latest file created: %s\n", latestFile)
// Run git commands
if err := runGitCommand("git", "add", "."); err != nil {
writeErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Error adding file to git: %v", err))
return
}
if err := runGitCommand("git", "commit", "-m", "Add new project "+project.Name); err != nil {
if !strings.Contains(err.Error(), "nothing to commit, working tree clean") {
writeErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Error committing file to git: %v", err))
return
}
}
/*if err := runGitCommand("git", "pull", "origin", "main", "--rebase"); err != nil {
writeErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Error pulling changes from git: %v", err))
return
}
if err := runGitCommand("git", "push", "origin", "main"); err != nil {
writeErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Error pushing changes to git: %v", err))
return
}*/
writeSuccessResponse(w, "Project created and changes commited", latestFile)
}
func getCurrentBranchHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
writeErrorResponse(w, http.StatusMethodNotAllowed, "Invalid request method")
return
}
setCorsHeaders(w)
cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
cmd.Dir = "/Users/ahoura/oss-directory"
output, err := cmd.Output()
if err != nil {
writeErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Error getting current branch: %v", err))
return
}
currentBranch := strings.TrimSpace(string(output))
writeSuccessResponse(w, "Current branch retrieved successfully", currentBranch)
}
func changeBranchHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
writeErrorResponse(w, http.StatusMethodNotAllowed, "Invalid request method")
return
}
setCorsHeaders(w)
branchName := r.URL.Query().Get("branch")
if branchName == "" {
writeErrorResponse(w, http.StatusBadRequest, "Branch name is required")
return
}
cmd := exec.Command("git", "checkout", branchName)
cmd.Dir = "/Users/ahoura/oss-directory"
output, err := cmd.CombinedOutput()
if err != nil {
writeErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Error changing branch: %v\nOutput: %s", err, string(output)))
return
}
writeSuccessResponse(w, fmt.Sprintf("Successfully changed to branch: %s", branchName), "")
}
func getLatestFileHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
writeErrorResponse(w, http.StatusMethodNotAllowed, "Invalid request method")
return
}
setCorsHeaders(w)
mutex.Lock()
currentLatestFile := latestFile
mutex.Unlock()
writeSuccessResponse(w, "Latest file retrieved successfully", currentLatestFile)
}
func getAddedFilesHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
writeErrorResponse(w, http.StatusMethodNotAllowed, "Invalid request method")
return
}
setCorsHeaders(w)
mutex.Lock()
response := struct {
Files []string `json:"files"`
}{
Files: addedFiles,
}
mutex.Unlock()
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(response)
}
func getFileContentHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
writeErrorResponse(w, http.StatusMethodNotAllowed, "Invalid request method")
return
}
setCorsHeaders(w)
filename := r.URL.Query().Get("filename")
if filename == "" {
writeErrorResponse(w, http.StatusBadRequest, "Filename is required")
return
}
gitDir := "/Users/ahoura/oss-directory"
filePath := filepath.Join(gitDir, "data", "projects", string(filename[0]), filename)
content, err := os.ReadFile(filePath)
if err != nil {
writeErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Error reading file: %v", err))
return
}
w.Header().Set("Content-Type", "text/plain")
w.Write(content)
}
func writeSuccessResponse(w http.ResponseWriter, message string, latestFile string) {
w.WriteHeader(http.StatusOK)
response := Response{Message: message, LatestFile: latestFile}
json.NewEncoder(w).Encode(response)
}
func runGitCommand(name string, args ...string) error {
cmd := exec.Command(name, args...)
var out, errBuf strings.Builder
cmd.Stdout = &out
cmd.Stderr = &errBuf
cmd.Dir = "/Users/ahoura/oss-directory"
if err := cmd.Run(); err != nil {
log.Printf("Error running git command: %v\nOutput: %s\nError: %s\n", err, out.String(), errBuf.String())
return fmt.Errorf("error running git command: %v\nOutput: %s\nError: %s\n", err, out.String(), errBuf.String())
}
log.Printf("Git command output: %s\n", out.String())
return nil
}
func setCorsHeaders(w http.ResponseWriter) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
}
func writeErrorResponse(w http.ResponseWriter, statusCode int, message string) {
log.Println(message)
w.WriteHeader(statusCode)
response := Response{Error: message}
json.NewEncoder(w).Encode(response)
}
func pullFromUpstream() error {
log.Println("Attempting to pull from upstream repository...")
// First, fetch the latest changes from upstream
fetchCmd := exec.Command("git", "fetch", "upstream")
fetchCmd.Dir = "/Users/ahoura/oss-directory"
fetchOutput, err := fetchCmd.CombinedOutput()
if err != nil {
return fmt.Errorf("error fetching from upstream: %v\nOutput: %s", err, string(fetchOutput))
}
log.Printf("Fetch from upstream successful. Output: %s", string(fetchOutput))
// Now, merge the changes into the current branch
mergeCmd := exec.Command("git", "merge", "upstream/main")
mergeCmd.Dir = "/Users/ahoura/oss-directory"
mergeOutput, err := mergeCmd.CombinedOutput()
if err != nil {
return fmt.Errorf("error merging upstream changes: %v\nOutput: %s", err, string(mergeOutput))
}
log.Printf("Merge from upstream successful. Output: %s", string(mergeOutput))
log.Println("Successfully pulled and merged changes from upstream repository.")
return nil
}