This repository has been archived by the owner on Jul 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
/
bosh.go
81 lines (68 loc) · 1.84 KB
/
bosh.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package bosh
import (
"code.cloudfoundry.org/cfdev/cfanalytics"
"os"
"runtime"
"code.cloudfoundry.org/cfdev/bosh"
"code.cloudfoundry.org/cfdev/errors"
"code.cloudfoundry.org/cfdev/shell"
"github.com/spf13/cobra"
)
//go:generate mockgen -package mocks -destination mocks/ui.go code.cloudfoundry.org/cfdev/cmd/bosh UI
type UI interface {
Say(message string, args ...interface{})
}
//go:generate mockgen -package mocks -destination mocks/analytics_client.go code.cloudfoundry.org/cfdev/cmd/bosh AnalyticsClient
type AnalyticsClient interface {
Event(event string, data ...map[string]interface{}) error
PromptOptIn() error
}
//go:generate mockgen -package mocks -destination mocks/provision.go code.cloudfoundry.org/cfdev/cmd/bosh Provisioner
type Provisioner interface {
FetchBOSHConfig() (bosh.Config, error)
}
type Bosh struct {
Exit chan struct{}
UI UI
StateDir string
Provisioner Provisioner
Analytics AnalyticsClient
}
func (b *Bosh) Cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "bosh",
Run: func(cmd *cobra.Command, args []string) {
if runtime.GOOS != "windows" {
b.UI.Say(`Usage: eval $(cf dev bosh env)`)
} else {
b.UI.Say(`Usage: cf dev bosh env | Invoke-Expression`)
}
},
}
envCmd := &cobra.Command{
Use: "env",
RunE: func(cmd *cobra.Command, args []string) error {
return b.Env()
},
}
cmd.AddCommand(envCmd)
return cmd
}
func (b *Bosh) Env() error {
go func() {
<-b.Exit
os.Exit(128)
}()
config, err := b.Provisioner.FetchBOSHConfig()
if err != nil {
return errors.SafeWrap(err, "failed to fetch bosh configuration")
}
b.Analytics.Event(cfanalytics.BOSH_ENV)
env := shell.Environment{StateDir: b.StateDir}
shellScript, err := env.Prepare(config)
if err != nil {
return errors.SafeWrap(err, "failed to prepare bosh configuration")
}
b.UI.Say(shellScript)
return nil
}