/
proxy.go
149 lines (115 loc) · 3.39 KB
/
proxy.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
package proxyexecution
import (
"io/ioutil"
"os"
"os/exec"
"strings"
"github.com/Oppodelldog/droxy/logger"
"github.com/Oppodelldog/droxy/config"
"github.com/Oppodelldog/droxy/dockercommand"
"github.com/Oppodelldog/droxy/logging"
)
const errorPreparingDockerCall = 900
type (
// ConfigLoader loads configuration.
ConfigLoader interface {
Load() config.Configuration
}
// Builder builds a executable command object.
CommandBuilder interface {
BuildCommandFromConfig(commandDef config.CommandDefinition) (*exec.Cmd, error)
}
// CommandRunner runs a command.
CommandRunner interface {
RunCommand(cmd *exec.Cmd) error
}
// CommandResultHandler handles the result of an executed command.
CommandResultHandler interface {
HandleCommandResult(*exec.Cmd, error) int
}
// ExecutableNameParser parsed the name of the current executed file from cli arguments.
ExecutableNameParser interface {
ParseCommandNameFromCommandLine() string
}
)
func ExecuteDroxyCommand(args []string) int {
dockerRunCommandBuilder, err := dockercommand.NewBuilder()
if err != nil {
logger.Errorf("error preparing docker call: %v", err)
return errorPreparingDockerCall
}
configLoader := config.NewLoader()
commandResultHandler := newResultHandler()
commandRunner := newCommandRunner()
executableNameParser := newExecutableNameParser()
return executeCommand(
args,
dockerRunCommandBuilder,
configLoader,
commandResultHandler,
commandRunner,
executableNameParser,
)
}
// executeCommand executes a proxy command.
func executeCommand(
args []string,
commandBuilder CommandBuilder,
configLoader ConfigLoader,
commandResultHandler CommandResultHandler,
commandRunner CommandRunner,
executableNameParser ExecutableNameParser,
) int {
cfg := configLoader.Load()
cfg.Logging = true
closeLogger := enableLogging(&cfg)
defer closeLogger()
logger.Infof("configuration load from: '%s'", cfg.GetConfigurationFilePath())
logger.Info()
logger.Infof("environment variables:")
for _, envVar := range os.Environ() {
logger.Info(envVar)
}
logger.Info("----------------------------------------------------------------------")
logger.Infof("origin arguments:")
for _, arg := range args {
logger.Info(arg)
}
logger.Info("----------------------------------------------------------------------")
commandName := executableNameParser.ParseCommandNameFromCommandLine()
cmdDef, err := cfg.FindCommandByName(commandName)
if err != nil {
logger.Errorf("cannot find command definition for '%s', but got: %v", commandName, err)
}
cmd, err := commandBuilder.BuildCommandFromConfig(cmdDef)
if err != nil {
logger.Errorf("error preparing docker call for '%s': %v", commandName, err)
return errorPreparingDockerCall
}
logger.Infof("calling docker to run '%s'", commandName)
logger.Infof(strings.Join(cmd.Args, " "))
err = commandRunner.RunCommand(cmd)
exitCode := commandResultHandler.HandleCommandResult(cmd, err)
return exitCode
}
func enableLogging(cfg *config.Configuration) (f func()) {
f = func() {}
if !cfg.Logging {
logger.SetOutput(ioutil.Discard)
return
}
logfileWriter, err := logging.GetLogWriter(cfg)
if err != nil {
// no chance to log error output since running docker process has priority before logging
logger.SetOutput(ioutil.Discard)
return
}
logger.SetOutput(logfileWriter)
f = func() {
err := logfileWriter.Close()
if err != nil {
logger.Error(err)
}
}
return
}