Skip to content

Commit e564cc7

Browse files
committed
Refactor talk into smaller chunks
Signed-off-by: Matt Stratton <matt.stratton@gmail.com>
1 parent a311830 commit e564cc7

4 files changed

Lines changed: 101 additions & 80 deletions

File tree

talk/get_talk_info.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ import (
88

99
"github.com/BurntSushi/toml"
1010
"github.com/devopsdays/devopsdays-cli/helpers/paths"
11-
"github.com/devopsdays/devopsdays-cli/model"
1211
"github.com/gernest/front"
1312
"github.com/pkg/errors"
1413
)
1514

1615
// GetTalkInfo loads in a talk file and returns a struct with the information, decoded
1716
// from the TOML in the frontmatter
18-
func GetTalkInfo(file, city, year string) (talk *model.Talk, err error) {
17+
func GetTalkInfo(file, city, year string) (talk *Talk, err error) {
1918

2019
filePath := filepath.Join(paths.EventContentPath(city, year), "program", file)
2120
dat, err := ioutil.ReadFile(filePath)
@@ -30,7 +29,7 @@ func GetTalkInfo(file, city, year string) (talk *model.Talk, err error) {
3029
return talk, errors.Wrap(err, "get list of talks failed")
3130
}
3231

33-
talk = &model.Talk{
32+
talk = &Talk{
3433
Name: file,
3534
Title: f["Title"].(string),
3635
Description: f["Description"].(string),
@@ -51,7 +50,7 @@ func GetTalkInfo(file, city, year string) (talk *model.Talk, err error) {
5150
// TOMLHandler decodes TOML string into a go map[string]interface{}
5251
func TOMLHandler(front string) (map[string]interface{}, error) {
5352

54-
var thisTalk model.Talk
53+
var thisTalk Talk
5554
if _, err := toml.Decode(front, &thisTalk); err != nil {
5655

5756
log.Fatal(err)
@@ -72,4 +71,5 @@ func TOMLHandler(front string) (map[string]interface{}, error) {
7271
}
7372

7473
return x, nil
74+
7575
}

talk/load_talks.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package talk
2+
3+
import (
4+
"io/ioutil"
5+
"path/filepath"
6+
7+
"github.com/devopsdays/devopsdays-cli/helpers/paths"
8+
"github.com/pkg/errors"
9+
)
10+
11+
func loadTalks(city, year string) (talks []*Talk, err error) {
12+
talksDir := filepath.Join(paths.EventContentPath(city, year), "program")
13+
files, err := ioutil.ReadDir(talksDir)
14+
if err != nil {
15+
return nil, errors.Wrap(err, "read directory failed")
16+
}
17+
18+
for _, f := range files {
19+
thisTalk, _ := GetTalkInfo(f.Name(), city, year)
20+
talks = append(talks, thisTalk)
21+
}
22+
23+
return
24+
}

talk/show_talks.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package talk
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
"strings"
7+
8+
"github.com/AlecAivazis/survey"
9+
"github.com/fatih/color"
10+
)
11+
12+
func ShowTalks(city, year string) (err error) {
13+
var selection string
14+
15+
myTalks, err := loadTalks(city, year)
16+
17+
var options2 []string
18+
19+
for _, d := range myTalks {
20+
options2 = append(options2, d.Title)
21+
}
22+
options2 = append(options2, "Return to Main Menu")
23+
for selection != "Return to Main Menu" {
24+
prompt := &survey.Select{
25+
Message: "Select a talk:",
26+
Options: options2,
27+
}
28+
survey.AskOne(prompt, &selection, nil)
29+
if selection == "Return to Main Menu" {
30+
return
31+
}
32+
var myTalk *Talk
33+
for _, d := range myTalks {
34+
if d.Title == selection {
35+
myTalk = d
36+
break
37+
}
38+
}
39+
s := reflect.ValueOf(myTalk).Elem()
40+
typeOfT := s.Type()
41+
fmt.Println()
42+
color.Cyan("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+")
43+
for i := 0; i < s.NumField(); i++ {
44+
f := s.Field(i)
45+
if (typeOfT.Field(i).Name != "Name") && (f.Interface() != "") {
46+
if f.Type() != reflect.TypeOf("") {
47+
continue
48+
}
49+
str := f.Interface().(string)
50+
str = strings.TrimSpace(str)
51+
fmt.Fprintf(color.Output, "%s: %s\n", color.CyanString(typeOfT.Field(i).Name), color.GreenString(str))
52+
}
53+
}
54+
color.Cyan("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+")
55+
fmt.Println()
56+
57+
}
58+
return
59+
}

talk/talk.go

Lines changed: 14 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -2,80 +2,18 @@
22
// It also includes supporting and helper functions that are talk-releated.
33
package talk
44

5-
import (
6-
"fmt"
7-
"io/ioutil"
8-
"path/filepath"
9-
"reflect"
10-
"strings"
11-
12-
"github.com/AlecAivazis/survey"
13-
"github.com/devopsdays/devopsdays-cli/helpers/paths"
14-
"github.com/devopsdays/devopsdays-cli/model"
15-
"github.com/fatih/color"
16-
"github.com/pkg/errors"
17-
)
18-
19-
func ShowTalks(city, year string) (err error) {
20-
var selection string
21-
22-
myTalks, err := loadTalks(city, year)
23-
24-
var options2 []string
25-
26-
for _, d := range myTalks {
27-
options2 = append(options2, d.Title)
28-
}
29-
options2 = append(options2, "Return to Main Menu")
30-
for selection != "Return to Main Menu" {
31-
prompt := &survey.Select{
32-
Message: "Select a talk:",
33-
Options: options2,
34-
}
35-
survey.AskOne(prompt, &selection, nil)
36-
if selection == "Return to Main Menu" {
37-
return
38-
}
39-
var myTalk *model.Talk
40-
for _, d := range myTalks {
41-
if d.Title == selection {
42-
myTalk = d
43-
break
44-
}
45-
}
46-
s := reflect.ValueOf(myTalk).Elem()
47-
typeOfT := s.Type()
48-
fmt.Println()
49-
color.Cyan("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+")
50-
for i := 0; i < s.NumField(); i++ {
51-
f := s.Field(i)
52-
if (typeOfT.Field(i).Name != "Name") && (f.Interface() != "") {
53-
if f.Type() != reflect.TypeOf("") {
54-
continue
55-
}
56-
str := f.Interface().(string)
57-
str = strings.TrimSpace(str)
58-
fmt.Fprintf(color.Output, "%s: %s\n", color.CyanString(typeOfT.Field(i).Name), color.GreenString(str))
59-
}
60-
}
61-
color.Cyan("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+")
62-
fmt.Println()
63-
64-
}
65-
return
66-
}
67-
68-
func loadTalks(city, year string) (talks []*model.Talk, err error) {
69-
talksDir := filepath.Join(paths.EventContentPath(city, year), "program")
70-
files, err := ioutil.ReadDir(talksDir)
71-
if err != nil {
72-
return nil, errors.Wrap(err, "read directory failed")
73-
}
74-
75-
for _, f := range files {
76-
thisTalk, _ := GetTalkInfo(f.Name(), city, year)
77-
talks = append(talks, thisTalk)
78-
}
79-
80-
return
5+
// Talk defines a devopsdays event's talk
6+
type Talk struct {
7+
Name string
8+
Title string
9+
Description string `toml:"description,omitempty"`
10+
Speakers []string `toml:"speakers, omitempty"`
11+
YouTube string `toml:"youtube,omitempty"`
12+
Vimeo string `toml:"vimeo,omitempty"`
13+
Speakerdeck string `toml:"speakerdeck,omitempty"`
14+
Slideshare string `toml:"slideshare,omitempty"`
15+
Googleslides string `toml:"googleslides,omitempty"`
16+
PDF string `toml:"pdf,omitempty"`
17+
Slides string `toml:"slides,omitempty"`
18+
Abstract string
8119
}

0 commit comments

Comments
 (0)