This repository has been archived by the owner on May 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
executor.go
150 lines (121 loc) · 3.77 KB
/
executor.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
package internal
import (
"path/filepath"
"github.com/pieterclaerhout/go-james/internal/builder"
"github.com/pieterclaerhout/go-james/internal/cleaner"
"github.com/pieterclaerhout/go-james/internal/common"
"github.com/pieterclaerhout/go-james/internal/config"
"github.com/pieterclaerhout/go-james/internal/creator"
"github.com/pieterclaerhout/go-james/internal/debugger"
"github.com/pieterclaerhout/go-james/internal/installer"
"github.com/pieterclaerhout/go-james/internal/packager"
"github.com/pieterclaerhout/go-james/internal/runner"
"github.com/pieterclaerhout/go-james/internal/tester"
"github.com/pieterclaerhout/go-james/internal/uninstaller"
"github.com/pieterclaerhout/go-log"
)
// Executor is used to execute the subcommands
type Executor struct {
common.Logging
Path string
}
// NewExecutor returns a new Executor instance
//
// The path to the project is changed to the absolute path is it exists
func NewExecutor(path string) Executor {
if absPath, err := filepath.Abs(path); err == nil {
path = absPath
}
return Executor{
Path: path,
}
}
// DoBuild performs a build of the project
func (executor Executor) DoBuild(outputPath string, goos string, goarch string, verbose bool) int {
return executor.runSubcommand(builder.Builder{
OutputPath: outputPath,
GOOS: goos,
GOARCH: goarch,
Verbose: verbose,
}, true)
}
// DoPackage performs a package of the project
func (executor Executor) DoPackage(verbose bool, concurrency int) int {
return executor.runSubcommand(packager.Packager{
Verbose: verbose,
Concurrency: concurrency,
}, true)
}
// DoClean performs a clean of the project
func (executor Executor) DoClean() int {
return executor.runSubcommand(cleaner.Cleaner{}, true)
}
// DoTest performs the tests defined in the project
func (executor Executor) DoTest() int {
return executor.runSubcommand(tester.Tester{}, true)
}
// DoInstall builds the executable and installs it in $GOPATH/bin
func (executor Executor) DoInstall(verbose bool) int {
return executor.runSubcommand(installer.Installer{
Verbose: verbose,
}, true)
}
// DoUninstall removes the executable from $GOPATH/bin
func (executor Executor) DoUninstall() int {
return executor.runSubcommand(uninstaller.Uninstaller{}, true)
}
// DoRun runs the project and passes the arguments to the command
func (executor Executor) DoRun(args []string) int {
return executor.runSubcommand(runner.Runner{
Args: args,
}, true)
}
// DoDebug debugs the project and passes the arguments to the command
func (executor Executor) DoDebug(args []string) int {
return executor.runSubcommand(debugger.Debugger{
Args: args,
}, true)
}
// DoInit initializes a project in an existing directory
func (executor Executor) DoInit() int {
return executor.runSubcommand(creator.Creator{
Mode: creator.InitProject,
}, false)
}
// DoNew initializes a project in an existing directory
func (executor Executor) DoNew(path string, packageName string, name string, description string) int {
return executor.runSubcommand(creator.Creator{
Mode: creator.NewProject,
Path: path,
Package: packageName,
Name: name,
Description: description,
}, false)
}
func (executor Executor) runSubcommand(subcommand Subcommand, parseConfig bool) int {
var cfg config.Config
var err error
if parseConfig {
cfg, err = config.NewConfigFromDir(executor.Path)
if err != nil {
executor.LogErrorInDebugMode(err)
return 1
}
}
project := common.NewProject(executor.Path)
if subcommand.RequiresBuild() {
b := builder.Builder{
Verbose: false,
}
if err := b.Execute(project, cfg); err != nil {
executor.LogErrorInDebugMode(err)
return 1
}
}
log.DebugDump(subcommand, "subcommand")
if err := subcommand.Execute(project, cfg); err != nil {
log.Error(err)
return 1
}
return 0
}