-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from synalice/dev/sdtin
Merge dev/sdtin
- Loading branch information
Showing
10 changed files
with
122 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package container | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/docker/docker/api/types" | ||
|
||
"github.com/synalice/gobox/docker/controller" | ||
) | ||
|
||
// attach is basically an equivalent of `$ docker attach <container>` | ||
// It returns a connection to a container, allowing you to read from stdout and | ||
// write into stdin. | ||
func attach(controller *controller.Controller, containerID string) (types.HijackedResponse, error) { | ||
hijackedResponse, err := controller.Cli.ContainerAttach( | ||
context.Background(), | ||
containerID, | ||
types.ContainerAttachOptions{ | ||
Stream: true, | ||
Stdin: true, | ||
Stdout: true, | ||
Stderr: true, | ||
}, | ||
) | ||
if err != nil { | ||
return hijackedResponse, fmt.Errorf("error attaching to a container: %w", err) | ||
} | ||
|
||
return hijackedResponse, nil | ||
} |
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 |
---|---|---|
@@ -1,33 +1,29 @@ | ||
package container | ||
|
||
import ( | ||
"context" | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"time" | ||
|
||
"github.com/docker/docker/api/types" | ||
|
||
"github.com/synalice/gobox/docker/controller" | ||
) | ||
|
||
// GetLogs returns logs of a specific container | ||
func GetLogs(controller *controller.Controller, containerID string) (string, error) { | ||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
func GetLogs(controller *controller.Controller, container *Container) (string, error) { | ||
// GetLogs doesn't really need a controller, but I think it's better to | ||
// still require one to make the user API more unified. | ||
// | ||
// This line is given here only so that the compiler doesn't yell at | ||
// you because of an unused parameter. | ||
_ = controller | ||
|
||
r := container.Connection.Reader | ||
var buf bytes.Buffer | ||
|
||
reader, err := controller.Cli.ContainerLogs(ctx, containerID, types.ContainerLogsOptions{ | ||
ShowStdout: true, | ||
ShowStderr: true, | ||
}) | ||
_, err := io.Copy(&buf, r) | ||
if err != nil { | ||
return "", fmt.Errorf("error getting contaner's logs: %w", err) | ||
} | ||
|
||
buffer, err := io.ReadAll(reader) | ||
if err != nil && err != io.EOF { | ||
return "", fmt.Errorf("error while executing io.ReadAll(): %w", err) | ||
} | ||
|
||
return string(buffer), nil | ||
return buf.String(), nil | ||
} |
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