Skip to content

Commit e44fa21

Browse files
committed
Add error handling and rename index to welcome
We now use a welcome page instead of an index page for the events. Signed-off-by: Matt Stratton <matt.stratton@gmail.com>
1 parent 302d606 commit e44fa21

5 files changed

Lines changed: 40 additions & 41 deletions

File tree

event/event.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
package event
33

44
import (
5-
"errors"
65
"fmt"
76
"log"
87
"os"
@@ -12,6 +11,8 @@ import (
1211

1312
"text/template"
1413

14+
"github.com/pkg/errors"
15+
1516
helpers "github.com/devopsdays/devopsdays-cli/helpers"
1617
paths "github.com/devopsdays/devopsdays-cli/helpers/paths"
1718
"github.com/devopsdays/devopsdays-cli/images"
@@ -227,14 +228,14 @@ func CreateEvent(city, year string) (err error) {
227228
NewEvent(myEvent, CityClean(city), year)
228229

229230
// create the event content files
230-
contentfiles := []string{"index", "conduct", "contact", "location", "program", "propose", "registration", "sponsor"}
231+
contentfiles := []string{"welcome", "conduct", "contact", "location", "program", "propose", "registration", "sponsor"}
231232
for _, contentFile := range contentfiles {
232233

233-
if result, err := createEventContentFile(city, year, contentFile); err != nil {
234+
if err := createEventContentFile(city, year, contentFile); err != nil {
234235
fmt.Printf("Error: %s\n", err)
235-
} else {
236-
fmt.Printf("Event content file created for %s!!!\n", result)
236+
os.Exit(1)
237237
}
238+
fmt.Printf("Event content file created for %s!!!\n", city)
238239

239240
}
240241
if answers.LogoPath != "" {
@@ -280,8 +281,7 @@ func Logo(srcPath, city, year string) (err error) {
280281
err = helpers.CopyFile(srcPath, filepath.Join(eventStaticPath, "logo.png"))
281282

282283
if err != nil {
283-
fmt.Println(err)
284-
return err
284+
return errors.Wrapf(err, "cannot copy logo for %s %s", city, year)
285285
}
286286
return nil
287287

@@ -294,9 +294,10 @@ func LogoSquare(srcPath, city, year string) (err error) {
294294
log.Fatal(err)
295295
}
296296
destPath := filepath.Join(eventStaticPath, "logo-square.png")
297-
images.ResizeImage(srcPath, destPath, "png", 600, 600)
298-
299-
// @todo update helpers.ResizeImage to return error code and do something with it here
297+
err = images.ResizeImage(srcPath, destPath, "png", 600, 600)
298+
if err != nil {
299+
return errors.Wrap(err, "cannot resize image")
300+
}
300301

301302
return nil
302303
}

event/make_event_files.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package event
22

33
import (
4-
"fmt"
5-
"log"
64
"os"
75
"path/filepath"
86
"strings"
@@ -11,32 +9,33 @@ import (
119

1210
rice "github.com/GeertJohan/go.rice"
1311
"github.com/devopsdays/devopsdays-cli/helpers/paths"
12+
"github.com/pkg/errors"
1413
)
1514

16-
func createEventContentFile(city, year, page string) (string, error) {
15+
func createEventContentFile(city, year, page string) error {
1716

1817
err := os.MkdirAll((paths.EventContentPath(city, year)), 0755)
1918
if err != nil {
20-
return "", err
19+
return errors.Wrap(err, "make event content directory failed")
2120
}
2221

2322
// find a rice.Box
2423
// to compile, cd to event directory and run `rice embed-go`
2524
templateBox, err := rice.FindBox("templates")
2625
if err != nil {
27-
log.Fatal(err)
26+
return errors.Wrap(err, "content template find failed")
2827
}
2928
templateName := page + ".md.tmpl"
3029
// get file contents as string
3130
templateString, err := templateBox.String(templateName)
3231
if err != nil {
33-
log.Fatal(err)
32+
return errors.Wrapf(err, "cannot load template for %s", templateName)
3433
}
3534
s := []string{strings.TrimSpace(year), "-", strings.Replace(strings.TrimSpace(strings.ToLower(city)), " ", "-", 10)}
3635
slug := strings.Join(s, "")
3736
t, err := template.New(page+".md").Delims("[[", "]]").Parse(templateString)
3837
if err != nil {
39-
log.Fatal(err)
38+
return errors.Wrapf(err, "template parse failed for %s", templateName)
4039
}
4140
data := struct {
4241
City string
@@ -52,15 +51,13 @@ func createEventContentFile(city, year, page string) (string, error) {
5251
filePath := filepath.Join((paths.EventContentPath(city, year)), (page + ".md"))
5352
f, err := os.Create(filePath)
5453
if err != nil {
55-
return "Cannot create", err
54+
return errors.Wrapf(err, "cannot create file for %s", filePath)
5655
}
5756
defer f.Close()
5857
t.Execute(f, data)
5958
if err != nil {
60-
fmt.Println(err, "template execute error")
61-
} else {
62-
fmt.Println("Created event content file for", city, "for year", year, "at", filePath)
59+
return errors.Wrapf(err, "template execute error for %s", templateName)
6360
}
64-
return city, nil
61+
return nil
6562

6663
}

0 commit comments

Comments
 (0)