Skip to content

Commit 5dc0487

Browse files
committed
Add show speaker functionality
Needs major refactoring as it is pretty slow right now and very inefficient. Fixes #58 Signed-off-by: Matt Stratton <[email protected]>
1 parent b6dd35a commit 5dc0487

File tree

5 files changed

+186
-4
lines changed

5 files changed

+186
-4
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ build/
3636

3737
vendor
3838
coverage.txt
39-
/delver.sh
39+
delver.sh

cmd/prompt.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func mainPrompt() (err error) {
1717
"Create a new event",
1818
"Create a new speaker",
1919
"Create a new sponsor",
20+
"Show a speaker",
2021
"Quit the application",
2122
},
2223
}
@@ -28,6 +29,8 @@ func mainPrompt() (err error) {
2829
createSpeakerPrompt("", "")
2930
case "Create a new sponsor":
3031
sponsor.CreateSponsor("")
32+
case "Show a speaker":
33+
showSpeakerPrompt("", "")
3134
}
3235
}
3336

@@ -59,3 +62,23 @@ func createSpeakerPrompt(city, year string) (err error) {
5962
}
6063
return
6164
}
65+
66+
func showSpeakerPrompt(city, year string) (err error) {
67+
68+
if city == "" {
69+
prompt := &survey.Input{
70+
Message: "Enter the city name:",
71+
}
72+
survey.AskOne(prompt, &city, survey.Required)
73+
}
74+
75+
if year == "" {
76+
prompt := &survey.Input{
77+
Message: "Enter the year:",
78+
}
79+
survey.AskOne(prompt, &year, survey.Required)
80+
}
81+
speaker.ShowSpeakers(city, year)
82+
83+
return
84+
}

cmd/speaker.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"fmt"
55

6+
speaker "github.com/devopsdays/devopsdays-cli/speaker"
67
"github.com/spf13/cobra"
78
)
89

@@ -81,15 +82,14 @@ var showSpeakerCmd = &cobra.Command{
8182
Use: "speaker",
8283
Short: "Show a speaker from an event",
8384
Long: `Show a speaker from an event.
84-
You can provide the speaker's name as an argument to this command, but it must be the "cleaned" name.
8585
`,
8686
Example: ` devopsdays-cli remove speaker george-bluth
8787
devopsdays-cli show speaker --city new-york --year 2017 --all
8888
devopsdays-cli remove speaker george-bluth -c "New York" --year "2017"`,
8989
Args: cobra.MaximumNArgs(1),
9090
Run: func(cmd *cobra.Command, args []string) {
9191
// TODO: Work your own magic here
92-
showSpeaker()
92+
showSpeakerPrompt(City, Year)
9393
},
9494
}
9595

@@ -129,5 +129,7 @@ func removeSpeaker() {
129129
}
130130

131131
func showSpeaker() {
132-
fmt.Println("You would have shown a speaker if this happened")
132+
fmt.Println("Showing a speaker")
133+
mySpeaker, _ := speaker.GetSpeakerInfo("fluttershy.md", "ponyville", "2017")
134+
fmt.Println(mySpeaker)
133135
}

speaker/speaker.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,47 @@ func SpeakerImage(srcPath, speaker, city, year string) (imageFile string) {
260260
}
261261
return "busted"
262262
}
263+
264+
func ShowSpeakers(city, year string) (err error) {
265+
var selection string
266+
267+
options, _ := GetSpeakers(city, year)
268+
options = append(options, "Return to Main Menu")
269+
for selection != "Return to Main Menu" {
270+
prompt := &survey.Select{
271+
Message: "Select a speaker:",
272+
Options: options,
273+
}
274+
survey.AskOne(prompt, &selection, nil)
275+
if selection == "Return to Main Menu" {
276+
return
277+
}
278+
var mySpeaker model.Speaker
279+
mySpeaker, err = GetSpeakerInfo(selection, city, year)
280+
color.Cyan("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+")
281+
fmt.Println("Name is " + mySpeaker.Title)
282+
if mySpeaker.Website != "" {
283+
fmt.Println("Website is " + mySpeaker.Website)
284+
}
285+
if mySpeaker.Twitter != "" {
286+
fmt.Println("Twitter is @" + mySpeaker.Twitter)
287+
}
288+
if mySpeaker.Facebook != "" {
289+
fmt.Println("Facebook is " + mySpeaker.Facebook)
290+
}
291+
if mySpeaker.Linkedin != "" {
292+
fmt.Println("Website is " + mySpeaker.Linkedin)
293+
}
294+
if mySpeaker.Github != "" {
295+
fmt.Println("Github is @" + mySpeaker.Github)
296+
}
297+
if mySpeaker.Gitlab != "" {
298+
fmt.Println("Gitlab is " + mySpeaker.Gitlab)
299+
}
300+
if mySpeaker.Bio != "" {
301+
fmt.Println("Bio is " + mySpeaker.Bio)
302+
}
303+
color.Cyan("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+")
304+
}
305+
return
306+
}

