~ubuntu-branches/ubuntu/trusty/juju-core/trusty-proposed

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/juju/osenv/proxy.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-01-29 11:40:20 UTC
  • mfrom: (23.1.1 trusty-proposed)
  • Revision ID: package-import@ubuntu.com-20140129114020-ejieitm8smtt5vln
Tags: 1.17.1-0ubuntu2
d/tests/local-provider: Don't fail tests if ~/.juju is present as its
created by the juju version command. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2014 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package osenv
 
5
 
 
6
import (
 
7
        "fmt"
 
8
        "os"
 
9
        "strings"
 
10
)
 
11
 
 
12
// ProxySettings holds the values for the http, https and ftp proxies found by
 
13
// Detect Proxies.
 
14
type ProxySettings struct {
 
15
        Http  string
 
16
        Https string
 
17
        Ftp   string
 
18
}
 
19
 
 
20
func getProxySetting(key string) string {
 
21
        value := os.Getenv(key)
 
22
        if value == "" {
 
23
                value = os.Getenv(strings.ToUpper(key))
 
24
        }
 
25
        return value
 
26
}
 
27
 
 
28
// DetectProxies returns the proxy settings found the environment.
 
29
func DetectProxies() ProxySettings {
 
30
        return ProxySettings{
 
31
                Http:  getProxySetting("http_proxy"),
 
32
                Https: getProxySetting("https_proxy"),
 
33
                Ftp:   getProxySetting("ftp_proxy"),
 
34
        }
 
35
}
 
36
 
 
37
// AsEnvironmentValues returns a potentially multi-line
 
38
// string in a format that specifies key=value lines.
 
39
// There are two lines for each non-empty proxy value,
 
40
// one lower-case and one upper-case.
 
41
func (s *ProxySettings) AsEnvironmentValues() string {
 
42
        lines := []string{}
 
43
        addLine := func(proxy, value string) {
 
44
                if value != "" {
 
45
                        lines = append(
 
46
                                lines,
 
47
                                fmt.Sprintf("export %s=%s", proxy, value),
 
48
                                fmt.Sprintf("export %s=%s", strings.ToUpper(proxy), value))
 
49
                }
 
50
        }
 
51
        addLine("http_proxy", s.Http)
 
52
        addLine("https_proxy", s.Https)
 
53
        addLine("ftp_proxy", s.Ftp)
 
54
        return strings.Join(lines, "\n")
 
55
}