forked from cloudfoundry-incubator/cflocal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pull.go
85 lines (75 loc) · 1.5 KB
/
pull.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
82
83
84
85
package cmd
import (
"flag"
"fmt"
"io"
)
type Pull struct {
UI UI
App App
FS FS
Help Help
Config Config
}
func (p *Pull) Match(args []string) bool {
return len(args) > 0 && args[0] == "pull"
}
func (p *Pull) Run(args []string) error {
name, err := p.options(args)
if err != nil {
p.Help.Short()
return err
}
if err := p.saveDroplet(name); err != nil {
return err
}
if err := p.updateLocalYML(name); err != nil {
return err
}
p.UI.Output("Successfully downloaded: %s", name)
return nil
}
func (p *Pull) saveDroplet(name string) error {
droplet, size, err := p.App.Droplet(name)
if err != nil {
return err
}
defer droplet.Close()
file, err := p.FS.WriteFile(fmt.Sprintf("./%s.droplet", name))
if err != nil {
return err
}
defer file.Close()
if _, err := io.CopyN(file, droplet, size); err != nil && err != io.EOF {
return err
}
return nil
}
func (p *Pull) updateLocalYML(name string) error {
localYML, err := p.Config.Load()
if err != nil {
return err
}
app := getAppConfig(name, localYML)
env, err := p.App.Env(name)
if err != nil {
return err
}
app.StagingEnv = env.Staging
app.RunningEnv = env.Running
app.Env = env.App
command, err := p.App.Command(name)
if err != nil {
return err
}
app.Command = command
if err := p.Config.Save(localYML); err != nil {
return err
}
return nil
}
func (*Pull) options(args []string) (appName string, err error) {
return appName, parseOptions(args, func(name string, _ *flag.FlagSet) {
appName = name
})
}