/
app.go
181 lines (143 loc) · 4 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
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
178
179
180
181
package app
import (
"errors"
"fmt"
"io"
"os"
"os/exec"
"strings"
"github.com/asannikov/jumper/app/bash"
"github.com/asannikov/jumper/app/command"
"github.com/asannikov/jumper/app/docker"
"github.com/asannikov/jumper/app/lib"
"github.com/docker/docker/api/types"
"github.com/asannikov/jumper/app/config"
"github.com/asannikov/jumper/app/dialog"
"github.com/urfave/cli/v2"
)
const confgFile = "jumper.json"
// JumperApp initializate app
func JumperApp(cli *cli.App) {
// Dialogs
DLG := dialog.InitDialogFunctions()
cfg := &config.Config{
ProjectFile: confgFile,
}
cfg.Init()
fs := &FileSystem{}
cfg.SetFileSystem(fs)
// Loading only global config
loadGlobalConfig(cfg, fs)
// Loading project config if exists
loadProjectConfig(cfg, fs)
// Define docker command
defineDockerCommand(cfg, &DLG)
opt := getOptions(cfg, &DLG)
opt.setInitFuntion(func(seekProject bool) string {
if err := seekPath(cfg, &DLG, fs, seekProject); err != nil {
fmt.Println(err)
os.Exit(1)
}
if seekProject == true {
currentDir, _ := fs.GetWd()
fmt.Printf("\nchanged user location to directory: %s\n\n", currentDir)
return currentDir
}
return ""
})
cli.Copyright = lib.GetCopyrightText(cfg)
cli.Commands = commandList(cfg, &DLG, opt)
}
type ioCli struct {
reader io.Reader
writer io.Writer
errWriter io.Writer
}
func (ic *ioCli) GetReader() io.Reader {
return ic.reader
}
func (ic *ioCli) GetWriter() io.Writer {
return ic.writer
}
func (ic *ioCli) GetErrWriter() io.Writer {
return ic.errWriter
}
type getOptionsConfig interface {
GetDockerCommand() string
GetProjectMainContainer() string
}
func getOptions(c getOptionsConfig, d commandListDialog) *commandOptions {
b := bash.Bash{}
getCommandLocationF := b.GetCommandLocation()
dck := docker.GetDockerInstance()
dockerDialog := getDockerStartDialog()
dockerDialog.setDialog(d)
dockerDialog.setDocker(dck)
dockerDialog.setDockerService(c.GetDockerCommand())
dockerStatus := false
if dockerAPIVersiongo, _ := dck.Stat(); dockerAPIVersiongo != "" {
dockerStatus = true
dck.InitClient()
}
opt := &commandOptions{}
opt.setCommandLocation(getCommandLocationF)
opt.setDockerStatus(dockerStatus)
opt.setStopContainers(dck.StopContainers())
opt.setExecCommand(func(eo command.ExecOptions, app *cli.App) error {
cmd := exec.Command(eo.GetCommand(), eo.GetArgs()...)
cmd.Stdin = app.Reader
cmd.Stdout = app.Writer
cmd.Stderr = app.ErrWriter
fmt.Printf("\ncommand: %s\n\n", eo.GetCommand()+" "+strings.Join(eo.GetArgs(), " "))
return cmd.Run()
})
opt.setDirExists(func(path string) (bool, error) {
if info, err := os.Stat(path); err == nil {
if info.IsDir() {
return true, nil
}
return false, fmt.Errorf("Path %s is a file ", path)
} else if os.IsNotExist(err) {
// path does *not* exist
return false, err
} else {
// Schrodinger: file may or may not exist. See err for details.
// Therefore, do *NOT* use !os.IsNotExist(err) to test for file existence
return false, err
}
})
opt.setMkdirAll(func(path string, fileMode os.FileMode) error {
return os.MkdirAll(path, fileMode)
})
opt.setDockerDialog(dockerDialog)
opt.setCopyTo(func(container string, sourcePath string, dstPath string) error {
return dck.CopyTo(container, sourcePath, dstPath)
})
opt.setNativeExec(func(eo command.ExecOptions, app *cli.App) (err error) {
ic := &ioCli{
reader: app.Reader,
writer: app.Writer,
errWriter: app.ErrWriter,
}
cnf := types.ExecConfig{
AttachStderr: true,
AttachStdin: true,
AttachStdout: true,
User: eo.GetUser(),
Tty: eo.GetTty(),
Cmd: append([]string{eo.GetCommand()}, eo.GetArgs()...),
WorkingDir: eo.GetWorkingDir(),
}
status, err := dck.Exec(c.GetProjectMainContainer(), &cnf, ic)
if err != nil {
return err
}
if status > 0 {
return errors.New("Error is occurred on exec function")
}
return nil
})
opt.setMagentoBin(command.CheckMagentoBin)
opt.setXdebugStatus(command.CheckXdebugStatus)
return opt
}