-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp_fetch.go
84 lines (77 loc) · 1.84 KB
/
app_fetch.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
package main
import (
"archive/zip"
"fmt"
"github.com/igm/cf"
"io"
"log"
"os"
"time"
)
func init() {
register(&Command{
group: "Application",
name: "app.fetch",
help: "Fetch application instance files into local zip file",
params: []Param{
Param{name: "name", desc: "Application name"},
Param{name: "org", desc: "Organization name"},
Param{name: "space", desc: "Space name"},
Param{name: "instance", desc: "Application instance", defval: "0"},
Param{name: "dir", desc: "Remote dir"},
},
handle: app_fetch,
})
}
func app_fetch() {
target, err := c.SelectedTarget()
if err != nil {
log.Fatal(err)
}
// Get Application ID
app, err := target.AppFind(params["name"], params["space"], params["org"])
if err != nil {
log.Fatal(err)
}
instance := params["instance"]
timestamp := time.Now().Format("20060102150405")
filename := fmt.Sprintf("%s.%s.%s.zip", app.Name, instance, timestamp)
zipOut, err := os.Create(filename)
if err != nil {
log.Fatal(err)
}
fmt.Printf("creating zip: %s\n", filename)
zipWriter := zip.NewWriter(zipOut)
defer zipWriter.Close()
var fetchDir func(string)
fetchDir = func(dir string) {
files, err := target.AppLs(app.Guid, instance, dir)
if err != nil {
log.Fatal(err)
}
for _, file := range files {
if file.Dir {
log.Printf("recuring into %s%s\n", dir, file.Name)
fetchDir(dir + file.Name)
} else {
remoteFile := fmt.Sprintf("%s%s", dir, file.Name)
log.Printf("fetching %s\n", remoteFile)
reader, err := target.AppGet(app.Guid, instance, remoteFile)
if err != nil {
if cferr, ok := err.(*cf.Error); ok {
log.Println(cferr)
continue
} else {
log.Fatal(err)
}
}
w, err := zipWriter.Create(remoteFile)
if err != nil {
log.Fatal(err)
}
io.Copy(w, reader)
}
}
}
fetchDir(params["dir"] + "/")
}