Add GTK application implementation
This commit is contained in:
parent
d1bb505e0a
commit
b7c0307f6b
29 changed files with 2564 additions and 266 deletions
78
internal/gui/config.go
Normal file
78
internal/gui/config.go
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
package gui
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type Configuration struct {
|
||||
Host string `json:"host"`
|
||||
Port uint64 `json:"port"`
|
||||
Password string `json:"password"`
|
||||
IsConnected bool `json:"connected"`
|
||||
Panel string `json:"panel"`
|
||||
SortField string `json:"sortField"`
|
||||
SortDesc bool `json:"sortDesc"`
|
||||
ItemSize int `json:"itemSize"`
|
||||
}
|
||||
|
||||
func readConfig(config *Configuration) error {
|
||||
filename, filenameErr := getConfigFile()
|
||||
if filenameErr != nil {
|
||||
return filenameErr
|
||||
}
|
||||
|
||||
file, fileErr := os.Open(filename)
|
||||
if fileErr != nil {
|
||||
return fileErr
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
return json.NewDecoder(file).Decode(config)
|
||||
}
|
||||
|
||||
func writeConfig(config *Configuration) error {
|
||||
filename, filenameErr := getConfigFile()
|
||||
if filenameErr != nil {
|
||||
return filenameErr
|
||||
}
|
||||
|
||||
file, fileErr := os.Create(filename)
|
||||
if fileErr != nil {
|
||||
return fileErr
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
encoder := json.NewEncoder(file)
|
||||
encoder.SetIndent("", "\t")
|
||||
|
||||
return encoder.Encode(&config)
|
||||
}
|
||||
|
||||
func getConfigFile() (string, error) {
|
||||
configDir, configDirErr := getConfigDir()
|
||||
if configDirErr != nil {
|
||||
return "", configDirErr
|
||||
}
|
||||
|
||||
createErr := ensureConfigDirExists(configDir)
|
||||
if createErr != nil {
|
||||
return "", createErr
|
||||
}
|
||||
|
||||
return filepath.Join(configDir, "config.json"), nil
|
||||
}
|
||||
|
||||
func ensureConfigDirExists(folder string) error {
|
||||
return os.MkdirAll(folder, 0o750)
|
||||
}
|
||||
|
||||
func getConfigDir() (string, error) {
|
||||
configDir, configDirErr := os.UserConfigDir()
|
||||
if configDirErr != nil {
|
||||
return "", configDirErr
|
||||
}
|
||||
|
||||
return filepath.Join(configDir, "mcg"), nil
|
||||
}
|
||||
Loading…
Reference in a new issue