Setup git repo on settings change

This commit is contained in:
2024-10-13 11:16:44 +02:00
parent aad6616ebe
commit ac54c083a9
4 changed files with 36 additions and 10 deletions

View File

@@ -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)
}
}
}

View File

@@ -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))

View File

@@ -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")

View File

@@ -30,13 +30,21 @@ const MainContent = ({ selectedFile, handleFileSelect, handleLinkClick }) => {
const handleSaveFile = useCallback(
async (filePath, content) => {
const success = await handleSave(filePath, content);
let success = await handleSave(filePath, content);
if (success) {
setHasUnsavedChanges(false);
if (settings.gitAutoCommit && settings.gitEnabled) {
const commitMessage = settings.gitCommitMsgTemplate.replace(
'${filename}',
filePath
);
success = await handleCommitAndPush(commitMessage);
}
}
return success;
},
[handleSave, setHasUnsavedChanges]
[handleSave, setHasUnsavedChanges, settings, handleCommitAndPush]
);
const handleCreateFile = useCallback(