forked from cloudfoundry-community/cloudfoundry-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
171 lines (137 loc) · 3.95 KB
/
app.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package helpers
import (
"archive/zip"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"strings"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
)
// WithHelloWorldApp creates a simple application to use with your CLI command
// (typically CF Push). When pushing, be aware of specifying '-b
// staticfile_buildpack" so that your app will correctly start up with the
// proper buildpack.
func WithHelloWorldApp(f func(dir string)) {
dir, err := ioutil.TempDir("", "simple-app")
Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(dir)
tempfile := filepath.Join(dir, "index.html")
err = ioutil.WriteFile(tempfile, []byte(fmt.Sprintf("hello world %d", rand.Int())), 0666)
Expect(err).ToNot(HaveOccurred())
err = ioutil.WriteFile(filepath.Join(dir, "Staticfile"), nil, 0666)
Expect(err).ToNot(HaveOccurred())
f(dir)
}
func WithMultiBuildpackApp(f func(dir string)) {
f("../assets/go_calls_ruby")
}
// WithProcfileApp creates an application to use with your CLI command
// that contains Procfile defining web and worker processes.
func WithProcfileApp(f func(dir string)) {
dir, err := ioutil.TempDir("", "simple-ruby-app")
Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(dir)
err = ioutil.WriteFile(filepath.Join(dir, "Procfile"), []byte(`---
web: ruby -run -e httpd . -p $PORT
console: bundle exec irb`,
), 0666)
Expect(err).ToNot(HaveOccurred())
err = ioutil.WriteFile(filepath.Join(dir, "Gemfile"), nil, 0666)
Expect(err).ToNot(HaveOccurred())
err = ioutil.WriteFile(filepath.Join(dir, "Gemfile.lock"), []byte(`
GEM
specs:
PLATFORMS
ruby
DEPENDENCIES
BUNDLED WITH
1.15.0
`), 0666)
Expect(err).ToNot(HaveOccurred())
f(dir)
}
// WithBananaPantsApp creates a simple application to use with your CLI command
// (typically CF Push). When pushing, be aware of specifying '-b
// staticfile_buildpack" so that your app will correctly start up with the
// proper buildpack.
func WithBananaPantsApp(f func(dir string)) {
dir, err := ioutil.TempDir("", "simple-app")
Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(dir)
tempfile := filepath.Join(dir, "index.html")
err = ioutil.WriteFile(tempfile, []byte("Banana Pants"), 0666)
Expect(err).ToNot(HaveOccurred())
err = ioutil.WriteFile(filepath.Join(dir, "Staticfile"), nil, 0666)
Expect(err).ToNot(HaveOccurred())
f(dir)
}
// AppGUID returns the GUID for an app in the currently targeted space.
func AppGUID(appName string) string {
session := CF("app", appName, "--guid")
Eventually(session).Should(gexec.Exit(0))
return strings.TrimSpace(string(session.Out.Contents()))
}
func WriteManifest(path string, manifest map[string]interface{}) {
body, err := json.Marshal(manifest)
Expect(err).ToNot(HaveOccurred())
err = ioutil.WriteFile(path, body, 0666)
Expect(err).ToNot(HaveOccurred())
}
// Thanks to Svett Ralchev
// http://blog.ralch.com/tutorial/golang-working-with-zip/
// Zipit zips the source into a .zip file in the target dir
func Zipit(source, target, prefix string) error {
zipfile, err := os.Create(target)
if err != nil {
return err
}
defer zipfile.Close()
if prefix != "" {
_, err = io.WriteString(zipfile, prefix)
if err != nil {
return err
}
}
archive := zip.NewWriter(zipfile)
defer archive.Close()
err = filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if path == source {
return nil
}
header, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
header.Name = strings.TrimPrefix(path, source+string(filepath.Separator))
if info.IsDir() {
header.Name += string(os.PathSeparator)
header.SetMode(0755)
} else {
header.Method = zip.Deflate
header.SetMode(0744)
}
writer, err := archive.CreateHeader(header)
if err != nil {
return err
}
if info.IsDir() {
return nil
}
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(writer, file)
return err
})
return err
}