Skip to content

Commit a52cd2b

Browse files
committed
Fixes #852 optionally add last newline
Tested with Linux and Windows, by appending many new functions to a single stack.yml file. Signed-off-by: Kyle Brennan <[email protected]>
1 parent b562392 commit a52cd2b

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

commands/new_function.go

+30
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ Download templates:
152152
}
153153

154154
fileName = appendFile
155+
156+
if err := addEofNewlines("./" + fileName); err != nil {
157+
return err
158+
}
159+
155160
outputMsg = fmt.Sprintf("Stack file updated: %s\n", fileName)
156161

157162
} else {
@@ -346,3 +351,28 @@ Cannot have duplicate function names in same yaml file`, functionName, appendFil
346351

347352
return nil
348353
}
354+
355+
func addEofNewlines(fileName string) error {
356+
bytes, err := os.ReadFile(fileName)
357+
if err != nil {
358+
return fmt.Errorf("could not open '%s' to check for new lines %s", fileName, err)
359+
}
360+
361+
content := string(bytes)
362+
hasLastNewline := strings.HasSuffix(content, "\n")
363+
if hasLastNewline {
364+
return nil
365+
}
366+
367+
file, err := os.OpenFile(fileName, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
368+
if err != nil {
369+
return fmt.Errorf("could not open '%s' to append new lines %s", fileName, err)
370+
}
371+
defer file.Close()
372+
373+
if _, err = file.WriteString("\n\n"); err != nil {
374+
return fmt.Errorf("could not write to '%s' to append new lines %s", fileName, err)
375+
}
376+
377+
return nil
378+
}

commands/new_function_test.go

+60
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,11 @@ func tearDownNewFunction(t *testing.T, functionName string) {
372372
t.Log(err)
373373
}
374374
}
375+
if _, err := os.Stat("stack.yml"); err == nil {
376+
if err := os.Remove("stack.yml"); err != nil {
377+
t.Log(err)
378+
}
379+
}
375380
hDir := handlerDir
376381
if len(hDir) == 0 {
377382
hDir = functionName
@@ -423,3 +428,58 @@ func Test_getPrefixValue_Flag(t *testing.T) {
423428
t.Errorf("want %s, got %s", want, val)
424429
}
425430
}
431+
432+
func Test_addEofNewlines_works(t *testing.T) {
433+
table := []struct {
434+
funcName string
435+
}{
436+
// TODO: after #906 is fixed
437+
// -f or --yaml will work
438+
// and we can change stack to func1
439+
{"stack"},
440+
{"func2"},
441+
{"func3"},
442+
}
443+
resetForTest()
444+
templatePullLocalTemplateRepo(t)
445+
446+
defer tearDownFetchTemplates(t)
447+
const stackFile = "stack.yml"
448+
449+
for i, row := range table {
450+
const functionLang = "ruby"
451+
452+
parameters := []string{
453+
"new",
454+
row.funcName,
455+
"--lang=" + functionLang,
456+
}
457+
458+
if i == 0 {
459+
// there's a bug where this doesn't get honored
460+
// as a workaround, func1 is "stack" in the test table
461+
// this way we predictability create stack.yml
462+
parameters = append(parameters, "--yaml="+stackFile)
463+
} else {
464+
parameters = append(parameters, "--append="+stackFile)
465+
}
466+
467+
faasCmd.SetArgs(nil)
468+
faasCmd.SetArgs(parameters)
469+
faasCmd.Execute()
470+
471+
// drop last two bytes (\n\n) for the first function
472+
if i == 0 {
473+
func(fileName string) {
474+
file, _ := os.OpenFile(fileName, os.O_RDWR, 0600)
475+
defer file.Close()
476+
fileStats, _ := file.Stat()
477+
file.Truncate(fileStats.Size() - 2)
478+
}(stackFile)
479+
}
480+
}
481+
482+
for _, row := range table {
483+
defer tearDownNewFunction(t, row.funcName)
484+
}
485+
}

0 commit comments

Comments
 (0)