Skip to content

Commit 2d986bf

Browse files
author
Jason Stangroome
authored
Merge pull request #116 from section/jason-do-not-create-server-conf
Do not create server.conf if missing but validate if present
2 parents 44aeb78 + ae47046 commit 2d986bf

File tree

2 files changed

+20
-35
lines changed

2 files changed

+20
-35
lines changed

commands/apps.go

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -212,29 +212,12 @@ func (c *AppsDeleteCmd) Run() (err error) {
212212
return err
213213
}
214214

215-
// AppsInitCmd creates and validates server.conf and package.json to prepare an app for deployment
215+
// AppsInitCmd creates and validates package.json to prepare an app for deployment
216216
type AppsInitCmd struct {
217217
StackName string `optional default:"nodejs-basic" short:"s" help:"Name of stack to deploy. Default is nodejs-basic"`
218218
Force bool `optional short:"f" help:"Resets deployment specific files to their default configuration"`
219219
}
220220

221-
func (c *AppsInitCmd) buildServerConf() []byte {
222-
return []byte(
223-
`location / {
224-
proxy_set_header X-Forwarded-For $http_x_forwarded_for;
225-
proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
226-
proxy_set_header Host $host;
227-
include /etc/nginx/section.module/node.conf;
228-
}
229-
230-
location ~ "/next-proxy-hop/" {
231-
proxy_set_header X-Forwarded-For $http_x_forwarded_for;
232-
proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
233-
proxy_set_header Host $host;
234-
proxy_pass http://next-hop;
235-
}`)
236-
}
237-
238221
// Run executes the command
239222
func (c *AppsInitCmd) Run() (err error) {
240223
var stdout bytes.Buffer
@@ -263,27 +246,27 @@ func (c *AppsInitCmd) CreatePkgJSON(stdout, stderr bytes.Buffer) (err error) {
263246
// InitializeNodeBasicApp initializes a basic node app.
264247
func (c *AppsInitCmd) InitializeNodeBasicApp(stdout, stderr bytes.Buffer) (err error) {
265248
if c.Force {
266-
log.Println("[INFO] Removing old versions of server.conf and package.json")
267-
err1 := os.Remove("package.json")
268-
err2 := os.Remove("server.conf")
269-
if err1 != nil || err2 != nil {
270-
log.Println("[ERROR] unable to remove files, perhaps they do not exist?")
249+
log.Println("[INFO] Removing old version of package.json")
250+
err = os.Remove("package.json")
251+
if err != nil {
252+
if !os.IsNotExist(err) {
253+
log.Println("[ERROR] unable to remove package.json file")
254+
return err
255+
}
271256
} else {
272257
log.Println("[DEBUG] Files successfully removed")
273258
}
274259
}
275260
log.Println("[DEBUG] Checking to see if server.conf exists")
276261
checkServConf, err := os.Open("server.conf")
277262
if err != nil {
278-
log.Println("[WARN] server.conf does not exist. Creating server.conf")
279-
f, err := os.Create("server.conf")
280-
if err != nil {
281-
return fmt.Errorf("error in creating a file: server.conf %w", err)
263+
if !os.IsNotExist(err) {
264+
log.Println("[ERROR] server.conf file could not be opened")
265+
return err
282266
}
283-
b := c.buildServerConf()
284-
f.Write(b)
285-
defer f.Close()
286-
} else {
267+
}
268+
defer checkServConf.Close()
269+
if err == nil {
287270
log.Println("[INFO] Validating server.conf")
288271
fileinfo, err := checkServConf.Stat()
289272
if err != nil {
@@ -299,7 +282,6 @@ func (c *AppsInitCmd) InitializeNodeBasicApp(stdout, stderr bytes.Buffer) (err e
299282
log.Println("[WARN] default location unspecified. Edit or delete server.conf and rerun this command")
300283
}
301284
}
302-
defer checkServConf.Close()
303285
log.Println("[DEBUG] Checking to see if package.json exists")
304286
checkPkgJSON, err := os.Open("package.json")
305287
if err != nil {

commands/apps_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func TestCommandsAppsInitHandlesErrors(t *testing.T) {
5757
pkgJSON string
5858
isFatal bool
5959
}{
60+
{"ABSENT", string(helperLoadBytes(t, "apps/init.good.package.json")), false}, // server.conf is missing
6061
{string(helperLoadBytes(t, "apps/init.good.server.conf")), string(helperLoadBytes(t, "apps/init.good.package.json")), false}, // no files are broken or missing
6162
{string(helperLoadBytes(t, "apps/init.bad.server.conf")), string(helperLoadBytes(t, "apps/init.bad.package.json")), false}, // both files are broken
6263
{``, ``, false}, // both files are empty
@@ -94,9 +95,11 @@ func TestCommandsAppsInitHandlesErrors(t *testing.T) {
9495
err = os.Chdir(tempDir)
9596
assert.NoError(err)
9697

97-
err = OverwriteFile("server.conf", tc.servConf)
98-
if err != nil {
99-
fmt.Println("server.conf creation failed")
98+
if tc.servConf != "ABSENT" {
99+
err = OverwriteFile("server.conf", tc.servConf)
100+
if err != nil {
101+
fmt.Println("server.conf creation failed")
102+
}
100103
}
101104

102105
err = OverwriteFile("package.json", tc.pkgJSON)

0 commit comments

Comments
 (0)