1
0
mirror of https://github.com/garraflavatra/rolens.git synced 2025-01-18 13:07:58 +00:00
rolens/internal/app/hosts.go

174 lines
4.1 KiB
Go
Raw Normal View History

2023-01-18 19:59:00 +00:00
package app
import (
"encoding/json"
"errors"
"io/ioutil"
2023-02-11 10:22:02 +00:00
"path"
2023-01-18 19:59:00 +00:00
"github.com/google/uuid"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
type Host struct {
2023-01-23 19:47:43 +00:00
Name string `json:"name"`
URI string `json:"uri"`
2023-01-18 19:59:00 +00:00
}
2023-02-11 10:22:02 +00:00
func updateHostsFile(a *App, newData map[string]Host) error {
filePath := path.Join(a.Env.DataDirectory, "hosts.json")
2023-01-18 19:59:00 +00:00
jsonData, err := json.MarshalIndent(newData, "", "\t")
if err != nil {
return err
}
err = ioutil.WriteFile(filePath, jsonData, 0644)
2023-01-18 19:59:00 +00:00
return err
}
func (a *App) Hosts() (map[string]Host, error) {
2023-02-11 10:22:02 +00:00
filePath := path.Join(a.Env.DataDirectory, "hosts.json")
2023-01-18 19:59:00 +00:00
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.LogInfof(a.ctx, "Could not open hosts.json (%s), trying to create it.", err.Error())
2023-01-18 19:59:00 +00:00
return make(map[string]Host, 0), nil
}
if len(jsonData) == 0 {
return make(map[string]Host, 0), nil
} else {
var hosts map[string]Host
err = json.Unmarshal(jsonData, &hosts)
if err != nil {
runtime.LogInfof(a.ctx, "host.json file contains malformatted JSON data: %s", err.Error())
2023-01-18 19:59:00 +00:00
return nil, errors.New("host.json file contains malformatted JSON data")
}
return hosts, nil
}
}
func (a *App) AddHost(jsonData string) string {
2023-01-18 19:59:00 +00:00
hosts, err := a.Hosts()
if err != nil {
2023-06-24 18:27:48 +00:00
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Title: "Error getting hosts",
Message: err.Error(),
Type: runtime.ErrorDialog,
})
return ""
2023-01-18 19:59:00 +00:00
}
var newHost Host
err = json.Unmarshal([]byte(jsonData), &newHost)
if err != nil {
runtime.LogErrorf(a.ctx, "Add host: malformed form: %s", err.Error())
2023-06-24 18:27:48 +00:00
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Title: "Malformed JSON",
Message: err.Error(),
Type: runtime.ErrorDialog,
})
return ""
2023-01-18 19:59:00 +00:00
}
id, err := uuid.NewRandom()
if err != nil {
runtime.LogErrorf(a.ctx, "Add host: failed to generate a UUID: %s", err.Error())
2023-06-24 18:27:48 +00:00
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Title: "Error generating UUID",
Message: err.Error(),
Type: runtime.ErrorDialog,
})
return ""
2023-01-18 19:59:00 +00:00
}
hosts[id.String()] = newHost
2023-02-11 10:22:02 +00:00
err = updateHostsFile(a, hosts)
2023-01-18 19:59:00 +00:00
if err != nil {
2023-06-24 18:27:48 +00:00
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Title: "Error updating host list",
Message: err.Error(),
Type: runtime.ErrorDialog,
})
return ""
2023-01-18 19:59:00 +00:00
}
return id.String()
2023-01-18 19:59:00 +00:00
}
func (a *App) UpdateHost(hostKey string, jsonData string) bool {
hosts, err := a.Hosts()
if err != nil {
2023-06-24 18:27:48 +00:00
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Title: "Error getting host list",
Message: err.Error(),
Type: runtime.ErrorDialog,
})
return false
}
var host Host
err = json.Unmarshal([]byte(jsonData), &host)
if err != nil {
runtime.LogErrorf(a.ctx, "Could not parse update host JSON: %s", err.Error())
2023-06-24 18:27:48 +00:00
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Title: "Malformed JSON",
Message: err.Error(),
Type: runtime.ErrorDialog,
})
return false
}
hosts[hostKey] = host
2023-02-11 10:22:02 +00:00
err = updateHostsFile(a, hosts)
if err != nil {
2023-06-24 18:27:48 +00:00
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Title: "Error updating hosts",
Message: err.Error(),
Type: runtime.ErrorDialog,
})
return false
}
return true
}
func (a *App) RemoveHost(key string) bool {
2023-01-18 19:59:00 +00:00
hosts, err := a.Hosts()
if err != nil {
2023-06-24 18:27:48 +00:00
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Title: "Error getting host list",
Message: err.Error(),
Type: runtime.ErrorDialog,
})
return false
2023-01-18 19:59:00 +00:00
}
2023-06-24 18:27:48 +00:00
choice, _ := runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Title: "Confirm",
Message: "Are you sure you want to remove " + hosts[key].Name + "?",
Buttons: []string{"Yes", "Cancel"},
DefaultButton: "Yes",
CancelButton: "Cancel",
})
if choice != "Yes" {
return false
2023-01-18 19:59:00 +00:00
}
delete(hosts, key)
2023-02-11 10:22:02 +00:00
err = updateHostsFile(a, hosts)
2023-01-18 19:59:00 +00:00
if err != nil {
2023-06-24 18:27:48 +00:00
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Title: "Error updating host list",
Message: err.Error(),
Type: runtime.ErrorDialog,
})
return false
2023-01-18 19:59:00 +00:00
}
return true
2023-01-18 19:59:00 +00:00
}