translate_exec.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. Copyright 2016 The Rook Authors. All rights reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package exec
  14. import (
  15. "time"
  16. )
  17. // TranslateCommandExecutor is an exec.Executor that translates every command before executing it
  18. // This is useful to run the commands in a job with `kubectl run ...` when running the operator outside
  19. // of Kubernetes and need to run tools that require running inside the cluster.
  20. type TranslateCommandExecutor struct {
  21. // Executor is probably a exec.CommandExecutor that will run the translated commands
  22. Executor Executor
  23. // Translator translates every command before running it
  24. Translator func(command string, arg ...string) (string, []string)
  25. }
  26. // ExecuteCommand starts a process and wait for its completion
  27. func (e *TranslateCommandExecutor) ExecuteCommand(command string, arg ...string) error {
  28. transCommand, transArgs := e.Translator(command, arg...)
  29. return e.Executor.ExecuteCommand(transCommand, transArgs...)
  30. }
  31. // ExecuteCommandWithStdin starts a process, provides stdin and wait for its completion with timeout.
  32. func (e *TranslateCommandExecutor) ExecuteCommandWithStdin(timeout time.Duration, command string, stdin *string, arg ...string) error {
  33. transCommand, transArgs := e.Translator(command, arg...)
  34. return e.Executor.ExecuteCommandWithStdin(timeout, transCommand, stdin, transArgs...)
  35. }
  36. // ExecuteCommandWithEnv starts a process with an env variable and wait for its completion
  37. func (e *TranslateCommandExecutor) ExecuteCommandWithEnv(env []string, command string, arg ...string) error {
  38. transCommand, transArgs := e.Translator(command, arg...)
  39. return e.Executor.ExecuteCommandWithEnv(env, transCommand, transArgs...)
  40. }
  41. // ExecuteCommandWithOutput starts a process and wait for its completion
  42. func (e *TranslateCommandExecutor) ExecuteCommandWithOutput(command string, arg ...string) (string, error) {
  43. transCommand, transArgs := e.Translator(command, arg...)
  44. return e.Executor.ExecuteCommandWithOutput(transCommand, transArgs...)
  45. }
  46. // ExecuteCommandWithCombinedOutput starts a process and returns its stdout and stderr combined.
  47. func (e *TranslateCommandExecutor) ExecuteCommandWithCombinedOutput(command string, arg ...string) (string, error) {
  48. transCommand, transArgs := e.Translator(command, arg...)
  49. return e.Executor.ExecuteCommandWithCombinedOutput(transCommand, transArgs...)
  50. }
  51. // ExecuteCommandWithTimeout starts a process and wait for its completion with timeout.
  52. func (e *TranslateCommandExecutor) ExecuteCommandWithTimeout(timeout time.Duration, command string, arg ...string) (string, error) {
  53. transCommand, transArgs := e.Translator(command, arg...)
  54. return e.Executor.ExecuteCommandWithTimeout(timeout, transCommand, transArgs...)
  55. }