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

[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 ...@@ -2,6 +2,7 @@ package main
import ( import (
"dfss" "dfss"
"dfss/gui/config"
"dfss/gui/userform" "dfss/gui/userform"
"github.com/visualfc/goqt/ui" "github.com/visualfc/goqt/ui"
) )
...@@ -10,8 +11,13 @@ const WIDTH = 650 ...@@ -10,8 +11,13 @@ const WIDTH = 650
const HEIGHT = 350 const HEIGHT = 350
func main() { func main() {
// Load configuration
conf := config.Load()
// Start first window
ui.Run(func() { ui.Run(func() {
form := userform.NewWidget() form := userform.NewWidget(&conf)
layout := ui.NewVBoxLayout() layout := ui.NewVBoxLayout()
layout.AddWidget(form.W) layout.AddWidget(form.W)
......
...@@ -2,11 +2,9 @@ package userform ...@@ -2,11 +2,9 @@ package userform
import ( import (
"io/ioutil" "io/ioutil"
"os"
osuser "os/user"
"path/filepath"
"dfss/dfssc/user" "dfss/dfssc/user"
"dfss/gui/config"
"github.com/visualfc/goqt/ui" "github.com/visualfc/goqt/ui"
) )
...@@ -14,7 +12,7 @@ type Widget struct { ...@@ -14,7 +12,7 @@ type Widget struct {
W *ui.QWidget W *ui.QWidget
} }
func NewWidget() *Widget { func NewWidget(conf *config.Config) *Widget {
file := ui.NewFileWithName(":/userform/userform.ui") file := ui.NewFileWithName(":/userform/userform.ui")
loader := ui.NewUiLoader() loader := ui.NewUiLoader()
form := loader.Load(file) form := loader.Load(file)
...@@ -27,7 +25,7 @@ func NewWidget() *Widget { ...@@ -27,7 +25,7 @@ func NewWidget() *Widget {
feedbackLabel := ui.NewLabelFromDriver(form.FindChild("feedbackLabel")) feedbackLabel := ui.NewLabelFromDriver(form.FindChild("feedbackLabel"))
registerButton := ui.NewPushButtonFromDriver(form.FindChild("registerButton")) 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 (*.*)") fileDialog := ui.NewFileDialogWithParentCaptionDirectoryFilter(nil, "Select the CA file for the platform", home, "Root Certificates (*.pem);;Any (*.*)")
// Events // Events
...@@ -40,13 +38,13 @@ func NewWidget() *Widget { ...@@ -40,13 +38,13 @@ func NewWidget() *Widget {
fileDialog.OnFileSelected(func(ca string) { fileDialog.OnFileSelected(func(ca string) {
fileDialog.Hide() fileDialog.Hide()
caDest := home + "ca.pem" caDest := home + config.CAFile
_ = copyCA(ca, caDest) _ = copyCA(ca, caDest)
err := user.Register( err := user.Register(
home+"ca.pem", caDest,
home+"cert.pem", home+config.CertFile,
home+"key.pem", home+config.KeyFile,
hostField.Text(), hostField.Text(),
passwordField.Text(), passwordField.Text(),
"", "", "", emailField.Text(), 2048, "", "", "", emailField.Text(), 2048,
...@@ -54,7 +52,8 @@ func NewWidget() *Widget { ...@@ -54,7 +52,8 @@ func NewWidget() *Widget {
if err != nil { if err != nil {
feedbackLabel.SetText(err.Error()) feedbackLabel.SetText(err.Error())
} else { } else {
feedbackLabel.SetText("Registration done! Please check your mails.") conf.Platform = hostField.Text()
config.Save(*conf)
} }
form.SetDisabled(false) form.SetDisabled(false)
}) })
...@@ -67,20 +66,6 @@ func NewWidget() *Widget { ...@@ -67,20 +66,6 @@ func NewWidget() *Widget {
return &Widget{W: form} 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 { func copyCA(from string, to string) error {
if from == to { if from == to {
return nil return nil
......
Supports Markdown
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