| 1 | /* |
| 2 | Copyright 2023 The Kubernetes Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package knftables |
| 18 | |
| 19 | import ( |
| 20 | "os/exec" |
| 21 | ) |
| 22 | |
| 23 | // execer is a mockable wrapper around os/exec. |
| 24 | type execer interface { |
| 25 | // LookPath wraps exec.LookPath |
| 26 | LookPath(file string) (string, error) |
| 27 | |
| 28 | // Run runs cmd as with cmd.Output(). If an error occurs, and the process outputs |
| 29 | // stderr, then that output will be returned in the error. |
| 30 | Run(cmd *exec.Cmd) (string, error) |
| 31 | } |
| 32 | |
| 33 | // realExec implements execer by actually using os/exec |
| 34 | type realExec struct{} |
| 35 | |
| 36 | // LookPath is part of execer |
| 37 | func (realExec) LookPath(file string) (string, error) { |
| 38 | return exec.LookPath(file) |
| 39 | } |
| 40 | |
| 41 | // Run is part of execer |
| 42 | func (realExec) Run(cmd *exec.Cmd) (string, error) { |
| 43 | out, err := cmd.Output() |
| 44 | if err != nil { |
| 45 | err = wrapError(err) |
| 46 | } |
| 47 | return string(out), err |
| 48 | } |