Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
Rename struct
Browse files Browse the repository at this point in the history
  • Loading branch information
duck8823 committed Dec 1, 2018
1 parent 56d9943 commit b702f41
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions domain/model/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,27 @@ type Docker interface {
Status() error
}

type dockerService struct {
type client struct {
moby Moby
}

// New returns instance of docker service
// New returns instance of docker client
func New() (Docker, error) {
cli, err := moby.NewClientWithOpts(moby.FromEnv)
if err != nil {
return nil, errors.WithStack(err)
}
return &dockerService{moby: cli}, nil
return &client{moby: cli}, nil
}

// Build a docker image.
func (s *dockerService) Build(ctx context.Context, file io.Reader, tag Tag, dockerfile Dockerfile) (Log, error) {
func (c *client) Build(ctx context.Context, file io.Reader, tag Tag, dockerfile Dockerfile) (Log, error) {
opts := types.ImageBuildOptions{
Tags: []string{tag.ToString()},
Dockerfile: dockerfile.ToString(),
Remove: true,
}
resp, err := s.moby.ImageBuild(ctx, file, opts)
resp, err := c.moby.ImageBuild(ctx, file, opts)
if err != nil {
return nil, errors.WithStack(err)
}
Expand All @@ -49,8 +49,8 @@ func (s *dockerService) Build(ctx context.Context, file io.Reader, tag Tag, dock
}

// Run docker container with command.
func (s *dockerService) Run(ctx context.Context, opts RuntimeOptions, tag Tag, cmd Command) (ContainerID, Log, error) {
con, err := s.moby.ContainerCreate(ctx, &container.Config{
func (c *client) Run(ctx context.Context, opts RuntimeOptions, tag Tag, cmd Command) (ContainerID, Log, error) {
con, err := c.moby.ContainerCreate(ctx, &container.Config{
Image: tag.ToString(),
Env: opts.Environments.ToArray(),
Volumes: opts.Volumes.ToMap(),
Expand All @@ -62,11 +62,11 @@ func (s *dockerService) Run(ctx context.Context, opts RuntimeOptions, tag Tag, c
return "", nil, errors.WithStack(err)
}

if err := s.moby.ContainerStart(ctx, con.ID, types.ContainerStartOptions{}); err != nil {
if err := c.moby.ContainerStart(ctx, con.ID, types.ContainerStartOptions{}); err != nil {
return ContainerID(con.ID), nil, errors.WithStack(err)
}

logs, err := s.moby.ContainerLogs(ctx, con.ID, types.ContainerLogsOptions{
logs, err := c.moby.ContainerLogs(ctx, con.ID, types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
Follow: true,
Expand All @@ -79,24 +79,24 @@ func (s *dockerService) Run(ctx context.Context, opts RuntimeOptions, tag Tag, c
}

// RemoveContainer remove docker container.
func (s *dockerService) RemoveContainer(ctx context.Context, conID ContainerID) error {
if err := s.moby.ContainerRemove(ctx, conID.ToString(), types.ContainerRemoveOptions{}); err != nil {
func (c *client) RemoveContainer(ctx context.Context, conID ContainerID) error {
if err := c.moby.ContainerRemove(ctx, conID.ToString(), types.ContainerRemoveOptions{}); err != nil {
return errors.WithStack(err)
}
return nil
}

// RemoveImage remove docker image.
func (s *dockerService) RemoveImage(ctx context.Context, tag Tag) error {
if _, err := s.moby.ImageRemove(ctx, tag.ToString(), types.ImageRemoveOptions{}); err != nil {
func (c *client) RemoveImage(ctx context.Context, tag Tag) error {
if _, err := c.moby.ImageRemove(ctx, tag.ToString(), types.ImageRemoveOptions{}); err != nil {
return errors.WithStack(err)
}
return nil
}

// ExitCode returns exit code specific container id.
func (s *dockerService) ExitCode(ctx context.Context, conID ContainerID) (ExitCode, error) {
body, err := s.moby.ContainerWait(ctx, conID.ToString(), container.WaitConditionNotRunning)
func (c *client) ExitCode(ctx context.Context, conID ContainerID) (ExitCode, error) {
body, err := c.moby.ContainerWait(ctx, conID.ToString(), container.WaitConditionNotRunning)
select {
case b := <-body:
return ExitCode(b.StatusCode), nil
Expand All @@ -106,8 +106,8 @@ func (s *dockerService) ExitCode(ctx context.Context, conID ContainerID) (ExitCo
}

// Status returns error of docker daemon status.
func (s *dockerService) Status() error {
if _, err := s.moby.Info(context.Background()); err != nil {
func (c *client) Status() error {
if _, err := c.moby.Info(context.Background()); err != nil {
return errors.Wrap(err, "Couldn't connect to Docker daemon.")
}
return nil
Expand Down

0 comments on commit b702f41

Please sign in to comment.