Skip to content

Commit a10efd5

Browse files
committed
WIP on the speaker image functionality
1 parent 7d2ecbc commit a10efd5

File tree

7 files changed

+412
-111
lines changed

7 files changed

+412
-111
lines changed

README.e.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ LicenseColor: yellow
66

77
# {{.Name}}
88

9-
{{template "badge/travis" .}} {{template "badge/appveyor" .}} {{template "badge/goreport" .}} {{template "badge/godoc" .}} {{template "license/shields" .}}
9+
{{template "badge/travis" .}} [![Build status](https://ci.appveyor.com/api/projects/status/u7pu7ins2csxngxu?svg=true)](https://ci.appveyor.com/project/DevOpsDays/devopsdays-cli) [![Coveralls](https://img.shields.io/coveralls/devopsdays/devopsdays-cli.svg)]()
10+
{{template "badge/goreport" .}} {{template "badge/godoc" .}} [![GitHub release](https://img.shields.io/github/release/devopsdays/devopsdays-cli.svg)](https://github.com/devopsdays/devopsdays-cli/releases) {{template "license/shields" .}}
1011

1112
Command-line utilities for the [devopsdays](https://www.devopsdays.org) website built with :heart: by [mattstratton](https://github.com/mattstratton) in [Go](https://golang.org/).
1213

cmd/speaker.go

Lines changed: 2 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@ package cmd
22

33
import (
44
"fmt"
5-
"log"
65

76
"github.com/devopsdays/devopsdays-cli/create"
8-
"github.com/devopsdays/devopsdays-cli/helpers"
9-
"github.com/devopsdays/devopsdays-cli/model"
107
"github.com/spf13/cobra"
11-
"github.com/tcnksm/go-input"
128
)
139

1410
// addSpeakerCmd represents the "speaker add" command
@@ -37,9 +33,9 @@ devopsdays-cli create speaker "George Bluth"`,
3733
Args: cobra.MaximumNArgs(1),
3834
Run: func(cmd *cobra.Command, args []string) {
3935
if len(args) > 0 {
40-
createSpeaker(args[0], "", "")
36+
create.CreateSpeaker(args[0], "", "")
4137
} else {
42-
createSpeaker("", "", "")
38+
create.CreateSpeaker("", "", "")
4339
}
4440
},
4541
}
@@ -118,108 +114,6 @@ func addSpeaker() {
118114
fmt.Println("You would have added a speaker to a talk if this happened")
119115
}
120116

121-
func createSpeaker(speakerName, city, year string) (err error) {
122-
// debug
123-
fmt.Println("Creating new speaker")
124-
fmt.Println("Value of city is")
125-
fmt.Println(cityFlag)
126-
// actual stuff
127-
ui := &input.UI{}
128-
129-
if city == "" {
130-
cityName, err := ui.Ask("City", &input.Options{
131-
// Read the default val from env var
132-
Required: true,
133-
Loop: true,
134-
HideOrder: true,
135-
})
136-
if err != nil {
137-
log.Fatal(err)
138-
}
139-
city = cityName
140-
}
141-
142-
if year == "" {
143-
yearName, err := ui.Ask("Year", &input.Options{
144-
// Read the default val from env var
145-
Required: true,
146-
Loop: true,
147-
HideOrder: true,
148-
})
149-
if err != nil {
150-
log.Fatal(err)
151-
}
152-
year = yearName
153-
}
154-
155-
name, err := ui.Ask("Speaker Name", &input.Options{
156-
Required: true,
157-
Loop: true,
158-
HideOrder: true,
159-
})
160-
if err != nil {
161-
log.Fatal(err)
162-
}
163-
164-
website, err := ui.Ask("Website (optional)", &input.Options{
165-
HideOrder: true,
166-
})
167-
if err != nil {
168-
log.Fatal(err)
169-
}
170-
171-
twitter, err := ui.Ask("Twitter (optional)", &input.Options{
172-
HideOrder: true,
173-
})
174-
if err != nil {
175-
log.Fatal(err)
176-
}
177-
178-
facebook, err := ui.Ask("Facebook (optional)", &input.Options{
179-
HideOrder: true,
180-
})
181-
if err != nil {
182-
log.Fatal(err)
183-
}
184-
185-
linkedin, err := ui.Ask("LinkedIn (optional)", &input.Options{
186-
HideOrder: true,
187-
})
188-
if err != nil {
189-
log.Fatal(err)
190-
}
191-
192-
github, err := ui.Ask("GitHub (optional)", &input.Options{
193-
HideOrder: true,
194-
})
195-
if err != nil {
196-
log.Fatal(err)
197-
}
198-
199-
gitlab, err := ui.Ask("GitLab (optional)", &input.Options{
200-
HideOrder: true,
201-
})
202-
if err != nil {
203-
log.Fatal(err)
204-
}
205-
206-
mySpeaker := model.Speaker{
207-
Name: helpers.NameClean(name),
208-
Title: name,
209-
Website: website,
210-
Twitter: twitter,
211-
Facebook: facebook,
212-
Linkedin: linkedin,
213-
Github: github,
214-
Gitlab: gitlab,
215-
ImagePath: "",
216-
}
217-
218-
create.NewSpeaker(mySpeaker, "ponyville", "2017")
219-
220-
return
221-
}
222-
223117
func editSpeaker(speakerName, city, year string) {
224118
fmt.Println("You would have edited a speaker if this happened")
225119
}

create/speaker.go

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import (
66
"log"
77
"os"
88
"path/filepath"
9+
"regexp"
910
"strings"
1011

1112
"text/template"
1213

1314
"github.com/devopsdays/devopsdays-cli/helpers"
1415
"github.com/devopsdays/devopsdays-cli/model"
16+
"github.com/tcnksm/go-input"
1517
)
1618

1719
const tmpl = `+++
@@ -29,6 +31,181 @@ Food-truck SpaceTeam pivot earned media agile big data entrepreneur actionable i
2931
3032
`
3133

34+
// CreateSpeaker takes input from the user to create a new speaker
35+
func CreateSpeaker(speakerName, city, year string) (err error) {
36+
37+
var imagePath string
38+
39+
ui := &input.UI{}
40+
41+
if city == "" {
42+
cityName, err := ui.Ask("City", &input.Options{
43+
// Read the default val from env var
44+
Required: true,
45+
Loop: true,
46+
HideOrder: true,
47+
})
48+
if err != nil {
49+
log.Fatal(err)
50+
}
51+
city = cityName
52+
}
53+
54+
if year == "" {
55+
yearName, err := ui.Ask("Year", &input.Options{
56+
// Read the default val from env var
57+
Required: true,
58+
Loop: true,
59+
HideOrder: true,
60+
})
61+
if err != nil {
62+
log.Fatal(err)
63+
}
64+
year = yearName
65+
}
66+
67+
name, err := ui.Ask("Speaker Name", &input.Options{
68+
Required: true,
69+
Loop: true,
70+
HideOrder: true,
71+
})
72+
if err != nil {
73+
log.Fatal(err)
74+
}
75+
76+
website, err := ui.Ask("Website (optional)", &input.Options{
77+
HideOrder: true,
78+
Required: false,
79+
Loop: true,
80+
ValidateFunc: func(s string) error {
81+
if (s != "") && (helpers.ValidateField(s, "website") != true) {
82+
return fmt.Errorf("please enter a properly formed URL")
83+
}
84+
85+
return nil
86+
},
87+
})
88+
if err != nil {
89+
log.Fatal(err)
90+
}
91+
92+
twitter, err := ui.Ask("Twitter (optional)", &input.Options{
93+
HideOrder: true,
94+
Loop: true,
95+
Required: false,
96+
ValidateFunc: func(s string) error {
97+
if (s != "") && (helpers.ValidateField(s, "twitter") != true) {
98+
return fmt.Errorf("please enter a properly formed twitter handle")
99+
}
100+
101+
return nil
102+
},
103+
})
104+
if err != nil {
105+
log.Fatal(err)
106+
}
107+
108+
facebook, err := ui.Ask("Facebook (optional)", &input.Options{
109+
HideOrder: true,
110+
Loop: true,
111+
Required: false,
112+
ValidateFunc: func(s string) error {
113+
if (s != "") && (helpers.ValidateField(s, "facebook") != true) {
114+
return fmt.Errorf("please enter a properly formed Facebook profile URL")
115+
}
116+
117+
return nil
118+
},
119+
})
120+
if err != nil {
121+
log.Fatal(err)
122+
}
123+
124+
linkedin, err := ui.Ask("LinkedIn (optional)", &input.Options{
125+
HideOrder: true, Loop: true,
126+
Required: false,
127+
ValidateFunc: func(s string) error {
128+
if (s != "") && (helpers.ValidateField(s, "linkedin") != true) {
129+
return fmt.Errorf("please enter a properly formed LinkedIn URL")
130+
}
131+
132+
return nil
133+
},
134+
})
135+
if err != nil {
136+
log.Fatal(err)
137+
}
138+
139+
github, err := ui.Ask("GitHub (optional)", &input.Options{
140+
HideOrder: true, Loop: true,
141+
Required: false,
142+
ValidateFunc: func(s string) error {
143+
if (s != "") && (helpers.ValidateField(s, "github") != true) {
144+
return fmt.Errorf("please enter a properly formed GitHub handle")
145+
}
146+
147+
return nil
148+
},
149+
})
150+
if err != nil {
151+
log.Fatal(err)
152+
}
153+
154+
gitlab, err := ui.Ask("GitLab (optional)", &input.Options{
155+
HideOrder: true,
156+
Loop: true,
157+
Required: false,
158+
ValidateFunc: func(s string) error {
159+
if (s != "") && (helpers.ValidateField(s, "gitlab") != true) {
160+
return fmt.Errorf("please enter a properly formed GitLab handle")
161+
}
162+
163+
return nil
164+
},
165+
})
166+
if err != nil {
167+
log.Fatal(err)
168+
}
169+
170+
imageQuery, err := ui.Ask("Would you like to add the speaker image now? [Y/n]", &input.Options{
171+
Default: "Y",
172+
HideOrder: true,
173+
Loop: true,
174+
Required: true,
175+
ValidateFunc: func(s string) error {
176+
if s != "Y" && s != "y" && s != "N" && s != "n" {
177+
return fmt.Errorf("input must be Y or n")
178+
}
179+
180+
return nil
181+
},
182+
})
183+
184+
if (imageQuery == "Y") || (imageQuery == "y") {
185+
imagePath = CreateSpeakerImage(helpers.NameClean(name), city, year)
186+
} else {
187+
imagePath = ""
188+
}
189+
190+
fmt.Println(imageQuery)
191+
192+
mySpeaker := model.Speaker{
193+
Name: helpers.NameClean(name),
194+
Title: name,
195+
Website: website,
196+
Twitter: twitter,
197+
Facebook: facebook,
198+
Linkedin: linkedin,
199+
Github: github,
200+
Gitlab: gitlab,
201+
ImagePath: imagePath,
202+
}
203+
204+
NewSpeaker(mySpeaker, city, year)
205+
206+
return
207+
}
208+
32209
// NewSpeaker takes in a constructed Speaker type and generates the stuff
33210
func NewSpeaker(speaker model.Speaker, city string, year string) (err error) {
34211

@@ -62,3 +239,49 @@ func NewSpeaker(speaker model.Speaker, city string, year string) (err error) {
62239
}
63240
return
64241
}
242+
243+
// CreateSpeakerImage takes in a path to an image and resizes it to the proper dimensions and copies it to the destination
244+
func CreateSpeakerImage(speaker, city, year string) (imageFile string) {
245+
ui := &input.UI{}
246+
srcPath, err := ui.Ask("Path to speaker image. Must be a PNG or JPG file.", &input.Options{
247+
Required: true,
248+
Loop: true,
249+
HideOrder: true,
250+
ValidateFunc: func(s string) error {
251+
if (s != "") && (helpers.ValidateField(s, "filepath") != true) {
252+
return fmt.Errorf("please enter a proper path")
253+
}
254+
255+
if _, err := os.Stat(s); err == nil {
256+
return fmt.Errorf("File not found.")
257+
}
258+
259+
return nil
260+
},
261+
})
262+
if err != nil {
263+
log.Fatal(err)
264+
}
265+
// create the destPath better here
266+
267+
re := regexp.MustCompile("\\.[^.]+$")
268+
ext := strings.ToLower(re.FindString(srcPath))
269+
switch ext {
270+
case "jpg":
271+
s := []string{strings.TrimSpace(speaker), ".jpg"}
272+
destPath := filepath.Join(helpers.EventContentPath(city, year), "speakers", strings.Join(s, ""))
273+
helpers.ResizeImage(srcPath, destPath, "jpg", 600, 600)
274+
return strings.Join(s, "")
275+
case "jpeg":
276+
s := []string{strings.TrimSpace(speaker), ".jpg"}
277+
destPath := filepath.Join(helpers.EventContentPath(city, year), "speakers", strings.Join(s, ""))
278+
helpers.ResizeImage(srcPath, destPath, "jpg", 600, 600)
279+
return strings.Join(s, "")
280+
case "png":
281+
s := []string{strings.TrimSpace(speaker), ".png"}
282+
destPath := filepath.Join(helpers.EventContentPath(city, year), "speakers", strings.Join(s, ""))
283+
helpers.ResizeImage(srcPath, destPath, "png", 600, 600)
284+
return strings.Join(s, "")
285+
}
286+
return ""
287+
}

0 commit comments

Comments
 (0)