speaker/speaker_utils.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package speaker
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"log"
7+
"path/filepath"
8+
"strings"
9+
10+
"github.com/BurntSushi/toml"
11+
paths "github.com/devopsdays/devopsdays-cli/helpers/paths"
12+
"github.com/devopsdays/devopsdays-cli/model"
13+
"github.com/gernest/front"
14+
"github.com/pkg/errors"
15+
)
16+
17+
// GetSpeakers takes in the city and year and returns a list of the talks
18+
func GetSpeakers(city, year string) ([]string, error) {
19+
20+
speakerdir := filepath.Join(paths.EventContentPath(city, year), "speakers")
21+
22+
fmt.Println(speakerdir)
23+
files, err := ioutil.ReadDir(speakerdir)
24+
25+
if err != nil {
26+
return nil, errors.Wrap(err, "read speaker directory failed")
27+
}
28+
var s []string
29+
// s := make([]string, len(files))
30+
for _, f := range files {
31+
s = append(s, f.Name())
32+
}
33+
return s, nil
34+
}
35+
36+
func GetSpeakerInfo(file, city, year string) (speaker model.Speaker, err error) {
37+
38+
// speakerPerson := `+++
39+
// Website = ""
40+
// Title = "Rainbow Dash"
41+
// Twitter = ""
42+
// date = "2016-12-08T20:55:58-06:00"
43+
// Type = "speaker"
44+
// Image = "rainbow-dash.png"
45+
// Facebook = ""
46+
// Linkedin = ""
47+
// Pronouns = ""
48+
// +++
49+
// Food-truck SpaceTeam pivot
50+
// `
51+
filePath := filepath.Join(paths.EventContentPath(city, year), "speakers", file)
52+
dat, err := ioutil.ReadFile(filePath)
53+
// lines.WriteTo(os.Stdout)
54+
m := front.NewMatter()
55+
m.Handle("+++", TOMLHandler)
56+
57+
f, body, err := m.Parse(strings.NewReader(string(dat)))
58+
if err != nil {
59+
panic(err)
60+
}
61+
62+
speaker = model.Speaker{
63+
Name: file,
64+
Title: f["Title"].(string),
65+
Website: f["Website"].(string),
66+
Twitter: f["Twitter"].(string),
67+
Facebook: f["Facebook"].(string),
68+
Linkedin: f["Linkedin"].(string),
69+
Github: f["Github"].(string),
70+
Gitlab: f["Gitlab"].(string),
71+
ImagePath: f["ImagePath"].(string),
72+
Bio: body,
73+
}
74+
75+
return
76+
}
77+
78+
// TOMLHandler decodes ymal string into a go map[string]interface{}
79+
func TOMLHandler(front string) (map[string]interface{}, error) {
80+
81+
// type thing struct {
82+
// Name string
83+
// Title string
84+
// Website string `toml:"website,omitempty"`
85+
// Twitter string `toml:"twitter,omitempty"`
86+
// Facebook string `toml:"facebook,omitempty"`
87+
// Linkedin string `toml:"linkedin,omitempty"`
88+
// Github string `toml:"github,omitempty"`
89+
// Gitlab string `toml:"gitlab,omitempty"`
90+
// ImagePath string `toml:"image,omitempty"`
91+
// Bio string
92+
// }
93+
94+
// var stuff thing
95+
var stuff model.Speaker
96+
if _, err := toml.Decode(front, &stuff); err != nil {
97+
log.Fatal(err)
98+
}
99+
x := map[string]interface{}{
100+
"Name": stuff.Name,
101+
"Title": stuff.Title,
102+
"Website": stuff.Website,
103+
"Twitter": stuff.Twitter,
104+
"Facebook": stuff.Facebook,
105+
"Linkedin": stuff.Linkedin,
106+
"Github": stuff.Github,
107+
"Gitlab": stuff.Gitlab,
108+
"ImagePath": stuff.ImagePath,
109+
"Bio": stuff.Bio,
110+
}
111+
112+
return x, nil
113+
}

0 commit comments

Comments
 (0)