mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-06 07:54:22 +00:00
Setup git repo on settings change
This commit is contained in:
@@ -97,9 +97,9 @@ func SaveFile(fs *filesystem.FileSystem) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
if _, err := w.Write([]byte("File saved successfully")); err != nil {
|
||||
http.Error(w, "Failed to write response", http.StatusInternalServerError)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if err := json.NewEncoder(w).Encode(map[string]string{"message": "File saved successfully"}); err != nil {
|
||||
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -144,7 +144,7 @@ func GetSettings(db *db.DB) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func UpdateSettings(db *db.DB) http.HandlerFunc {
|
||||
func UpdateSettings(db *db.DB, fs *filesystem.FileSystem) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var settings models.Settings
|
||||
if err := json.NewDecoder(r.Body).Decode(&settings); err != nil {
|
||||
@@ -165,6 +165,15 @@ func UpdateSettings(db *db.DB) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
if settings.Settings.GitEnabled {
|
||||
err := fs.SetupGitRepo(settings.Settings.GitURL, settings.Settings.GitUser, settings.Settings.GitToken)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to setup git repo", http.StatusInternalServerError)
|
||||
}
|
||||
} else {
|
||||
fs.DisableGitRepo()
|
||||
}
|
||||
|
||||
// Fetch the saved settings to return
|
||||
savedSettings, err := db.GetSettings(settings.UserID)
|
||||
if err != nil {
|
||||
@@ -203,9 +212,9 @@ func StageCommitAndPush(fs *filesystem.FileSystem) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
if _, err := w.Write([]byte("Changes staged, committed, and pushed successfully")); err != nil {
|
||||
http.Error(w, "Failed to write response", http.StatusInternalServerError)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if err := json.NewEncoder(w).Encode(map[string]string{"message": "Changes staged, committed, and pushed successfully"}); err != nil {
|
||||
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ func SetupRoutes(r chi.Router, db *db.DB, fs *filesystem.FileSystem) {
|
||||
r.Route("/", func(r chi.Router) {
|
||||
r.Route("/settings", func(r chi.Router) {
|
||||
r.Get("/", GetSettings(db))
|
||||
r.Post("/", UpdateSettings(db))
|
||||
r.Post("/", UpdateSettings(db, fs))
|
||||
})
|
||||
r.Route("/files", func(r chi.Router) {
|
||||
r.Get("/", ListFiles(fs))
|
||||
|
||||
@@ -42,6 +42,15 @@ func New(rootDir string, settings *models.Settings) *FileSystem {
|
||||
return fs
|
||||
}
|
||||
|
||||
func (fs *FileSystem) SetupGitRepo(gitURL string, gitUser string, gitToken string) error {
|
||||
fs.GitRepo = gitutils.New(gitURL, gitUser, gitToken, fs.RootDir)
|
||||
return fs.InitializeGitRepo()
|
||||
}
|
||||
|
||||
func (fs *FileSystem) DisableGitRepo() {
|
||||
fs.GitRepo = nil;
|
||||
}
|
||||
|
||||
func (fs *FileSystem) InitializeGitRepo() error {
|
||||
if fs.GitRepo == nil {
|
||||
return errors.New("git settings not configured")
|
||||
|
||||
Reference in New Issue
Block a user