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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
w.Header().Set("Content-Type", "application/json")
|
||||||
if _, err := w.Write([]byte("File saved successfully")); err != nil {
|
if err := json.NewEncoder(w).Encode(map[string]string{"message": "File saved successfully"}); err != nil {
|
||||||
http.Error(w, "Failed to write response", http.StatusInternalServerError)
|
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) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
var settings models.Settings
|
var settings models.Settings
|
||||||
if err := json.NewDecoder(r.Body).Decode(&settings); err != nil {
|
if err := json.NewDecoder(r.Body).Decode(&settings); err != nil {
|
||||||
@@ -165,6 +165,15 @@ func UpdateSettings(db *db.DB) http.HandlerFunc {
|
|||||||
return
|
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
|
// Fetch the saved settings to return
|
||||||
savedSettings, err := db.GetSettings(settings.UserID)
|
savedSettings, err := db.GetSettings(settings.UserID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -203,9 +212,9 @@ func StageCommitAndPush(fs *filesystem.FileSystem) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
w.Header().Set("Content-Type", "application/json")
|
||||||
if _, err := w.Write([]byte("Changes staged, committed, and pushed successfully")); err != nil {
|
if err := json.NewEncoder(w).Encode(map[string]string{"message": "Changes staged, committed, and pushed successfully"}); err != nil {
|
||||||
http.Error(w, "Failed to write response", http.StatusInternalServerError)
|
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("/", func(r chi.Router) {
|
||||||
r.Route("/settings", func(r chi.Router) {
|
r.Route("/settings", func(r chi.Router) {
|
||||||
r.Get("/", GetSettings(db))
|
r.Get("/", GetSettings(db))
|
||||||
r.Post("/", UpdateSettings(db))
|
r.Post("/", UpdateSettings(db, fs))
|
||||||
})
|
})
|
||||||
r.Route("/files", func(r chi.Router) {
|
r.Route("/files", func(r chi.Router) {
|
||||||
r.Get("/", ListFiles(fs))
|
r.Get("/", ListFiles(fs))
|
||||||
|
|||||||
@@ -42,6 +42,15 @@ func New(rootDir string, settings *models.Settings) *FileSystem {
|
|||||||
return fs
|
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 {
|
func (fs *FileSystem) InitializeGitRepo() error {
|
||||||
if fs.GitRepo == nil {
|
if fs.GitRepo == nil {
|
||||||
return errors.New("git settings not configured")
|
return errors.New("git settings not configured")
|
||||||
|
|||||||
@@ -30,13 +30,21 @@ const MainContent = ({ selectedFile, handleFileSelect, handleLinkClick }) => {
|
|||||||
|
|
||||||
const handleSaveFile = useCallback(
|
const handleSaveFile = useCallback(
|
||||||
async (filePath, content) => {
|
async (filePath, content) => {
|
||||||
const success = await handleSave(filePath, content);
|
let success = await handleSave(filePath, content);
|
||||||
if (success) {
|
if (success) {
|
||||||
setHasUnsavedChanges(false);
|
setHasUnsavedChanges(false);
|
||||||
|
|
||||||
|
if (settings.gitAutoCommit && settings.gitEnabled) {
|
||||||
|
const commitMessage = settings.gitCommitMsgTemplate.replace(
|
||||||
|
'${filename}',
|
||||||
|
filePath
|
||||||
|
);
|
||||||
|
success = await handleCommitAndPush(commitMessage);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return success;
|
return success;
|
||||||
},
|
},
|
||||||
[handleSave, setHasUnsavedChanges]
|
[handleSave, setHasUnsavedChanges, settings, handleCommitAndPush]
|
||||||
);
|
);
|
||||||
|
|
||||||
const handleCreateFile = useCallback(
|
const handleCreateFile = useCallback(
|
||||||
|
|||||||
Reference in New Issue
Block a user