|
| 1 | +/** |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package cmdutil |
| 16 | + |
| 17 | +import ( |
| 18 | + "bufio" |
| 19 | + "context" |
| 20 | + "errors" |
| 21 | + "os/exec" |
| 22 | + "strings" |
| 23 | + "sync/atomic" |
| 24 | +) |
| 25 | + |
| 26 | +// Command is a wrapper for executing cmd command |
| 27 | +type Command interface { |
| 28 | + // Exec execute the command |
| 29 | + Exec() error |
| 30 | + |
| 31 | + // GetCommand return the command |
| 32 | + GetCommand() string |
| 33 | + |
| 34 | + // GetArgs return the args |
| 35 | + GetArgs() []string |
| 36 | + |
| 37 | + // Output return the output of command |
| 38 | + Output() <-chan string |
| 39 | + |
| 40 | + // Wait wait for command to finish |
| 41 | + // is stderr is not empty, send an error |
| 42 | + Wait() <-chan error |
| 43 | + |
| 44 | + // GetExitState return the exit state of command |
| 45 | + GetExitError() error |
| 46 | + |
| 47 | + // Kill the command |
| 48 | + Kill() error |
| 49 | + |
| 50 | + // String return the string representation of command |
| 51 | + String() string |
| 52 | +} |
| 53 | + |
| 54 | +// BuildCommand return a new Command |
| 55 | +func BuildCommand( |
| 56 | + ctx context.Context, |
| 57 | + cmd string, args ...string) Command { |
| 58 | + return BuildCommandWithWorkDir(ctx, "", cmd, args...) |
| 59 | +} |
| 60 | + |
| 61 | +func BuildCommandWithWorkDir( |
| 62 | + ctx context.Context, |
| 63 | + workdir string, |
| 64 | + cmd string, |
| 65 | + args ...string) Command { |
| 66 | + cancableContext, cancelFunc := context.WithCancel(ctx) |
| 67 | + return &command{ |
| 68 | + ctx: cancableContext, |
| 69 | + cmd: cmd, |
| 70 | + workdir: workdir, |
| 71 | + args: args, |
| 72 | + output: make(chan string, 1), |
| 73 | + completeSignal: make(chan error, 1), |
| 74 | + cancel: cancelFunc, |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +type command struct { |
| 79 | + started *atomic.Bool |
| 80 | + ctx context.Context |
| 81 | + workdir string |
| 82 | + cmd string |
| 83 | + args []string |
| 84 | + |
| 85 | + cancel context.CancelFunc |
| 86 | + output chan string |
| 87 | + completeSignal chan error |
| 88 | + exitState error |
| 89 | +} |
| 90 | + |
| 91 | +func (c *command) String() string { |
| 92 | + return c.cmd + " " + strings.Join(c.args, " ") |
| 93 | +} |
| 94 | + |
| 95 | +func (c *command) GetCommand() string { |
| 96 | + return c.cmd |
| 97 | +} |
| 98 | + |
| 99 | +func (c *command) GetArgs() []string { |
| 100 | + return c.args |
| 101 | +} |
| 102 | + |
| 103 | +func (c *command) GetExitError() error { |
| 104 | + return c.exitState |
| 105 | +} |
| 106 | + |
| 107 | +func (c *command) Exec() error { |
| 108 | + execCmd := exec.CommandContext(c.ctx, c.cmd, c.args...) |
| 109 | + execCmd.Dir = c.workdir |
| 110 | + |
| 111 | + stdoutpipeline, err := execCmd.StdoutPipe() |
| 112 | + |
| 113 | + if err != nil { |
| 114 | + return err |
| 115 | + } |
| 116 | + |
| 117 | + stderrorPipeline, err := execCmd.StderrPipe() |
| 118 | + if err != nil { |
| 119 | + return err |
| 120 | + } |
| 121 | + |
| 122 | + closed := &atomic.Bool{} |
| 123 | + closed.Store(false) |
| 124 | + closeCompleteSignal := func(err error) { |
| 125 | + if closed.CompareAndSwap(false, true) { |
| 126 | + if err != nil { |
| 127 | + c.completeSignal <- err |
| 128 | + } |
| 129 | + close(c.completeSignal) |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + go func() { |
| 134 | + scanner := bufio.NewScanner(stdoutpipeline) |
| 135 | + for scanner.Scan() { |
| 136 | + c.output <- scanner.Text() |
| 137 | + } |
| 138 | + close(c.output) |
| 139 | + }() |
| 140 | + |
| 141 | + go func() { |
| 142 | + scanner := bufio.NewScanner(stderrorPipeline) |
| 143 | + sb := &strings.Builder{} |
| 144 | + for scanner.Scan() { |
| 145 | + sb.WriteString(scanner.Text()) |
| 146 | + sb.WriteString("\n") |
| 147 | + } |
| 148 | + |
| 149 | + if len(sb.String()) != 0 { |
| 150 | + closeCompleteSignal(errors.New(sb.String())) |
| 151 | + } |
| 152 | + }() |
| 153 | + |
| 154 | + if err := execCmd.Start(); err != nil { |
| 155 | + return err |
| 156 | + } |
| 157 | + |
| 158 | + go func() { |
| 159 | + c.exitState = execCmd.Wait() |
| 160 | + closeCompleteSignal(nil) |
| 161 | + }() |
| 162 | + |
| 163 | + return nil |
| 164 | +} |
| 165 | + |
| 166 | +func (c *command) Wait() <-chan error { |
| 167 | + return c.completeSignal |
| 168 | +} |
| 169 | + |
| 170 | +func (c *command) Kill() error { |
| 171 | + c.cancel() |
| 172 | + return nil |
| 173 | +} |
| 174 | + |
| 175 | +func (c *command) Output() <-chan string { |
| 176 | + return c.output |
| 177 | +} |
0 commit comments