-
Notifications
You must be signed in to change notification settings - Fork 11
/
app.go
90 lines (69 loc) · 2.36 KB
/
app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package backend
import (
"errors"
"fmt"
"path/filepath"
"github.com/adrg/xdg"
"go.uber.org/zap"
)
var (
ErrBackendNotStarted = errors.New("the application backend is not started")
ErrAppIsNotInitialised = errors.New("the application has not been initialised")
)
func (h *Handler) IsAppInitialised() (bool, error) {
isAppInit, err := h.isAppInitialised()
if err != nil {
h.log.Error("Could not verify the application is initialized", zap.Error(err))
return false, fmt.Errorf("could not verify the application is initialized: %w", err)
}
return isAppInit, nil
}
// isAppInitialised abstract the application initialization state verification,
// so we don't have to rely on the backend API to start the program.
func (h *Handler) isAppInitialised() (bool, error) {
return h.configLoader.IsConfigInitialised()
}
type InitialiseAppRequest struct {
VegaHome string `json:"vegaHome"`
}
func (h *Handler) InitialiseApp(req InitialiseAppRequest) error {
h.log.Debug("Entering InitialiseApp")
defer h.log.Debug("Leaving InitialiseApp")
cfg, err := h.appConfig()
if err != nil {
return err
}
cfg.VegaHome = req.VegaHome
if err := h.configLoader.SaveConfig(cfg); err != nil {
h.log.Error("Could not save the application configuration", zap.Error(err))
return fmt.Errorf("could not save the application configuration: %w", err)
}
if err := h.configLoader.MarkOnBoardingAsDone(); err != nil {
return fmt.Errorf("could not mark the on-boarding as done: %w", err)
}
h.appInitialised.Store(true)
if err := h.reloadBackendComponentsFromConfig(); err != nil {
h.log.Error("Could not reload the backend components during the application initialisation", zap.Error(err))
return fmt.Errorf("could not reload the backend components during the application initialisation: %w", err)
}
return nil
}
func (h *Handler) SuggestFairgroundFolder() string {
h.log.Debug("Entering SuggestFairgroundFolder")
defer h.log.Debug("Leaving SuggestFairgroundFolder")
return filepath.Join(xdg.Home, "Vega", "Fairground")
}
func (h *Handler) ensureBackendStarted() error {
if !h.backendStarted.Load() {
h.log.Error("The application backend is not started")
return ErrBackendNotStarted
}
return nil
}
func (h *Handler) ensureAppIsInitialised() error {
if !h.appInitialised.Load() {
h.log.Error("The application is not initialised")
return ErrAppIsNotInitialised
}
return nil
}