|
| 1 | +// Copyright 2019 Yunion |
| 2 | +// |
| 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 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package driver |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "io" |
| 20 | + "io/ioutil" |
| 21 | + |
| 22 | + "yunion.io/x/onecloud/pkg/util/procutils" |
| 23 | +) |
| 24 | + |
| 25 | +type IFsutilExecDriver interface { |
| 26 | + Run(name string, args ...string) error |
| 27 | + Exec(name string, args ...string) ([]byte, error) |
| 28 | + ExecInputWait(name string, args []string, input []string) (int, string, string, error) |
| 29 | +} |
| 30 | + |
| 31 | +type SProcDriver struct { |
| 32 | +} |
| 33 | + |
| 34 | +func NewProcDriver() IFsutilExecDriver { |
| 35 | + return new(SProcDriver) |
| 36 | +} |
| 37 | + |
| 38 | +func (*SProcDriver) Exec(name string, args ...string) ([]byte, error) { |
| 39 | + return procutils.NewCommand(name, args...).Output() |
| 40 | +} |
| 41 | + |
| 42 | +func (*SProcDriver) Run(name string, args ...string) error { |
| 43 | + return procutils.NewCommand(name, args...).Run() |
| 44 | +} |
| 45 | + |
| 46 | +func (*SProcDriver) ExecInputWait(name string, args []string, input []string) (int, string, string, error) { |
| 47 | + proc := procutils.NewCommand(name, args...) |
| 48 | + stdin, err := proc.StdinPipe() |
| 49 | + if err != nil { |
| 50 | + return -1, "", "", err |
| 51 | + } |
| 52 | + defer stdin.Close() |
| 53 | + |
| 54 | + outb, err := proc.StdoutPipe() |
| 55 | + if err != nil { |
| 56 | + return -1, "", "", err |
| 57 | + } |
| 58 | + defer outb.Close() |
| 59 | + |
| 60 | + errb, err := proc.StderrPipe() |
| 61 | + if err != nil { |
| 62 | + return -1, "", "", err |
| 63 | + } |
| 64 | + defer errb.Close() |
| 65 | + if err := proc.Start(); err != nil { |
| 66 | + return -1, "", "", err |
| 67 | + } |
| 68 | + for _, s := range input { |
| 69 | + io.WriteString(stdin, fmt.Sprintf("%s\n", s)) |
| 70 | + } |
| 71 | + stdoutPut, err := ioutil.ReadAll(outb) |
| 72 | + if err != nil { |
| 73 | + return -1, "", "", err |
| 74 | + } |
| 75 | + stderrOutPut, err := ioutil.ReadAll(errb) |
| 76 | + if err != nil { |
| 77 | + return -1, "", "", err |
| 78 | + } |
| 79 | + if err = proc.Wait(); err != nil { |
| 80 | + if status, succ := proc.GetExitStatus(err); succ { |
| 81 | + return status, string(stdoutPut), string(stderrOutPut), err |
| 82 | + } else { |
| 83 | + return 0, string(stdoutPut), string(stderrOutPut), err |
| 84 | + } |
| 85 | + } |
| 86 | + return 0, string(stdoutPut), string(stderrOutPut), nil |
| 87 | +} |
0 commit comments