Skip to content

Commit d16a1b3

Browse files
committed
Add createSponsor function
Fixes #39 Signed-off-by: Matt Stratton <matt.stratton@gmail.com>
1 parent baceb44 commit d16a1b3

File tree

7 files changed

+242
-207
lines changed

7 files changed

+242
-207
lines changed

cmd/sponsor.go

Lines changed: 20 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
11
package cmd
22

33
import (
4-
"bufio"
5-
"errors"
64
"fmt"
7-
"image/png"
8-
"log"
9-
"os"
10-
"path/filepath"
11-
"strings"
12-
"text/template"
135

14-
rice "github.com/GeertJohan/go.rice"
15-
"github.com/nfnt/resize"
166
"github.com/spf13/cobra"
177
)
188

199
// sponsorCmd represents the sponsor command
2010
var sponsorCmd = &cobra.Command{
2111
Use: "sponsor [name]",
2212
Short: "Create a sponsor",
23-
Long: `Create a new sponsor file, and optionally add the sponsor's image.
24-
The name argument must not contain any spaces.`,
13+
Long: `Create a new sponsor file add the sponsor's image.
14+
The name argument must not contain any spaces.
15+
`,
16+
Example: ` devopsdays-cli create sponsor
17+
devopsdays-cli create sponsor bluth-company`,
2518
Run: func(cmd *cobra.Command, args []string) {
2619

2720
if len(args) > 0 {
@@ -44,23 +37,26 @@ This application is a tool to generate the needed files
4437
to quickly create a Cobra application.`,
4538
Run: func(cmd *cobra.Command, args []string) {
4639
// TODO: Work your own magic here
47-
addSponsorFake() //TODO: This is the fake one
40+
addSponsor("") //TODO: This is the fake one
4841
},
4942
}
5043

5144
// createSponsorCmd represents the "create sponsor" command
5245
var createSponsorCmd = &cobra.Command{
53-
Use: "sponsor",
54-
Short: "Create a new sponsor",
55-
Long: `A longer description that spans multiple lines and likely contains examples
56-
and usage of using your command. For example:
57-
58-
Cobra is a CLI library for Go that empowers applications.
59-
This application is a tool to generate the needed files
60-
to quickly create a Cobra application.`,
46+
Use: "sponsor [name]",
47+
Short: "Create a sponsor",
48+
Long: `Create a new sponsor file add the sponsor's image.
49+
The name argument must not contain any spaces.
50+
`,
51+
Example: ` devopsdays-cli create sponsor
52+
devopsdays-cli create sponsor bluth-company`,
6153
Run: func(cmd *cobra.Command, args []string) {
62-
// TODO: Work your own magic here
63-
createSponsor()
54+
55+
if len(args) > 0 {
56+
addSponsor(args[0])
57+
} else {
58+
addSponsor("")
59+
}
6460
},
6561
}
6662

@@ -131,14 +127,10 @@ func init() {
131127

132128
// Main functions go down here
133129

134-
func addSponsorFake() {
130+
func addSponsor(sponsor string) {
135131
fmt.Println("You would have added a sponsor if this happened")
136132
}
137133

138-
func createSponsor() {
139-
fmt.Println("You would have created a new sponsor if this happened")
140-
}
141-
142134
func editSponsor() {
143135
fmt.Println("You would have edited an existing sponsor if this happened")
144136
}
@@ -150,143 +142,3 @@ func removeSponsor() {
150142
func showSponsor() {
151143
fmt.Println("You would have shown a sponsor if this happened")
152144
}
153-
154-
func addSponsor(sponsor string) (err error) {
155-
156-
reader := bufio.NewReader(os.Stdin)
157-
if sponsor == "" {
158-
fmt.Println("Enter the sponsor's name. It must not contain any spaces: ")
159-
sponsor, _ = reader.ReadString('\n')
160-
}
161-
// Check if the sponsor exists already
162-
if checkSponsor(sponsor) == true {
163-
return errors.New("Sponsor already exists. Try adding it again, perhaps appending '-YYYY'\nFor example, 'chef-2017'")
164-
}
165-
// prompt for the path to the sponsor image file
166-
fmt.Println("Optional: Enter the path to the sponsor's image. It must be the full path. For example: `/Users/mattstratton/chef.png`. Enter return to add the sponsor image manually later.")
167-
sponsorImage, _ := reader.ReadString('\n')
168-
if sponsorImage == "\n" {
169-
fmt.Println("No sponsor image entered. Be sure to copy it to the path ", sponsorImagePath(webdir, sponsor), "later.")
170-
} else {
171-
172-
if sponsorImage = strings.TrimSpace(sponsorImage); checkSponsorImage(sponsorImage) == false {
173-
return errors.New("Sponsor image not found.")
174-
}
175-
}
176-
177-
// prompt for sponsor's name
178-
fmt.Println("Enter the sponsor's full name. For example: `Chef Software, Inc`")
179-
sponsorName, _ := reader.ReadString('\n')
180-
if sponsorName == "\n" {
181-
return errors.New("Sponsor Name is required.")
182-
}
183-
fmt.Println(sponsorName)
184-
185-
// prompt for sponsor URL
186-
fmt.Println("Enter the sponsor's URL. It must include 'http://' or 'https://'. For example: `https://www.chef.io`")
187-
sponsorUrl, _ := reader.ReadString('\n')
188-
if sponsorUrl == "\n" {
189-
return errors.New("Sponsor URL is required.")
190-
}
191-
192-
// write sponsor YAML file and copy image from path to proper destination
193-
createSponsorFile(sponsor, sponsorName, sponsorUrl)
194-
fmt.Println("Sponsor created for ", sponsorName)
195-
if sponsorImage != "\n" {
196-
resizeSponsorImage(strings.TrimSpace(sponsorImage), sponsorImagePath(webdir, sponsor))
197-
} else {
198-
fmt.Println("Don't forget to place the sponsor image at ", sponsorImagePath(webdir, sponsor))
199-
}
200-
return
201-
}
202-
203-
func createSponsorFile(sponsor, sponsorName, sponsorUrl string) (string, error) {
204-
// find a rice.Box
205-
templateBox, err := rice.FindBox("../templates")
206-
if err != nil {
207-
log.Fatal(err)
208-
}
209-
// get file contents as string
210-
templateString, err := templateBox.String("sponsor.yml.tmpl")
211-
if err != nil {
212-
log.Fatal(err)
213-
}
214-
// t := template.Must(template.New("sponsor.yml.tmpl").ParseFiles("templates/sponsor.yml.tmpl"))
215-
t, err := template.New("sponsor.yml").Parse(templateString)
216-
data := struct {
217-
Name string
218-
Url string
219-
}{
220-
strings.TrimSpace(sponsorName),
221-
strings.TrimSpace(sponsorUrl),
222-
}
223-
f, err := os.Create(sponsorDataPath(webdir, sponsor))
224-
if err != nil {
225-
return "", err
226-
}
227-
defer f.Close()
228-
t.Execute(f, data)
229-
if err != nil {
230-
fmt.Println(err)
231-
} else {
232-
fmt.Println("Created sponsor file for", sponsor, "at", sponsorDataPath(webdir, sponsor))
233-
}
234-
return sponsor, nil
235-
236-
}
237-
238-
// checkSponsor takes in one argument, the name of a sponsor, and returns true if the sponsor already exists.
239-
func checkSponsor(sponsor string) bool {
240-
fmt.Println(sponsorDataPath(webdir, sponsor))
241-
if _, err := os.Stat(sponsorDataPath(webdir, sponsor)); err == nil {
242-
return true
243-
}
244-
return false
245-
246-
}
247-
248-
func checkSponsorImage(path string) bool {
249-
fmt.Println(path)
250-
if _, err := os.Stat(path); err == nil {
251-
return true
252-
}
253-
return false
254-
255-
}
256-
257-
func sponsorDataPath(webdir, sponsor string) (sponsorDataPath string) {
258-
s := []string{strings.TrimSpace(sponsor), ".yml"}
259-
// sponsorDataPath = strings.Join(s, "")
260-
sponsorDataPath = filepath.Join(webdir, "data", "sponsors", strings.Join(s, ""))
261-
return sponsorDataPath
262-
}
263-
264-
func sponsorImagePath(webdir, sponsor string) (sponsorImagePath string) {
265-
s := []string{webdir, "/static/img/sponsors/", strings.TrimSpace(sponsor), ".png"}
266-
sponsorImagePath = strings.Join(s, "")
267-
return sponsorImagePath
268-
}
269-
270-
func resizeSponsorImage(srcPath, destPath string) {
271-
fmt.Println("Resizing image")
272-
file, err := os.Open(srcPath)
273-
if err != nil {
274-
log.Fatal(err)
275-
}
276-
277-
img, err := png.Decode(file)
278-
if err != nil {
279-
log.Fatal(err)
280-
}
281-
file.Close()
282-
283-
m := resize.Resize(600, 0, img, resize.Lanczos3)
284-
285-
out, err := os.Create(destPath)
286-
if err != nil {
287-
log.Fatal(err)
288-
}
289-
defer out.Close()
290-
291-
png.Encode(out, m)
292-
}

cmd/sponsor_test.go

Lines changed: 0 additions & 36 deletions
This file was deleted.

model/sponsor.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package model
22

33
// Sponsor represents a sponsor inside a sponsor data file
44
type Sponsor struct {
5-
Name string `json:"name"`
6-
URL string `json:"url"`
7-
Twitter string `json:"twitter"`
5+
Name string `json:"name"`
6+
URL string `json:"url"`
87
}

0 commit comments

Comments
 (0)