-
Notifications
You must be signed in to change notification settings - Fork 15
Open
Labels
Description
Describe the feature request
The implementation code for command/alternate-command execution existing in plugin(sync_plugin.go) and ucp(ucp.go) package should be refactored and avoid duplicate code.
const (
// customCommandName is the name of the command expected to be implemented
// by the CLI should there be a need to discover and alternative invocation
// method
customCommandName string = "_custom_command"
)
// cmdOptions specifies the command options
type cmdOptions struct {
outWriter io.Writer
errWriter io.Writer
}
type CommandOptions func(o *cmdOptions)
// WithOutputWriter specifies the CommandOption for configuring Stdout
func WithOutputWriter(outWriter io.Writer) CommandOptions {
return func(o *cmdOptions) {
o.outWriter = outWriter
}
}
// WithErrorWriter specifies the CommandOption for configuring Stderr
func WithErrorWriter(errWriter io.Writer) CommandOptions {
return func(o *cmdOptions) {
o.errWriter = errWriter
}
}
// WithNoStdout specifies to ignore stdout
func WithNoStdout() CommandOptions {
return func(o *cmdOptions) {
o.outWriter = io.Discard
}
}
// WithNoStderr specifies to ignore stderr
func WithNoStderr() CommandOptions {
return func(o *cmdOptions) {
o.errWriter = io.Discard
}
}
func runCommand(commandPath string, args []string, opts *cmdOptions) (bytes.Buffer, bytes.Buffer, error) {
command := exec.Command(commandPath, args...)
var stderr bytes.Buffer
var stdout bytes.Buffer
wout := io.MultiWriter(&stdout, os.Stdout)
werr := io.MultiWriter(&stderr, os.Stderr)
if opts.outWriter != nil {
wout = io.MultiWriter(&stdout, opts.outWriter)
}
if opts.errWriter != nil {
werr = io.MultiWriter(&stderr, opts.errWriter)
}
command.Stdout = wout
command.Stderr = werr
return stdout, stderr, command.Run()
}
Describe alternative(s) you've considered
Additional context
Reference: #106 (comment)
Reactions are currently unavailable