/
start.go
177 lines (136 loc) · 3.53 KB
/
start.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package app
import (
"errors"
"os"
"github.com/asannikov/jumper/app/config"
"github.com/asannikov/jumper/app/dialog"
)
type projectConfig struct {
projectpath string
projectname string
selectProjectIsNew bool
}
func (pc *projectConfig) GetProjectName() string {
return pc.projectname
}
func (pc *projectConfig) GetProjectPath() string {
return pc.projectpath
}
func (pc *projectConfig) SetProjectName(v string) {
pc.projectname = v
}
func (pc *projectConfig) SetProjectPath(v string) {
pc.projectpath = v
}
type definePathsFileSystem interface {
GetUserDirectory() (string, error)
}
func definePaths(cfg *config.Config, fs definePathsFileSystem) (err error) {
userDir, err := fs.GetUserDirectory()
cfg.UserFile = userDir + string(os.PathSeparator) + ".jumper.json"
return err
}
type runDialogFileSystem interface {
GoToProjectPath(string) error
}
func runDialog(pc *projectConfig, cfg *config.Config, DLG *dialog.Dialog, fs runDialogFileSystem, pl []string, currentDir string) (err error) {
if cfg.ProjectConfgFileFound() == false && len(pl) > 0 {
var index int
var projectName string
if index, projectName, err = DLG.SelectProject(pl); err != nil {
return err
}
pc.SetProjectName(projectName)
if index == -1 { // add new project
pc.SetProjectPath(currentDir)
err = DLG.CallAddProjectDialog(pc)
pc.selectProjectIsNew = true
} else {
cfg.FindProjectPathInJSON(pc)
}
if err != nil {
return err
}
}
if pc.GetProjectPath() == "" {
pc.selectProjectIsNew = true
err = DLG.CallAddProjectDialog(pc)
}
return err
}
type seekPathFileSystem interface {
FileExists(string) (bool, error)
DirExists(string) (bool, error)
SaveConfigFile(interface{}, string) error
ReadConfigFile(string, interface{}) error
GoToProjectPath(string) error
GetUserDirectory() (string, error)
GetWd() (string, error)
}
func defineDockerCommand(cfg *config.Config, DLG *dialog.Dialog) (err error) {
command := cfg.GetDockerCommand()
if command == "" {
if command, err = DLG.DockerService(); err != nil {
return err
}
cfg.SetDockerCommand(command)
}
return nil
}
func loadGlobalConfig(cfg *config.Config, DLG *dialog.Dialog, fs seekPathFileSystem) (err error) {
if err = definePaths(cfg, fs); err != nil {
return err
}
if err = cfg.LoadConfig(false); err != nil {
return err
}
return nil
}
func loadProjectConfig(cfg *config.Config, fs seekPathFileSystem) (err error) {
var currentDir string
var status bool
if currentDir, err = fs.GetWd(); err != nil {
return err
}
status, _ = cfg.LoadProjectConfig()
if status && cfg.GetProjectName() != "" {
cfg.SetProjectPath(currentDir)
}
return nil
}
func seekPath(cfg *config.Config, DLG *dialog.Dialog, fs seekPathFileSystem, seekProject bool) error {
var currentDir string
var err error
if !seekProject {
return nil
}
if err = loadGlobalConfig(cfg, DLG, fs); err != nil {
return err
}
if currentDir, err = fs.GetWd(); err != nil {
return err
}
pl := cfg.GetProjectNameList()
pc := &projectConfig{
projectpath: cfg.GetProjectPath(),
projectname: cfg.GetProjectName(),
}
if err = runDialog(pc, cfg, DLG, fs, pl, currentDir); err != nil {
return err
}
cfg.SetProjectName(pc.GetProjectName())
cfg.SetProjectPath(pc.GetProjectPath())
if pc.selectProjectIsNew == true {
err = cfg.AddProjectConfigFile()
}
if err != nil {
return err
}
if pc.GetProjectPath() == "" {
return errors.New("No project found")
}
if err = fs.GoToProjectPath(pc.GetProjectPath()); err != nil {
return err
}
return cfg.LoadConfig(seekProject)
}