1
0
mirror of https://github.com/StackExchange/dnscontrol.git synced 2024-05-11 05:55:12 +00:00

CHORE: Vendor go-powershell (#2837)

This commit is contained in:
Tom Limoncelli
2024-02-15 13:46:32 -05:00
committed by GitHub
parent dbbc9e52a9
commit bb3d191efb
17 changed files with 647 additions and 7 deletions

View File

@ -0,0 +1,38 @@
// Copyright (c) 2017 Gorillalabs. All rights reserved.
package backend
import (
"io"
"os/exec"
"github.com/juju/errors"
)
type Local struct{}
func (b *Local) StartProcess(cmd string, args ...string) (Waiter, io.Writer, io.Reader, io.Reader, error) {
command := exec.Command(cmd, args...)
stdin, err := command.StdinPipe()
if err != nil {
return nil, nil, nil, nil, errors.Annotate(err, "Could not get hold of the PowerShell's stdin stream")
}
stdout, err := command.StdoutPipe()
if err != nil {
return nil, nil, nil, nil, errors.Annotate(err, "Could not get hold of the PowerShell's stdout stream")
}
stderr, err := command.StderrPipe()
if err != nil {
return nil, nil, nil, nil, errors.Annotate(err, "Could not get hold of the PowerShell's stderr stream")
}
err = command.Start()
if err != nil {
return nil, nil, nil, nil, errors.Annotate(err, "Could not spawn PowerShell process")
}
return command, stdin, stdout, stderr, nil
}

View File

@ -0,0 +1,69 @@
// Copyright (c) 2017 Gorillalabs. All rights reserved.
package backend
import (
"fmt"
"io"
"regexp"
"strings"
"github.com/juju/errors"
)
// sshSession exists so we don't create a hard dependency on crypto/ssh.
type sshSession interface {
Waiter
StdinPipe() (io.WriteCloser, error)
StdoutPipe() (io.Reader, error)
StderrPipe() (io.Reader, error)
Start(string) error
}
type SSH struct {
Session sshSession
}
func (b *SSH) StartProcess(cmd string, args ...string) (Waiter, io.Writer, io.Reader, io.Reader, error) {
stdin, err := b.Session.StdinPipe()
if err != nil {
return nil, nil, nil, nil, errors.Annotate(err, "Could not get hold of the SSH session's stdin stream")
}
stdout, err := b.Session.StdoutPipe()
if err != nil {
return nil, nil, nil, nil, errors.Annotate(err, "Could not get hold of the SSH session's stdout stream")
}
stderr, err := b.Session.StderrPipe()
if err != nil {
return nil, nil, nil, nil, errors.Annotate(err, "Could not get hold of the SSH session's stderr stream")
}
err = b.Session.Start(b.createCmd(cmd, args))
if err != nil {
return nil, nil, nil, nil, errors.Annotate(err, "Could not spawn process via SSH")
}
return b.Session, stdin, stdout, stderr, nil
}
func (b *SSH) createCmd(cmd string, args []string) string {
parts := []string{cmd}
simple := regexp.MustCompile(`^[a-z0-9_/.~+-]+$`)
for _, arg := range args {
if !simple.MatchString(arg) {
arg = b.quote(arg)
}
parts = append(parts, arg)
}
return strings.Join(parts, " ")
}
func (b *SSH) quote(s string) string {
return fmt.Sprintf(`"%s"`, s)
}

View File

@ -0,0 +1,13 @@
// Copyright (c) 2017 Gorillalabs. All rights reserved.
package backend
import "io"
type Waiter interface {
Wait() error
}
type Starter interface {
StartProcess(cmd string, args ...string) (Waiter, io.Writer, io.Reader, io.Reader, error)
}