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

Commit 3acd264

Browse files
Anthony Emengojoaopapereira
authored andcommitted
Refactor provision/services.go
[#161730948] Signed-off-by: Joao Pereira <[email protected]>
1 parent 9498f3c commit 3acd264

File tree

2 files changed

+29
-34
lines changed

2 files changed

+29
-34
lines changed

provision/services.go

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package provision
22

33
import (
44
"code.cloudfoundry.org/cfdev/bosh"
5-
"errors"
65
"os"
76
"os/exec"
87
"path/filepath"
@@ -22,43 +21,46 @@ type Service struct {
2221
}
2322

2423
func (c *Controller) WhiteListServices(whiteList string, services []Service) ([]Service, error) {
25-
if services == nil {
26-
return nil, errors.New("Error whitelisting services")
27-
}
24+
var whiteListed []Service
2825

29-
if strings.ToLower(whiteList) == "all" {
30-
return services, nil
26+
for _, service := range services {
27+
if service.Flagname == "always-include" {
28+
whiteListed = append(whiteListed, service)
29+
}
3130
}
3231

33-
var whiteListed []Service
34-
35-
if whiteList == "none" {
32+
switch strings.TrimSpace(strings.ToLower(whiteList)) {
33+
case "all":
34+
return services, nil
35+
case "none":
36+
return whiteListed, nil
37+
case "":
3638
for _, service := range services {
37-
if service.Flagname == "always-include" {
39+
if service.DefaultDeploy && !contains(whiteListed, service.Name) {
3840
whiteListed = append(whiteListed, service)
3941
}
4042
}
4143

4244
return whiteListed, nil
43-
}
44-
45-
if whiteList == "" {
45+
default:
4646
for _, service := range services {
47-
if service.DefaultDeploy {
47+
if strings.Contains(strings.ToLower(whiteList), strings.ToLower(service.Flagname)) && !contains(whiteListed, service.Name) {
4848
whiteListed = append(whiteListed, service)
4949
}
5050
}
5151

5252
return whiteListed, nil
5353
}
54+
}
5455

55-
for _, service := range services {
56-
if strings.Contains(strings.ToLower(whiteList), strings.ToLower(service.Flagname)) || (strings.ToLower(service.Flagname) == "always-include") {
57-
whiteListed = append(whiteListed, service)
56+
func contains(services []Service, name string) bool {
57+
for _, s := range services {
58+
if s.Name == name {
59+
return true
5860
}
5961
}
6062

61-
return whiteListed, nil
63+
return false
6264
}
6365

6466
func (c *Controller) DeployServices(ui UI, services []Service) error {
@@ -109,4 +111,4 @@ func (c *Controller) DeployService(service Service) error {
109111
cmd.Stderr = logFile
110112

111113
return cmd.Run()
112-
}
114+
}

provision/services_test.go

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ var _ = Describe("When progress whitelist is called with", func() {
5858
Expect(err).ToNot(HaveOccurred())
5959

6060
Expect(len(output)).To(Equal(3))
61-
Expect(output[0].Name).To(Equal("service-one"))
62-
Expect(output[1].Name).To(Equal("service-three"))
63-
Expect(output[2].Name).To(Equal("service-four"))
61+
Expect(output[0].Name).To(Equal("service-four"))
62+
Expect(output[1].Name).To(Equal("service-one"))
63+
Expect(output[2].Name).To(Equal("service-three"))
6464
})
6565
})
6666

@@ -79,8 +79,8 @@ var _ = Describe("When progress whitelist is called with", func() {
7979
Expect(err).ToNot(HaveOccurred())
8080

8181
Expect(len(output)).To(Equal(2))
82-
Expect(output[0].Name).To(Equal("service-three"))
83-
Expect(output[1].Name).To(Equal("service-four"))
82+
Expect(output[0].Name).To(Equal("service-four"))
83+
Expect(output[1].Name).To(Equal("service-three"))
8484
})
8585
})
8686

@@ -90,9 +90,9 @@ var _ = Describe("When progress whitelist is called with", func() {
9090
Expect(err).ToNot(HaveOccurred())
9191

9292
Expect(len(output)).To(Equal(3))
93-
Expect(output[0].Name).To(Equal("service-two"))
94-
Expect(output[1].Name).To(Equal("service-three"))
95-
Expect(output[2].Name).To(Equal("service-four"))
93+
Expect(output[0].Name).To(Equal("service-four"))
94+
Expect(output[1].Name).To(Equal("service-two"))
95+
Expect(output[2].Name).To(Equal("service-three"))
9696
})
9797
})
9898

@@ -105,11 +105,4 @@ var _ = Describe("When progress whitelist is called with", func() {
105105
Expect(output[0].Name).To(Equal("service-four"))
106106
})
107107
})
108-
109-
Context("nil services", func() {
110-
It("returns an error", func() {
111-
_, err := c.WhiteListServices("service-one", nil)
112-
Expect(err).To(HaveOccurred())
113-
})
114-
})
115108
})

0 commit comments

Comments
 (0)