Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
mpcs
dfss
Commits
5987d422
Commit
5987d422
authored
Mar 24, 2016
by
Loïck Bonniot
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[gui] Add configuration package
parent
ba93953b
Pipeline
#396
passed with stage
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
99 additions
and
25 deletions
+99
-25
gui/config/config.go
gui/config/config.go
+83
-0
gui/main.go
gui/main.go
+7
-1
gui/userform/userform.go
gui/userform/userform.go
+9
-24
No files found.
gui/config/config.go
0 → 100644
View file @
5987d422
// 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
}
gui/main.go
View file @
5987d422
...
...
@@ -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
)
...
...
gui/userform/userform.go
View file @
5987d422
...
...
@@ -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
:=
g
etHomeDir
()
home
:=
config
.
G
etHomeDir
()
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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment