1
0
mirror of https://github.com/garraflavatra/rolens.git synced 2024-12-01 06:20:40 +01:00
rolens/internal/app/collection_find_queries.go

108 lines
2.7 KiB
Go

package app
import (
"encoding/json"
"io/ioutil"
"os"
"path"
"github.com/ncruces/zenity"
"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, os.ModePerm)
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.
runtime.LogInfo(a.ctx, "Could not open queries.json")
runtime.LogInfo(a.ctx, err.Error())
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 {
runtime.LogInfo(a.ctx, "queries.json file contains malformatted JSON data")
runtime.LogInfo(a.ctx, err.Error())
return nil
}
return queries
}
}
func (a *App) SaveQuery(jsonData string) string {
var query SavedQuery
err := json.Unmarshal([]byte(jsonData), &query)
if err != nil {
runtime.LogError(a.ctx, "Add query: malformed form")
runtime.LogError(a.ctx, err.Error())
zenity.Error(err.Error(), zenity.Title("Malformed JSON"), zenity.ErrorIcon)
return ""
}
queries := a.SavedQueries()
queries[query.Name] = query
err = updateQueryFile(a, queries)
if err != nil {
zenity.Error(err.Error(), zenity.Title("Could not update query list"), zenity.ErrorIcon)
return ""
}
return query.Name
}
func (a *App) RemoveQuery(queryName string) {
queries := a.SavedQueries()
delete(queries, queryName)
if err := updateQueryFile(a, queries); err != nil {
zenity.Error(err.Error(), zenity.Title("Could not update query list"), zenity.ErrorIcon)
}
}
func (a *App) UpdateQueries(jsonData string) bool {
var queries map[string]SavedQuery
err := json.Unmarshal([]byte(jsonData), &queries)
if err != nil {
runtime.LogError(a.ctx, "Update queries: malformed form")
runtime.LogError(a.ctx, err.Error())
zenity.Error(err.Error(), zenity.Title("Malformed JSON"), zenity.ErrorIcon)
return false
}
err = updateQueryFile(a, queries)
if err != nil {
zenity.Error(err.Error(), zenity.Title("Could not save queries"), zenity.ErrorIcon)
return false
}
return true
}