-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
157 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package mexos | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
// Implements nanobox-io's ssh.Client interface, but runs commands locally. | ||
// This is used for kubernetes DIND or other local testing. | ||
type sshLocal struct { | ||
cmd *exec.Cmd | ||
} | ||
|
||
// Output returns the output of the command run on the remote host. | ||
func (s *sshLocal) Output(command string) (string, error) { | ||
cmd := exec.Command("sh", "-c", command) | ||
out, err := cmd.CombinedOutput() | ||
return string(out), err | ||
} | ||
|
||
// Shell requests a shell from the remote. If an arg is passed, it tries to | ||
// exec them on the server. | ||
func (s *sshLocal) Shell(args ...string) error { | ||
cmd := exec.Command("sh", "-c", strings.Join(args, " ")) | ||
return cmd.Run() | ||
} | ||
|
||
// Start starts the specified command without waiting for it to finish. You | ||
// have to call the Wait function for that. | ||
// | ||
// The first two io.ReadCloser are the standard output and the standard | ||
// error of the executing command respectively. The returned error follows | ||
// the same logic as in the exec.Cmd.Start function. | ||
func (s *sshLocal) Start(command string) (io.ReadCloser, io.ReadCloser, error) { | ||
cmd := exec.Command("sh", "-c", command) | ||
sout, err := cmd.StdoutPipe() | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
errout, err := cmd.StderrPipe() | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
err = cmd.Start() | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
s.cmd = cmd | ||
return sout, errout, nil | ||
} | ||
|
||
// Wait waits for the command started by the Start function to exit. The | ||
// returned error follows the same logic as in the exec.Cmd.Wait function. | ||
func (s *sshLocal) Wait() error { | ||
if s.cmd == nil { | ||
return fmt.Errorf("no command started") | ||
} | ||
err := s.cmd.Wait() | ||
s.cmd = nil | ||
return err | ||
} |