1
0
mirror of https://github.com/garraflavatra/rolens.git synced 2024-11-24 18:26:56 +01:00
rolens/internal/app/collection_find_queries.go

122 lines
2.9 KiB
Go
Raw Normal View History

2023-02-15 17:00:53 +01:00
package app
import (
"encoding/json"
"io/ioutil"
"path"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
type SavedQuery struct {
Query
Name string `json:"name"`
Remarks string `json:"remarks"`
HostKey string `json:"hostKey"`
DbKey string `json:"dbKey"`
CollKey string `json:"collKey"`
}
func updateQueryFile(a *App, newData map[string]SavedQuery) error {
filePath := path.Join(a.Env.DataDirectory, "queries.json")
jsonData, err := json.MarshalIndent(newData, "", "\t")
if err != nil {
return err
}
err = ioutil.WriteFile(filePath, jsonData, 0644)
2023-02-15 17:00:53 +01:00
return err
}
func (a *App) SavedQueries() map[string]SavedQuery {
filePath := path.Join(a.Env.DataDirectory, "queries.json")
jsonData, err := ioutil.ReadFile(filePath)
if err != nil {
// It's ok if the file cannot be opened, for example if it is not accessible.
// Therefore no error is returned.
2023-06-24 20:27:48 +02:00
runtime.LogInfof(a.ctx, "Could not open queries.json: %s", err.Error())
2023-02-15 17:00:53 +01:00
return make(map[string]SavedQuery, 0)
}
if len(jsonData) == 0 {
return make(map[string]SavedQuery, 0)
} else {
var queries map[string]SavedQuery
err = json.Unmarshal(jsonData, &queries)
if err != nil {
2023-06-24 20:27:48 +02:00
runtime.LogInfof(a.ctx, "queries.json file contains malformatted JSON data: %s", err.Error())
2023-02-15 17:00:53 +01:00
return nil
}
return queries
}
}
func (a *App) SaveQuery(jsonData string) string {
var query SavedQuery
err := json.Unmarshal([]byte(jsonData), &query)
if err != nil {
2023-06-24 20:27:48 +02:00
runtime.LogErrorf(a.ctx, "Add query: malformed form: %s", err.Error())
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Title: "Malformed JSON",
Message: err.Error(),
Type: runtime.ErrorDialog,
})
2023-02-15 17:00:53 +01:00
return ""
}
queries := a.SavedQueries()
queries[query.Name] = query
err = updateQueryFile(a, queries)
if err != nil {
2023-06-24 20:27:48 +02:00
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Title: "Could not update query list",
Message: err.Error(),
Type: runtime.ErrorDialog,
})
2023-02-15 17:00:53 +01:00
return ""
}
return query.Name
}
func (a *App) RemoveQuery(queryName string) {
queries := a.SavedQueries()
delete(queries, queryName)
if err := updateQueryFile(a, queries); err != nil {
2023-06-24 20:27:48 +02:00
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Title: "Could not update query list",
Message: err.Error(),
Type: runtime.ErrorDialog,
})
2023-02-15 17:00:53 +01:00
}
}
func (a *App) UpdateQueries(jsonData string) bool {
var queries map[string]SavedQuery
err := json.Unmarshal([]byte(jsonData), &queries)
if err != nil {
2023-06-24 20:27:48 +02:00
runtime.LogErrorf(a.ctx, "Update queries: malformed form: %s", err.Error())
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Title: "Malformed JSON",
Message: err.Error(),
Type: runtime.ErrorDialog,
})
2023-02-15 17:00:53 +01:00
return false
}
err = updateQueryFile(a, queries)
if err != nil {
2023-06-24 20:27:48 +02:00
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Title: "Could not save queries",
Message: err.Error(),
Type: runtime.ErrorDialog,
})
2023-02-15 17:00:53 +01:00
return false
}
return true
}