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

Commit 8945cca

Browse files
author
Anthony Emengo
committed
Continue the refactor effort
Migrate the env package functionality to more apt abstractions. Helps to reduce the overall clutter and ambiguity in class responsibilities.
1 parent d534551 commit 8945cca

15 files changed

+278
-421
lines changed

cfanalytics/analyticsd_darwin.go

+11-15
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,27 @@ package cfanalytics
22

33
import (
44
"code.cloudfoundry.org/cfdev/daemon"
5-
"code.cloudfoundry.org/cfdev/env"
65
"os"
76
"path"
87
"path/filepath"
98
)
109

1110
func (a *AnalyticsD) DaemonSpec() daemon.DaemonSpec {
12-
environmentVariables := map[string]string{
13-
"CFDEV_MODE": os.Getenv("CFDEV_MODE"),
14-
}
15-
16-
proxyConf := env.BuildProxyConfig(
17-
a.Config.BoshDirectorIP,
18-
a.Config.CFRouterIP,
19-
a.Config.HostIP,
11+
var (
12+
proxyConfig = a.Config.BuildProxyConfig()
13+
environmentVariables = map[string]string{
14+
"CFDEV_MODE": os.Getenv("CFDEV_MODE"),
15+
}
2016
)
2117

22-
if proxyConf.Http != "" {
23-
environmentVariables["HTTP_PROXY"] = proxyConf.Http
18+
if proxyConfig.Http != "" {
19+
environmentVariables["HTTP_PROXY"] = proxyConfig.Http
2420
}
25-
if proxyConf.Https != "" {
26-
environmentVariables["HTTPS_PROXY"] = proxyConf.Https
21+
if proxyConfig.Https != "" {
22+
environmentVariables["HTTPS_PROXY"] = proxyConfig.Https
2723
}
28-
if proxyConf.NoProxy != "" {
29-
environmentVariables["NO_PROXY"] = proxyConf.NoProxy
24+
if proxyConfig.NoProxy != "" {
25+
environmentVariables["NO_PROXY"] = proxyConfig.NoProxy
3026
}
3127

3228
return daemon.DaemonSpec{

cfanalytics/analyticsd_linux.go

+11-15
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,27 @@ package cfanalytics
22

33
import (
44
"code.cloudfoundry.org/cfdev/daemon"
5-
"code.cloudfoundry.org/cfdev/env"
65
"os"
76
"path"
87
"path/filepath"
98
)
109

1110
func (a *AnalyticsD) DaemonSpec() daemon.DaemonSpec {
12-
environmentVariables := map[string]string{
13-
"CFDEV_MODE": os.Getenv("CFDEV_MODE"),
14-
}
15-
16-
proxyConf := env.BuildProxyConfig(
17-
a.Config.BoshDirectorIP,
18-
a.Config.CFRouterIP,
19-
a.Config.HostIP,
11+
var (
12+
proxyConfig = a.Config.BuildProxyConfig()
13+
environmentVariables = map[string]string{
14+
"CFDEV_MODE": os.Getenv("CFDEV_MODE"),
15+
}
2016
)
2117

22-
if proxyConf.Http != "" {
23-
environmentVariables["HTTP_PROXY"] = proxyConf.Http
18+
if proxyConfig.Http != "" {
19+
environmentVariables["HTTP_PROXY"] = proxyConfig.Http
2420
}
25-
if proxyConf.Https != "" {
26-
environmentVariables["HTTPS_PROXY"] = proxyConf.Https
21+
if proxyConfig.Https != "" {
22+
environmentVariables["HTTPS_PROXY"] = proxyConfig.Https
2723
}
28-
if proxyConf.NoProxy != "" {
29-
environmentVariables["NO_PROXY"] = proxyConf.NoProxy
24+
if proxyConfig.NoProxy != "" {
25+
environmentVariables["NO_PROXY"] = proxyConfig.NoProxy
3026
}
3127

3228
return daemon.DaemonSpec{

cfanalytics/analyticsd_windows.go

+11-15
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,26 @@ package cfanalytics
22

33
import (
44
"code.cloudfoundry.org/cfdev/daemon"
5-
"code.cloudfoundry.org/cfdev/env"
65
"os"
76
"path/filepath"
87
)
98

109
func (a *AnalyticsD) DaemonSpec() daemon.DaemonSpec {
11-
environmentVariables := map[string]string{
12-
"CFDEV_MODE": os.Getenv("CFDEV_MODE"),
13-
}
14-
15-
proxyConf := env.BuildProxyConfig(
16-
a.Config.BoshDirectorIP,
17-
a.Config.CFRouterIP,
18-
a.Config.HostIP,
10+
var (
11+
proxyConfig = a.Config.BuildProxyConfig()
12+
environmentVariables = map[string]string{
13+
"CFDEV_MODE": os.Getenv("CFDEV_MODE"),
14+
}
1915
)
2016

21-
if proxyConf.Http != "" {
22-
environmentVariables["HTTP_PROXY"] = proxyConf.Http
17+
if proxyConfig.Http != "" {
18+
environmentVariables["HTTP_PROXY"] = proxyConfig.Http
2319
}
24-
if proxyConf.Https != "" {
25-
environmentVariables["HTTPS_PROXY"] = proxyConf.Https
20+
if proxyConfig.Https != "" {
21+
environmentVariables["HTTPS_PROXY"] = proxyConfig.Https
2622
}
27-
if proxyConf.NoProxy != "" {
28-
environmentVariables["NO_PROXY"] = proxyConf.NoProxy
23+
if proxyConfig.NoProxy != "" {
24+
environmentVariables["NO_PROXY"] = proxyConfig.NoProxy
2925
}
3026

3127
return daemon.DaemonSpec{

cmd/download/download.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ type UI interface {
1919
Writer() io.Writer
2020
}
2121

22-
type Env interface {
22+
type Workspace interface {
2323
CreateDirs() error
2424
}
2525

2626
type Download struct {
27-
Exit chan struct{}
28-
UI UI
29-
Config config.Config
30-
Env Env
27+
Exit chan struct{}
28+
UI UI
29+
Config config.Config
30+
Workspace Workspace
3131
}
3232

3333
func (d *Download) Cmd() *cobra.Command {
@@ -43,7 +43,7 @@ func (d *Download) RunE(cmd *cobra.Command, args []string) error {
4343
os.Exit(128)
4444
}()
4545

46-
if err := d.Env.CreateDirs(); err != nil {
46+
if err := d.Workspace.CreateDirs(); err != nil {
4747
return errors.SafeWrap(err, "setup for download")
4848
}
4949

@@ -53,11 +53,11 @@ func (d *Download) RunE(cmd *cobra.Command, args []string) error {
5353

5454
func CacheSync(dependencies resource.Catalog, cacheDir string, writer io.Writer) error {
5555
cache := resource.Cache{
56-
Dir: cacheDir,
57-
HttpDo: http.DefaultClient.Do,
58-
Progress: progress.New(writer),
59-
RetryWait: time.Second,
60-
Writer: writer,
56+
Dir: cacheDir,
57+
HttpDo: http.DefaultClient.Do,
58+
Progress: progress.New(writer),
59+
RetryWait: time.Second,
60+
Writer: writer,
6161
}
6262

6363
if err := cache.Sync(dependencies); err != nil {

cmd/root.go

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"code.cloudfoundry.org/cfdev/env"
54
cfdevos "code.cloudfoundry.org/cfdev/os"
65
"code.cloudfoundry.org/cfdev/workspace"
76
"io"
@@ -56,11 +55,11 @@ func NewRoot(exit chan struct{}, ui UI, config config.Config, analyticsClient An
5655
root.SetUsageTemplate(usageTemplate)
5756

5857
var (
59-
writer = ui.Writer()
60-
driver = newDriver(ui, config)
61-
workspace = workspace.New(config)
62-
provisioner = provision.NewController(config)
63-
analyticsD = &cfanalytics.AnalyticsD{
58+
writer = ui.Writer()
59+
driver = newDriver(ui, config)
60+
workspace = workspace.New(config)
61+
provisioner = provision.NewController(config)
62+
analyticsD = &cfanalytics.AnalyticsD{
6463
Config: config,
6564
DaemonRunner: newDaemonRunner(config),
6665
}
@@ -101,10 +100,10 @@ func NewRoot(exit chan struct{}, ui UI, config config.Config, analyticsClient An
101100
}
102101

103102
download = &b4.Download{
104-
Exit: exit,
105-
UI: ui,
106-
Config: config,
107-
Env: &env.Env{Config: config},
103+
Exit: exit,
104+
UI: ui,
105+
Config: config,
106+
Workspace: workspace,
108107
}
109108

110109
telemetryCmd = &b7.Telemetry{
@@ -134,7 +133,7 @@ func NewRoot(exit chan struct{}, ui UI, config config.Config, analyticsClient An
134133
UI: ui,
135134
Config: config,
136135
Cache: cache,
137-
Env: &env.Env{Config: config},
136+
Workspace: workspace,
138137
Analytics: analyticsClient,
139138
AnalyticsToggle: analyticsToggle,
140139
Driver: driver,

cmd/start/start.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ type Stop interface {
6666
RunE(cmd *cobra.Command, args []string) error
6767
}
6868

69-
//go:generate mockgen -package mocks -destination mocks/env.go code.cloudfoundry.org/cfdev/cmd/start Env
70-
type Env interface {
69+
//go:generate mockgen -package mocks -destination mocks/env.go code.cloudfoundry.org/cfdev/cmd/start Workspace
70+
type Workspace interface {
7171
CreateDirs() error
7272
SetupState(depsFile string) error
7373
}
@@ -105,7 +105,7 @@ type Start struct {
105105
Stop Stop
106106
Provisioner Provisioner
107107
Provision Provision
108-
Env Env
108+
Workspace Workspace
109109
OS OS
110110
}
111111

@@ -181,7 +181,7 @@ func (s *Start) Execute(args Args) error {
181181
return e.SafeWrap(err, "stopping cfdev")
182182
}
183183

184-
if err := s.Env.CreateDirs(); err != nil {
184+
if err := s.Workspace.CreateDirs(); err != nil {
185185
return e.SafeWrap(err, "setting up cfdev home dir")
186186
}
187187

@@ -208,7 +208,7 @@ func (s *Start) Execute(args Args) error {
208208
}
209209

210210
s.UI.Say("Setting State...")
211-
if err := s.Env.SetupState(depsPath); err != nil {
211+
if err := s.Workspace.SetupState(depsPath); err != nil {
212212
return e.SafeWrap(err, "Unable to setup directories")
213213
}
214214

config/proxy.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package config
2+
3+
import (
4+
"os"
5+
"strings"
6+
)
7+
8+
type ProxyConfig struct {
9+
Http string `json:"http,omitempty"`
10+
Https string `json:"https,omitempty"`
11+
NoProxy string `json:"exclude,omitempty"`
12+
}
13+
14+
func (c *Config) BuildProxyConfig() ProxyConfig {
15+
httpProxy := os.Getenv("http_proxy")
16+
if os.Getenv("HTTP_PROXY") != "" {
17+
httpProxy = os.Getenv("HTTP_PROXY")
18+
}
19+
20+
httpsProxy := os.Getenv("https_proxy")
21+
if os.Getenv("HTTPS_PROXY") != "" {
22+
httpsProxy = os.Getenv("HTTPS_PROXY")
23+
}
24+
25+
noProxy := os.Getenv("no_proxy")
26+
if os.Getenv("NO_PROXY") != "" {
27+
noProxy = os.Getenv("NO_PROXY")
28+
}
29+
30+
if c.BoshDirectorIP != "" && !strings.Contains(noProxy, c.BoshDirectorIP) {
31+
noProxy = strings.Join([]string{noProxy, c.BoshDirectorIP}, ",")
32+
}
33+
34+
if c.CFRouterIP != "" && !strings.Contains(noProxy, c.CFRouterIP) {
35+
noProxy = strings.Join([]string{noProxy, c.CFRouterIP}, ",")
36+
}
37+
38+
if c.HostIP != "" && !strings.Contains(noProxy, c.HostIP) {
39+
noProxy = strings.Join([]string{noProxy, c.HostIP}, ",")
40+
}
41+
42+
return ProxyConfig{
43+
Http: httpProxy,
44+
Https: httpsProxy,
45+
NoProxy: noProxy,
46+
}
47+
}
48+
49+
func IsBehindProxy() bool {
50+
return os.Getenv("HTTP_PROXY") != "" ||
51+
os.Getenv("http_proxy") != "" ||
52+
os.Getenv("HTTPS_PROXY") != "" ||
53+
os.Getenv("https_proxy") != ""
54+
}

config/proxy_test.go

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package config_test
2+
3+
import (
4+
"code.cloudfoundry.org/cfdev/config"
5+
. "github.com/onsi/ginkgo"
6+
. "github.com/onsi/gomega"
7+
"os"
8+
)
9+
10+
var _ = Describe("BuildProxyConfig", func() {
11+
var (
12+
cfg = config.Config{
13+
BoshDirectorIP: "bosh-ip",
14+
CFRouterIP: "router-ip",
15+
HostIP: "host-ip",
16+
}
17+
)
18+
19+
Context("when proxy env vars are set", func() {
20+
BeforeEach(func() {
21+
os.Setenv("HTTP_PROXY", "some-http-proxy")
22+
os.Setenv("HTTPS_PROXY", "some-https-proxy")
23+
os.Setenv("NO_PROXY", "some-no-proxy")
24+
})
25+
26+
AfterEach(func() {
27+
os.Unsetenv("HTTP_PROXY")
28+
os.Unsetenv("HTTPS_PROXY")
29+
os.Unsetenv("NO_PROXY")
30+
})
31+
32+
It("returns the http config", func() {
33+
proxyConfig := cfg.BuildProxyConfig()
34+
Expect(proxyConfig.Http).To(Equal("some-http-proxy"))
35+
Expect(proxyConfig.Https).To(Equal("some-https-proxy"))
36+
Expect(proxyConfig.NoProxy).To(Equal("some-no-proxy,bosh-ip,router-ip,host-ip"))
37+
})
38+
})
39+
40+
Context("when multiple mixed case proxy envs prioritize uppercase", func() {
41+
BeforeEach(func() {
42+
os.Setenv("http_proxy", "lower-case-http-proxy")
43+
os.Setenv("HTTP_PROXY", "upper-some-http-proxy")
44+
os.Setenv("https_proxy", "lower-case-https-proxy")
45+
os.Setenv("HTTPS_PROXY", "upper-some-https-proxy")
46+
os.Setenv("no_proxy", "lower-some-no-proxy")
47+
os.Setenv("NO_PROXY", "upper-some-no-proxy,bosh-ip,router-ip")
48+
})
49+
50+
AfterEach(func() {
51+
os.Unsetenv("http_proxy")
52+
os.Unsetenv("HTTP_PROXY")
53+
os.Unsetenv("https_proxy")
54+
os.Unsetenv("HTTPS_PROXY")
55+
os.Unsetenv("no_proxy")
56+
os.Unsetenv("NO_PROXY")
57+
})
58+
59+
It("returns the http config", func() {
60+
proxyConfig := cfg.BuildProxyConfig()
61+
Expect(proxyConfig.Http).To(Equal("upper-some-http-proxy"))
62+
Expect(proxyConfig.Https).To(Equal("upper-some-https-proxy"))
63+
Expect(proxyConfig.NoProxy).To(Equal("upper-some-no-proxy,bosh-ip,router-ip,host-ip"))
64+
})
65+
})
66+
})

0 commit comments

Comments
 (0)