diff --git a/config.go b/config.go index ae24e98..9628185 100644 --- a/config.go +++ b/config.go @@ -6,7 +6,7 @@ var SCRIPTS = map[string]string{ "balance this month": "b -b \"this month\"", } -const SCRIPTS_FILE = "queries.txt" +const QUERIES_FILE = "queries.txt" const HTPASSWD_FILE = ".htpasswd" const DEFAULT_JOURNAL = "ledger.txt" const ARCHETYPES_DIR = "archetypes" diff --git a/models.go b/models.go index c51c9de..8eb876d 100644 --- a/models.go +++ b/models.go @@ -5,7 +5,7 @@ import ( "bytes" "fmt" "io" - "log" + "io/ioutil" "os" "os/exec" "path" @@ -54,7 +54,7 @@ func (u *User) WriteFile(name string) (*os.File, error) { func (u *User) List() ([]string, error) { files, err := os.ReadDir(u.Dir()) if err != nil { - panic(err) + return []string{}, fmt.Errorf("Failed to open directory: %w", err) } result := make([]string, len(files)) for i, v := range files { @@ -63,6 +63,16 @@ func (u *User) List() ([]string, error) { return result, nil } +func (u *User) readAllFile(name string) (data []byte, err error) { + f, err := u.ReadFile(name) + if err != nil { + return + } + defer f.Close() + data, err = ioutil.ReadAll(f) + return +} + func (u *User) appendToFile(tx string) (err error) { f, err := u.AppendFile(DEFAULT_JOURNAL) if err != nil { @@ -99,24 +109,24 @@ func (u *User) query(query string) (result string, err error) { return buf.String(), err } -func (u *User) scripts() (scripts map[string]string, err error) { - f, err := u.ReadFile(SCRIPTS_FILE) +func (u *User) queries() (queries [][2]string, err error) { + f, err := u.ReadFile(QUERIES_FILE) if err != nil { - panic(err) + err = fmt.Errorf("Failed to read queries file: %w", err) + return } defer f.Close() fileScanner := bufio.NewScanner(f) fileScanner.Split(bufio.ScanLines) - scripts = make(map[string]string) + queries = make([][2]string, 0) for fileScanner.Scan() { arr := strings.SplitN(fileScanner.Text(), ":", 2) if len(arr) < 2 { - err = fmt.Errorf("invalid data %s", arr) - return + continue } - scripts[arr[0]] = arr[1] + queries = append(queries, [2]string{arr[0], arr[1]}) } return } @@ -131,7 +141,6 @@ func (u *User) templates() (templates []string, err error) { templates = append(templates, v) } } - log.Println(templates) return } diff --git a/route.go b/route.go index b9c4465..472fb51 100644 --- a/route.go +++ b/route.go @@ -1,7 +1,6 @@ package main import ( - "io/ioutil" "log" "github.com/gin-gonic/gin" @@ -31,16 +30,18 @@ func router() *gin.Engine { authZone.GET("/dashboard", func(c *gin.Context) { user := getUser(c) - scripts, err := user.scripts() + queries, err := user.queries() if err != nil { - panic(err) + c.AbortWithError(500, err) + return } templates, err := user.templates() if err != nil { - panic(err) + c.AbortWithError(500, err) + return } HTML(c, 200, "dashboard.html", gin.H{ - "Scripts": scripts, + "Queries": queries, "Templates": templates, }) }) @@ -78,19 +79,15 @@ func router() *gin.Engine { filename := c.Query("filename") list, err := user.List() if err != nil { - panic(err) + c.AbortWithError(500, err) + return } exists := contain(list, filename) var data []byte if exists { - f, err := user.ReadFile(filename) + data, err = user.readAllFile(filename) if err != nil { - panic(err) - } - defer f.Close() - data, err = ioutil.ReadAll(f) - if err != nil { - panic(err) + c.AbortWithError(500, err) } } @@ -108,7 +105,8 @@ func router() *gin.Engine { data := c.PostForm("data") err := user.overwriteFile(filename, data) if err != nil { - panic(err) + c.AbortWithError(500, err) + return } HTML(c, 200, "success.html", gin.H{ "FileName": filename, @@ -126,19 +124,21 @@ func router() *gin.Engine { response := struct { Query string Result string - Scripts map[string]string + Queries [][2]string }{} var ok bool var err error - response.Scripts, err = user.scripts() + response.Queries, err = user.queries() if err != nil { - panic(err) + c.AbortWithError(500, err) + return } response.Query, ok = c.GetQuery("query") if ok && response.Query != "" { response.Result, err = user.query(response.Query) if err != nil { - panic(err) + c.AbortWithError(500, err) + return } } HTML(c, 200, "query.html", response) diff --git a/templates/dashboard.html b/templates/dashboard.html index ed336bf..5ba4be6 100644 --- a/templates/dashboard.html +++ b/templates/dashboard.html @@ -12,5 +12,5 @@ -{{ template "scripts" .Scripts }} +{{ template "queries" .Queries }} {{ end }} diff --git a/templates/layouts/queries.html b/templates/layouts/queries.html new file mode 100644 index 0000000..f4748bb --- /dev/null +++ b/templates/layouts/queries.html @@ -0,0 +1,8 @@ +{{ define "queries" }} +