Skip to content

Commit 035cb39

Browse files
committed
Edit git/config and gitignore
1 parent ad9f085 commit 035cb39

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed

internal/models/models.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ type GuiData struct {
2828
Icon string
2929
Repos []Repo
3030
Themes []string
31+
Path string
3132
}

internal/web/edit.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package web
2+
3+
import (
4+
// "log"
5+
"net/http"
6+
"os"
7+
"strconv"
8+
9+
"github.com/aceberg/git-syr/internal/check"
10+
"github.com/aceberg/git-syr/internal/models"
11+
)
12+
13+
func editHandler(w http.ResponseWriter, r *http.Request) {
14+
var guiData models.GuiData
15+
guiData.Config = AppConfig
16+
guiData.Themes = make([]string, 2)
17+
18+
idStr := r.URL.Query().Get("id")
19+
id, err := strconv.Atoi(idStr)
20+
check.IfError(err)
21+
22+
for _, oneRepo := range AllRepos {
23+
if oneRepo.ID == id {
24+
guiData.Path = oneRepo.Path
25+
26+
file1, err := os.ReadFile(oneRepo.Path + "/.git/config")
27+
check.IfError(err)
28+
guiData.Themes[0] = string(file1)
29+
30+
file2, err := os.ReadFile(oneRepo.Path + "/.gitignore")
31+
check.IfError(err)
32+
guiData.Themes[1] = string(file2)
33+
}
34+
}
35+
36+
execTemplate(w, "edit", guiData)
37+
}
38+
39+
func saveFileHandler(w http.ResponseWriter, r *http.Request) {
40+
41+
file := r.FormValue("file")
42+
path := r.FormValue("path")
43+
text := r.FormValue("text")
44+
45+
if file == "config" {
46+
err := os.WriteFile(path+"/.git/config", []byte(text), 0644)
47+
check.IfError(err)
48+
}
49+
if file == "ignore" {
50+
err := os.WriteFile(path+"/.gitignore", []byte(text), 0644)
51+
check.IfError(err)
52+
}
53+
54+
http.Redirect(w, r, r.Header.Get("Referer"), 302)
55+
}

internal/web/templates/edit.html

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{{ define "edit" }}
2+
3+
<body>
4+
<div class="container mt-5">
5+
<div class="row">
6+
<div class="col">
7+
<p>Edit .git/config</p>
8+
<form action="/save_file/" method="post">
9+
<input name="file" value="config" type="hidden">
10+
<input name="path" value="{{ .Path }}" type="hidden">
11+
<textarea name="text" rows="15" class="form-control">{{index .Themes 0}}</textarea>
12+
<button type="submit" class="btn btn-primary">Save</button>
13+
</form>
14+
</div>
15+
<div class="col">
16+
<p>Edit .gitignore</p>
17+
<form action="/save_file/" method="post">
18+
<input name="file" value="ignore" type="hidden">
19+
<input name="path" value="{{ .Path }}" type="hidden">
20+
<textarea name="text" rows="15" class="form-control">{{index .Themes 1}}</textarea>
21+
<button type="submit" class="btn btn-primary">Save</button>
22+
</form>
23+
</div>
24+
</div>
25+
</div>
26+
27+
28+
{{ template "footer" }}
29+
{{ end }}

internal/web/webgui.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ func Gui(confPath, yamlPath, logPath string) {
3535
http.HandleFunc("/", indexHandler)
3636
http.HandleFunc("/add_repo/", addHandler)
3737
http.HandleFunc("/config/", configHandler)
38-
// http.HandleFunc("/edit/", editHandler)
38+
http.HandleFunc("/edit/", editHandler)
3939
http.HandleFunc("/error/", errorLogHandler)
4040
http.HandleFunc("/help/", helpHandler)
4141
http.HandleFunc("/log/", logHandler)
4242
http.HandleFunc("/save_config/", saveConfigHandler)
43+
http.HandleFunc("/save_file/", saveFileHandler)
4344
http.HandleFunc("/update_repo/", updateHandler)
4445
err := http.ListenAndServe(address, nil)
4546
check.IfError(err)

0 commit comments

Comments
 (0)