Commit 5987d422 authored by Loïck Bonniot's avatar Loïck Bonniot

[gui] Add configuration package

parent ba93953b
Pipeline #396 passed with stage
// Package config handles basic configuration store for the GUI only.
// DFSS configuration is stored in the HOME/.dfss/config.json file
package config
import (
"encoding/json"
"io/ioutil"
"os"
"os/user"
"path/filepath"
)
// CAFile is the filename for the root certificate
const CAFile = "ca.pem"
// CertFile is the filename for the user certificate
const CertFile = "cert.pem"
// KeyFile is the filename for the private user key
const KeyFile = "key.pem"
// ConfigFile is the filename for the DFSS configuration file
const ConfigFile = "config.json"
// Config is the structure that will be persisted in the configuration file
type Config struct {
Platform string
// Virtual-only fields
Registered bool `json:"-"`
Authenticated bool `json:"-"`
}
// Load loads the configuration file into memory.
// If the file does not exist, the configuration will holds default values.
func Load() (conf Config) {
data, err := ioutil.ReadFile(getConfigFilename())
if err != nil {
return
}
_ = json.Unmarshal(data, &conf)
// Fill virtual-only fields
path := GetHomeDir()
conf.Registered = isFileValid(filepath.Join(path, KeyFile))
conf.Authenticated = isFileValid(filepath.Join(path, CertFile))
return
}
// Save stores the current configuration object from memory.
func Save(c Config) {
data, err := json.MarshalIndent(c, "", " ")
if err != nil {
return
}
_ = ioutil.WriteFile(getConfigFilename(), data, 0600)
}
// GetHomeDir is a helper to get the .dfss store directory
func GetHomeDir() string {
u, err := user.Current()
if err != nil {
return ""
}
dfssPath := filepath.Join(u.HomeDir, ".dfss")
if err := os.MkdirAll(dfssPath, os.ModeDir|0700); err != nil {
return ""
}
return dfssPath + string(filepath.Separator)
}
func getConfigFilename() string {
return filepath.Join(GetHomeDir(), ConfigFile)
}
func isFileValid(file string) bool {
_, err := os.Stat(file)
return err == nil
}
......@@ -2,6 +2,7 @@ package main
import (
"dfss"
"dfss/gui/config"
"dfss/gui/userform"
"github.com/visualfc/goqt/ui"
)
......@@ -10,8 +11,13 @@ const WIDTH = 650
const HEIGHT = 350
func main() {
// Load configuration
conf := config.Load()
// Start first window
ui.Run(func() {
form := userform.NewWidget()
form := userform.NewWidget(&conf)
layout := ui.NewVBoxLayout()
layout.AddWidget(form.W)
......
......@@ -2,11 +2,9 @@ package userform
import (
"io/ioutil"
"os"
osuser "os/user"
"path/filepath"
"dfss/dfssc/user"
"dfss/gui/config"
"github.com/visualfc/goqt/ui"
)
......@@ -14,7 +12,7 @@ type Widget struct {
W *ui.QWidget
}
func NewWidget() *Widget {
func NewWidget(conf *config.Config) *Widget {
file := ui.NewFileWithName(":/userform/userform.ui")
loader := ui.NewUiLoader()
form := loader.Load(file)
......@@ -27,7 +25,7 @@ func NewWidget() *Widget {
feedbackLabel := ui.NewLabelFromDriver(form.FindChild("feedbackLabel"))
registerButton := ui.NewPushButtonFromDriver(form.FindChild("registerButton"))
home := getHomeDir()
home := config.GetHomeDir()
fileDialog := ui.NewFileDialogWithParentCaptionDirectoryFilter(nil, "Select the CA file for the platform", home, "Root Certificates (*.pem);;Any (*.*)")
// Events
......@@ -40,13 +38,13 @@ func NewWidget() *Widget {
fileDialog.OnFileSelected(func(ca string) {
fileDialog.Hide()
caDest := home + "ca.pem"
caDest := home + config.CAFile
_ = copyCA(ca, caDest)
err := user.Register(
home+"ca.pem",
home+"cert.pem",
home+"key.pem",
caDest,
home+config.CertFile,
home+config.KeyFile,
hostField.Text(),
passwordField.Text(),
"", "", "", emailField.Text(), 2048,
......@@ -54,7 +52,8 @@ func NewWidget() *Widget {
if err != nil {
feedbackLabel.SetText(err.Error())
} else {
feedbackLabel.SetText("Registration done! Please check your mails.")
conf.Platform = hostField.Text()
config.Save(*conf)
}
form.SetDisabled(false)
})
......@@ -67,20 +66,6 @@ func NewWidget() *Widget {
return &Widget{W: form}
}
func getHomeDir() string {
u, err := osuser.Current()
if err != nil {
return ""
}
dfssPath := filepath.Join(u.HomeDir, ".dfss")
if err := os.MkdirAll(dfssPath, os.ModeDir|0700); err != nil {
return ""
}
return dfssPath + string(filepath.Separator)
}
func copyCA(from string, to string) error {
if from == to {
return nil
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment