Skip to content

Commit f2f5ae2

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

8 files changed

Lines changed: 367 additions & 325 deletions

File tree

speaker/create_speaker.go

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
package speaker
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"os"
7+
8+
"github.com/devopsdays/devopsdays-cli/helpers"
9+
"github.com/devopsdays/devopsdays-cli/names"
10+
"github.com/devopsdays/devopsdays-cli/talk"
11+
"github.com/fatih/color"
12+
survey "gopkg.in/AlecAivazis/survey.v1"
13+
)
14+
15+
// Prompts for a new speaker
16+
var qsCreateSpeaker = []*survey.Question{
17+
{
18+
Name: "name",
19+
Prompt: &survey.Input{
20+
Message: "What is the speaker's name?",
21+
Help: "This is the speaker's full name",
22+
},
23+
Validate: survey.Required,
24+
},
25+
{
26+
Name: "bio",
27+
Prompt: &survey.Editor{Message: "Enter the speaker's bio [Enter to launch editor]"},
28+
},
29+
{
30+
Name: "website",
31+
Prompt: &survey.Input{
32+
Message: "What is the speaker's website? [optional]",
33+
Help: "This should be the full URL to the speaker's website. Example: https://www.mattstratton.com",
34+
},
35+
Validate: func(val interface{}) error {
36+
if str, _ := val.(string); (str != "") && (helpers.ValidateField(str, "facebook") == false) {
37+
return errors.New("Please enter a valid URL.")
38+
}
39+
return nil
40+
},
41+
},
42+
{
43+
Name: "twitter",
44+
Prompt: &survey.Input{
45+
Message: "What is the speaker's Twitter? [optional]",
46+
Help: "Twitter username can include the @ symbol or not. Examples: '@mattstratton' or 'mattstratton",
47+
},
48+
Validate: func(val interface{}) error {
49+
if str, _ := val.(string); (str != "") && (helpers.ValidateField(str, "twitter") == false) {
50+
return errors.New("Please enter a valid Twitter handle. It should not have any spaces.")
51+
}
52+
return nil
53+
},
54+
},
55+
{
56+
Name: "facebook",
57+
Prompt: &survey.Input{
58+
Message: "What is the speaker's Facebook URL? [optional]",
59+
Help: "This should be the full URL to the speaker's Facebook page. Example: https://www.facebook.com/matt.stratton",
60+
},
61+
Validate: func(val interface{}) error {
62+
if str, _ := val.(string); (str != "") && (helpers.ValidateField(str, "facebook") == false) {
63+
return errors.New("Please enter a valid Facebook URL. It should be a URL.")
64+
}
65+
return nil
66+
},
67+
},
68+
{
69+
Name: "linkedin",
70+
Prompt: &survey.Input{
71+
Message: "What is the speaker's LinkedIn URL? [optional]",
72+
Help: "This should be the full URL to the speaker's LinkedIn profile. Example: https://www.linkedin.com/in/mattstratton/",
73+
},
74+
Validate: func(val interface{}) error {
75+
if str, _ := val.(string); (str != "") && (helpers.ValidateField(str, "linkedin")) == false {
76+
return errors.New("Please enter a valid LinkedIn URL. It should be a URL.")
77+
}
78+
return nil
79+
},
80+
},
81+
{
82+
Name: "github",
83+
Prompt: &survey.Input{
84+
Message: "What is the speaker's GitHub username? [optional]",
85+
Help: "This should be the username, not the URL. Example: mattstratton",
86+
},
87+
Validate: func(val interface{}) error {
88+
if str, _ := val.(string); (str != "") && (helpers.ValidateField(str, "github")) == false {
89+
return errors.New("Please enter a valid GitHub username. It should be the username, not URL.")
90+
}
91+
return nil
92+
},
93+
},
94+
{
95+
Name: "gitlab",
96+
Prompt: &survey.Input{
97+
Message: "What is the speaker's GitLab username? [optional]",
98+
Help: "This should be the username, not the URL. Example: mattstratton",
99+
},
100+
Validate: func(val interface{}) error {
101+
if str, _ := val.(string); (str != "") && (helpers.ValidateField(str, "gitlab")) == false {
102+
return errors.New("Please enter a valid GitLab username. It should be the username, not URL.")
103+
}
104+
return nil
105+
},
106+
},
107+
{
108+
Name: "imagepath",
109+
Prompt: &survey.Input{
110+
Message: "Enter the path to the speaker's image. [optional]",
111+
Help: "Path to speaker image. Must be a PNG or JPG file. Example: /Users/mattstratton/Pictures/matt-stratton.png",
112+
},
113+
Validate: func(val interface{}) error {
114+
str, _ := val.(string)
115+
if str != "" {
116+
if _, err := os.Stat(str); err != nil {
117+
return errors.New("File not found.")
118+
}
119+
}
120+
121+
return nil
122+
},
123+
},
124+
}
125+
126+
// CreateSpeaker takes input from the user to create a new speaker
127+
func CreateSpeaker(speakerName, city, year string) (err error) {
128+
129+
answers := struct {
130+
Name string
131+
Bio string
132+
Website string
133+
Twitter string
134+
Facebook string
135+
Linkedin string
136+
Github string
137+
Gitlab string
138+
ImagePath string
139+
Talk string
140+
}{}
141+
142+
surveyErr := survey.Ask(qsCreateSpeaker, &answers)
143+
if surveyErr != nil {
144+
fmt.Println(surveyErr.Error())
145+
return
146+
}
147+
148+
name := false
149+
prompt := &survey.Confirm{
150+
Message: "Do you want to add this speaker to an existing talk?",
151+
}
152+
survey.AskOne(prompt, &name, nil)
153+
154+
t := ""
155+
if name == true {
156+
myList, _ := talk.GetTalks(city, year)
157+
prompt := &survey.Select{
158+
Message: "Choose a talk:",
159+
Options: myList,
160+
}
161+
survey.AskOne(prompt, &t, nil)
162+
color.Yellow("NOT IMPLEMENTED")
163+
}
164+
165+
if answers.ImagePath != "" {
166+
answers.ImagePath = SpeakerImage(answers.ImagePath, names.NameClean(answers.Name), city, year)
167+
}
168+
169+
mySpeaker := Speaker{
170+
Name: names.NameClean(answers.Name),
171+
Title: answers.Name,
172+
Website: answers.Website,
173+
Twitter: helpers.TwitterClean(answers.Twitter),
174+
Facebook: answers.Facebook,
175+
Linkedin: answers.Linkedin,
176+
Github: answers.Github,
177+
Gitlab: answers.Gitlab,
178+
ImagePath: answers.ImagePath,
179+
Bio: answers.Bio,
180+
}
181+
182+
NewSpeaker(mySpeaker, city, year)
183+
184+
return
185+
}

speaker/list_speaker_names.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package speaker
2+
3+
func listSpeakerNames(speakers []string, city string, year string) (speakerFullNames []string, err error) {
4+
for _, f := range speakers {
5+
var mySpeaker Speaker
6+
mySpeaker, err = GetSpeakerInfo(f, city, year)
7+
speakerFullNames = append(speakerFullNames, mySpeaker.Title)
8+
}
9+
return
10+
}

speaker/new_speaker.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package speaker
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"path/filepath"
8+
"strings"
9+
10+
"text/template"
11+
12+
"github.com/devopsdays/devopsdays-cli/helpers/paths"
13+
"github.com/devopsdays/devopsdays-cli/names"
14+
"github.com/fatih/color"
15+
)
16+
17+
// NewSpeaker takes in a constructed Speaker type and generates the stuff
18+
func NewSpeaker(speaker Speaker, city string, year string) (err error) {
19+
20+
cleanName := names.NameClean(speaker.Name)
21+
t := template.New("Speaker template")
22+
23+
t, err = t.Parse(speakerTmpl)
24+
if err != nil {
25+
log.Fatal("Parse: ", err)
26+
return
27+
}
28+
29+
if err := os.MkdirAll(filepath.Join(paths.EventContentPath(city, year), "speakers"), 0777); err != nil {
30+
log.Fatal(err)
31+
}
32+
s := []string{strings.TrimSpace(cleanName), ".md"}
33+
f, err := os.Create(filepath.Join(paths.EventContentPath(city, year), "speakers", strings.Join(s, "")))
34+
if err != nil {
35+
return err
36+
}
37+
defer f.Close()
38+
t.Execute(f, speaker)
39+
40+
if err != nil {
41+
fmt.Println(err)
42+
} else {
43+
fmt.Fprintf(color.Output, "\n\n\nCreated speaker file for %s\n", color.GreenString(speaker.Title))
44+
fmt.Fprintf(color.Output, "at %s\n\n\n", color.BlueString(filepath.Join(paths.EventContentPath(city, year), "speakers", strings.Join(s, ""))))
45+
}
46+
return
47+
}

speaker/show_speakers.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package speaker
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/devopsdays/devopsdays-cli/names"
8+
"github.com/fatih/color"
9+
survey "gopkg.in/AlecAivazis/survey.v1"
10+
)
11+
12+
func ShowSpeakers(city, year string) (err error) {
13+
var selection string
14+
15+
speakerList, _ := GetSpeakers(city, year)
16+
options2, _ := listSpeakerNames(speakerList, city, year)
17+
18+
options2 = append(options2, "Return to Main Menu")
19+
for selection != "Return to Main Menu" {
20+
prompt := &survey.Select{
21+
Message: "Select a speaker:",
22+
Options: options2,
23+
}
24+
survey.AskOne(prompt, &selection, nil)
25+
if selection == "Return to Main Menu" {
26+
return
27+
}
28+
speakerFileName := strings.Join([]string{strings.TrimSpace(names.NameClean(selection)), ".md"}, "")
29+
30+
var mySpeaker Speaker
31+
mySpeaker, err = GetSpeakerInfo(speakerFileName, city, year)
32+
fmt.Println()
33+
color.Cyan("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+")
34+
fmt.Fprintf(color.Output, "%s %s\n", color.CyanString("Name: "), color.GreenString(mySpeaker.Title))
35+
36+
if mySpeaker.Website != "" {
37+
fmt.Fprintf(color.Output, "%s %s\n", color.CyanString("WebsiteName: "), color.GreenString(mySpeaker.Website))
38+
}
39+
if mySpeaker.Twitter != "" {
40+
fmt.Fprintf(color.Output, "%s %s\n", color.CyanString("Twitter: "), color.GreenString(fmt.Sprintf("@%s", mySpeaker.Twitter)))
41+
}
42+
if mySpeaker.Facebook != "" {
43+
fmt.Fprintf(color.Output, "%s %s\n", color.CyanString("Facebook: "), color.GreenString(mySpeaker.Facebook))
44+
}
45+
if mySpeaker.Linkedin != "" {
46+
fmt.Fprintf(color.Output, "%s %s\n", color.CyanString("LinkedIn: "), color.GreenString(mySpeaker.Linkedin))
47+
}
48+
if mySpeaker.Github != "" {
49+
fmt.Fprintf(color.Output, "%s %s\n", color.CyanString("GitHub: "), color.GreenString(mySpeaker.Github))
50+
}
51+
if mySpeaker.Gitlab != "" {
52+
fmt.Fprintf(color.Output, "%s %s\n", color.CyanString("GitLab: "), color.GreenString(mySpeaker.Gitlab))
53+
}
54+
if mySpeaker.Bio != "" {
55+
fmt.Fprintf(color.Output, "%s %s\n", color.CyanString("Bio: "), color.GreenString(mySpeaker.Bio))
56+
}
57+
color.Cyan("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+")
58+
fmt.Println()
59+
}
60+
return
61+
}

0 commit comments

Comments
 (0)