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

Commit 3acdf2b

Browse files
authored
Merge pull request #379 from bfosberry/get-service-support
Adding GetServiceConfig to project interface
2 parents 10b5cd3 + 693a883 commit 3acdf2b

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

project/interface.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ type APIProject interface {
3939
AddConfig(name string, config *config.ServiceConfig) error
4040
Load(bytes []byte) error
4141
Containers(ctx context.Context, filter Filter, services ...string) ([]string, error)
42+
43+
GetServiceConfig(service string) (*config.ServiceConfig, bool)
4244
}
4345

4446
// Filter holds filter element to filter containers

project/project.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func (p *Project) Parse() error {
121121
// CreateService creates a service with the specified name based. If there
122122
// is no config in the project for this service, it will return an error.
123123
func (p *Project) CreateService(name string) (Service, error) {
124-
existing, ok := p.ServiceConfigs.Get(name)
124+
existing, ok := p.GetServiceConfig(name)
125125
if !ok {
126126
return nil, fmt.Errorf("Failed to find service: %s", name)
127127
}
@@ -527,6 +527,12 @@ func (p *Project) Notify(eventType events.EventType, serviceName string, data ma
527527
}
528528
}
529529

530+
// GetServiceConfig looks up a service config for a given service name, returning the ServiceConfig
531+
// object and a bool flag indicating whether it was found
532+
func (p *Project) GetServiceConfig(name string) (*config.ServiceConfig, bool) {
533+
return p.ServiceConfigs.Get(name)
534+
}
535+
530536
// IsNamedVolume returns whether the specified volume (string) is a named volume or not.
531537
func IsNamedVolume(volume string) bool {
532538
return !strings.HasPrefix(volume, ".") && !strings.HasPrefix(volume, "/") && !strings.HasPrefix(volume, "~")

project/project_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,32 @@ func TestTwoCall(t *testing.T) {
8080
}
8181
}
8282

83+
func TestGetServiceConfig(t *testing.T) {
84+
85+
p := NewProject(&Context{}, nil, nil)
86+
p.ServiceConfigs = config.NewServiceConfigs()
87+
fooService := &config.ServiceConfig{}
88+
p.ServiceConfigs.Add("foo", fooService)
89+
90+
config, ok := p.GetServiceConfig("foo")
91+
if !ok {
92+
t.Fatal("Foo service not found")
93+
}
94+
95+
if config != fooService {
96+
t.Fatal("Incorrect Service Config returned")
97+
}
98+
99+
config, ok = p.GetServiceConfig("unknown")
100+
if ok {
101+
t.Fatal("Found service incorrectly")
102+
}
103+
104+
if config != nil {
105+
t.Fatal("Incorrect Service Config returned")
106+
}
107+
}
108+
83109
func TestParseWithBadContent(t *testing.T) {
84110
p := NewProject(&Context{
85111
ComposeBytes: [][]byte{

0 commit comments

Comments
 (0)