-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate_exec.go
77 lines (62 loc) · 3.51 KB
/
translate_exec.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
/*
Copyright 2016 The Rook Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package exec
import (
"time"
)
// TranslateCommandExecutor is an exec.Executor that translates every command before executing it
// This is useful to run the commands in a job with `kubectl run ...` when running the operator outside
// of Kubernetes and need to run tools that require running inside the cluster.
type TranslateCommandExecutor struct {
// Executor is probably a exec.CommandExecutor that will run the translated commands
Executor Executor
// Translator translates every command before running it
Translator func(command string, arg ...string) (string, []string)
}
// ExecuteCommand starts a process and wait for its completion
func (e *TranslateCommandExecutor) ExecuteCommand(command string, arg ...string) error {
transCommand, transArgs := e.Translator(command, arg...)
return e.Executor.ExecuteCommand(transCommand, transArgs...)
}
// ExecuteCommandWithEnv starts a process with an env variable and wait for its completion
func (e *TranslateCommandExecutor) ExecuteCommandWithEnv(env []string, command string, arg ...string) error {
transCommand, transArgs := e.Translator(command, arg...)
return e.Executor.ExecuteCommandWithEnv(env, transCommand, transArgs...)
}
// ExecuteCommandWithOutput starts a process and wait for its completion
func (e *TranslateCommandExecutor) ExecuteCommandWithOutput(command string, arg ...string) (string, error) {
transCommand, transArgs := e.Translator(command, arg...)
return e.Executor.ExecuteCommandWithOutput(transCommand, transArgs...)
}
// ExecuteCommandWithCombinedOutput starts a process and returns its stdout and stderr combined.
func (e *TranslateCommandExecutor) ExecuteCommandWithCombinedOutput(command string, arg ...string) (string, error) {
transCommand, transArgs := e.Translator(command, arg...)
return e.Executor.ExecuteCommandWithCombinedOutput(transCommand, transArgs...)
}
// ExecuteCommandWithOutputFile starts a process and saves output to file
func (e *TranslateCommandExecutor) ExecuteCommandWithOutputFile(command, outfileArg string, arg ...string) (string, error) {
transCommand, transArgs := e.Translator(command, arg...)
return e.Executor.ExecuteCommandWithOutputFile(transCommand, outfileArg, transArgs...)
}
// ExecuteCommandWithOutputFileTimeout is the same as ExecuteCommandWithOutputFile but with a timeout limit.
func (e *TranslateCommandExecutor) ExecuteCommandWithOutputFileTimeout(
timeout time.Duration,
command, outfileArg string, arg ...string) (string, error) {
transCommand, transArgs := e.Translator(command, arg...)
return e.Executor.ExecuteCommandWithOutputFileTimeout(timeout, transCommand, outfileArg, transArgs...)
}
// ExecuteCommandWithTimeout starts a process and wait for its completion with timeout.
func (e *TranslateCommandExecutor) ExecuteCommandWithTimeout(timeout time.Duration, command string, arg ...string) (string, error) {
transCommand, transArgs := e.Translator(command, arg...)
return e.Executor.ExecuteCommandWithTimeout(timeout, transCommand, transArgs...)
}