-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandlers.go
More file actions
97 lines (82 loc) · 2.23 KB
/
handlers.go
File metadata and controls
97 lines (82 loc) · 2.23 KB
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
package main
import (
"./downloader"
"./editor"
"./parser"
"fmt"
"github.com/labstack/echo"
"log"
"net/http"
"net/url"
)
func returnError(err error) string {
return fmt.Sprintf(successFalse, err)
}
func handleAll(c echo.Context) error {
basepath := downloader.GetPluginsPath("")
files := parser.GetFiles(basepath)
parsed := parser.ParseFiles(basepath, files)
return c.JSON(http.StatusOK, parsed)
}
type successMsg struct {
Success bool `json:"success"`
Message string `json:"message"`
}
const (
successTrue = "All is well"
successFalse = "Error: %v"
)
func handlePack(c echo.Context) error {
query_url := c.QueryParam("url")
parsed_url, err := url.Parse(query_url)
if err != nil {
return c.JSON(http.StatusOK, successMsg{false, returnError(err)})
}
tmp_path, err := downloader.Download(parsed_url.String())
if err != nil {
return c.JSON(http.StatusOK, successMsg{false, returnError(err)})
}
if debugMode {
downloader.DebugMode = true
}
pack_name := downloader.GetFilenameFromURL(parsed_url.String())
err = downloader.ExtractPack(pack_name, tmp_path)
if err != nil {
return c.JSON(http.StatusOK, successMsg{false, returnError(err)})
} else {
return c.JSON(http.StatusOK, successMsg{true, successTrue})
}
}
func handleAdd(c echo.Context) error {
filename := c.FormValue("filename")
content := c.FormValue("content")
err := editor.AddFile(filename, content)
if err != nil {
log.Println(err)
return c.JSON(http.StatusOK, successMsg{false, returnError(err)})
} else {
return c.JSON(http.StatusOK, successMsg{true, successTrue})
}
}
func handleEdit(c echo.Context) error {
relpath := c.FormValue("filepath")
content := c.FormValue("content")
fmt.Println("File path:", relpath)
fmt.Println("Content:", content)
err := editor.EditFile(relpath, content, false)
if err != nil {
log.Println(err)
return c.JSON(http.StatusOK, successMsg{false, returnError(err)})
} else {
return c.JSON(http.StatusOK, successMsg{true, successTrue})
}
}
func handleDelete(c echo.Context) error {
relpath := c.QueryParam("filepath")
err := editor.DeleteFile(relpath)
if err != nil {
return c.JSON(http.StatusOK, successMsg{false, returnError(err)})
} else {
return c.JSON(http.StatusOK, successMsg{true, successTrue})
}
}