Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 0c3112e

Browse files
authored
Merge pull request #355 from vdemeester/create-service-and-ctx-package
Move Service and Context to their own package.
2 parents 981fb65 + b42d139 commit 0c3112e

File tree

17 files changed

+56
-47
lines changed

17 files changed

+56
-47
lines changed

cli/docker/app/commands.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package app
33
import (
44
"github.com/Sirupsen/logrus"
55
"github.com/docker/libcompose/cli/command"
6-
"github.com/docker/libcompose/docker"
76
"github.com/docker/libcompose/docker/client"
7+
"github.com/docker/libcompose/docker/ctx"
88
"github.com/urfave/cli"
99
)
1010

@@ -41,7 +41,7 @@ func DockerClientFlags() []cli.Flag {
4141
}
4242

4343
// Populate updates the specified docker context based on command line arguments and subcommands.
44-
func Populate(context *docker.Context, c *cli.Context) {
44+
func Populate(context *ctx.Context, c *cli.Context) {
4545
command.Populate(&context.Context, c)
4646

4747
context.ConfigDir = c.String("configdir")

cli/docker/app/factory.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package app
33
import (
44
"github.com/docker/libcompose/cli/logger"
55
"github.com/docker/libcompose/docker"
6+
"github.com/docker/libcompose/docker/ctx"
67
"github.com/docker/libcompose/project"
78
"github.com/urfave/cli"
89
)
@@ -13,7 +14,7 @@ type ProjectFactory struct {
1314

1415
// Create implements ProjectFactory.Create using docker client.
1516
func (p *ProjectFactory) Create(c *cli.Context) (project.APIProject, error) {
16-
context := &docker.Context{}
17+
context := &ctx.Context{}
1718
context.LoggerFactory = logger.NewColorLoggerFactory()
1819
Populate(context, c)
1920
return docker.NewProject(context, nil)

docker/context.go renamed to docker/ctx/context.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package docker
1+
package ctx
22

33
import (
44
"github.com/docker/docker/cliconfig"
@@ -18,10 +18,6 @@ type Context struct {
1818
AuthLookup auth.Lookup
1919
}
2020

21-
func (c *Context) open() error {
22-
return c.LookupConfig()
23-
}
24-
2521
// LookupConfig tries to load the docker configuration files, if any.
2622
func (c *Context) LookupConfig() error {
2723
if c.ConfigFile != nil {

docker/project.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,17 @@ import (
1212
"github.com/docker/libcompose/config"
1313
"github.com/docker/libcompose/docker/auth"
1414
"github.com/docker/libcompose/docker/client"
15+
"github.com/docker/libcompose/docker/ctx"
1516
"github.com/docker/libcompose/docker/network"
17+
"github.com/docker/libcompose/docker/service"
1618
"github.com/docker/libcompose/docker/volume"
1719
"github.com/docker/libcompose/labels"
1820
"github.com/docker/libcompose/lookup"
1921
"github.com/docker/libcompose/project"
2022
)
2123

22-
// ComposeVersion is name of docker-compose.yml file syntax supported version
23-
const ComposeVersion = "1.5.0"
24-
2524
// NewProject creates a Project with the specified context.
26-
func NewProject(context *Context, parseOptions *config.ParseOptions) (project.APIProject, error) {
25+
func NewProject(context *ctx.Context, parseOptions *config.ParseOptions) (project.APIProject, error) {
2726
if context.ResourceLookup == nil {
2827
context.ResourceLookup = &lookup.FileResourceLookup{}
2928
}
@@ -48,9 +47,7 @@ func NewProject(context *Context, parseOptions *config.ParseOptions) (project.AP
4847
}
4948

5049
if context.ServiceFactory == nil {
51-
context.ServiceFactory = &ServiceFactory{
52-
context: context,
53-
}
50+
context.ServiceFactory = service.NewFactory(context)
5451
}
5552

5653
if context.ClientFactory == nil {
@@ -86,7 +83,7 @@ func NewProject(context *Context, parseOptions *config.ParseOptions) (project.AP
8683
return nil, err
8784
}
8885

89-
if err = context.open(); err != nil {
86+
if err = context.LookupConfig(); err != nil {
9087
logrus.Errorf("Failed to open project %s: %v", p.Name, err)
9188
return nil, err
9289
}

docker/convert.go renamed to docker/service/convert.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package docker
1+
package service
22

33
import (
44
"fmt"

docker/convert_test.go renamed to docker/service/convert_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
package docker
1+
package service
22

33
import (
44
"path/filepath"
55
"testing"
66

77
"github.com/docker/libcompose/config"
8+
"github.com/docker/libcompose/docker/ctx"
89
"github.com/docker/libcompose/lookup"
910
"github.com/docker/libcompose/yaml"
1011
shlex "github.com/flynn/go-shlex"
@@ -19,7 +20,7 @@ func TestParseCommand(t *testing.T) {
1920
}
2021

2122
func TestParseBindsAndVolumes(t *testing.T) {
22-
ctx := &Context{}
23+
ctx := &ctx.Context{}
2324
ctx.ComposeFiles = []string{"foo/docker-compose.yml"}
2425
ctx.ResourceLookup = &lookup.FileResourceLookup{}
2526

@@ -56,7 +57,7 @@ func TestParseBindsAndVolumes(t *testing.T) {
5657
}
5758

5859
func TestParseLabels(t *testing.T) {
59-
ctx := &Context{}
60+
ctx := &ctx.Context{}
6061
ctx.ComposeFiles = []string{"foo/docker-compose.yml"}
6162
ctx.ResourceLookup = &lookup.FileResourceLookup{}
6263
bashCmd := "bash"

docker/name.go renamed to docker/service/name.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package docker
1+
package service
22

33
import (
44
"fmt"

docker/name_test.go renamed to docker/service/name_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package docker
1+
package service
22

33
import (
44
"fmt"

docker/service.go renamed to docker/service/service.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package docker
1+
package service
22

33
import (
44
"fmt"
@@ -19,6 +19,7 @@ import (
1919
"github.com/docker/libcompose/docker/builder"
2020
composeclient "github.com/docker/libcompose/docker/client"
2121
"github.com/docker/libcompose/docker/container"
22+
"github.com/docker/libcompose/docker/ctx"
2223
"github.com/docker/libcompose/docker/image"
2324
"github.com/docker/libcompose/labels"
2425
"github.com/docker/libcompose/project"
@@ -38,11 +39,11 @@ type Service struct {
3839
authLookup auth.Lookup
3940

4041
// FIXME(vdemeester) remove this at some point
41-
context *Context
42+
context *ctx.Context
4243
}
4344

4445
// NewService creates a service
45-
func NewService(name string, serviceConfig *config.ServiceConfig, context *Context) *Service {
46+
func NewService(name string, serviceConfig *config.ServiceConfig, context *ctx.Context) *Service {
4647
return &Service{
4748
name: name,
4849
project: context.Project,

docker/service_create.go renamed to docker/service/service_create.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package docker
1+
package service
22

33
import (
44
"fmt"
@@ -38,7 +38,7 @@ func (s *Service) createContainer(ctx context.Context, namer Namer, oldContainer
3838
configWrapper.Config.Labels[labels.HASH.Str()] = config.GetServiceHash(s.name, serviceConfig)
3939
configWrapper.Config.Labels[labels.ONEOFF.Str()] = strings.Title(strconv.FormatBool(oneOff))
4040
configWrapper.Config.Labels[labels.NUMBER.Str()] = fmt.Sprintf("%d", containerNumber)
41-
configWrapper.Config.Labels[labels.VERSION.Str()] = ComposeVersion
41+
configWrapper.Config.Labels[labels.VERSION.Str()] = project.ComposeVersion
4242

4343
err = s.populateAdditionalHostConfig(configWrapper.HostConfig)
4444
if err != nil {

0 commit comments

Comments
 (0)