Final changes for v3.0.0

- Add animation when bloggifying
- JSON payload should include city, author, and email address
- Revert to original return types for bloggify()
master
Abner Coimbre 2026-01-25 15:35:45 -08:00
parent c8fec23964
commit 4f10099b41
1 changed files with 70 additions and 17 deletions

87
main.go
View File

@ -192,9 +192,8 @@ func sendTest(cfg *Config, markdownFile string) {
/* Convert Markdown to Blog Post */ /* Convert Markdown to Blog Post */
/* *** */ /* *** */
url, err := bloggify(cfg, subject, body, false) url, valid := bloggify(cfg, subject, body, false)
if err != nil { if !valid {
fmt.Printf("Error converting '%s' into a blog post\n%v\n", markdownFile, err)
return return
} }
@ -218,71 +217,114 @@ func sendTest(cfg *Config, markdownFile string) {
os.WriteFile(sha1File, []byte(sumString), 0666) os.WriteFile(sha1File, []byte(sumString), 0666)
} }
func bloggify(cfg *Config, subject, body string, isLive bool) (string, error) { func bloggify(cfg *Config, subject, body string, isLive bool) (string, bool) {
if !isLive { if !isLive {
return "https://handmadecities.com/meetup-invite-test", nil return "https://handmadecities.com/meetupinvite2000", true
} }
/* Prepare request payload */ /* Prepare request payload */
/* *** */ /* *** */
payload := map[string]string{ payload := map[string]string{
"city": cfg.HMC.City,
"author": cfg.Postmark.SenderName,
"email": cfg.Postmark.SenderEmail,
"subject": subject, "subject": subject,
"body": body, "body": body,
} }
payloadBytes, err := json.Marshal(payload) payloadBytes, err := json.Marshal(payload)
if err != nil { if err != nil {
return "", fmt.Errorf("error encoding payload: %v", err) fmt.Printf("Error encoding payload: %v\n", err)
return "", false
} }
/* Prepare request */ /* Prepare request */
/* *** */ /* *** */
req, err := http.NewRequest("POST", "https://deploy.handmadecities.com/v1/bloggify", bytes.NewReader(payloadBytes)) req, err := http.NewRequest("POST", "https://deploy.handmadecities.com/v1/bloggify", bytes.NewReader(payloadBytes))
if err != nil { if err != nil {
return "", fmt.Errorf("error creating request: %v", err) fmt.Printf("Error creating request: %v\n", err)
return "", false
} }
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", cfg.HMC.SharedSecret) req.Header.Set("Authorization", cfg.HMC.SharedSecret)
/* Spinner while request runs */
/* *** */
stop := make(chan struct{})
done := make(chan struct{})
go func() {
frames := []rune{'⠁', '⠂', '⠄', '⡀', '⢀', '⠠', '⠐', '⠈'}
i := 0
for {
select {
case <-stop:
close(done)
return
default:
fmt.Printf("\r📝 Bloggifying your email... %c", frames[i%len(frames)])
i++
time.Sleep(100 * time.Millisecond)
}
}
}()
/* Execute request */ /* Execute request */
/* *** */ /* *** */
client := http.Client{} client := http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
return "", fmt.Errorf("error making request: %v", err) close(stop)
<-done
fmt.Printf("\r\033[31m📝 Failed to bloggify: %v\033[0m\n", err)
return "", false
} }
defer resp.Body.Close() defer resp.Body.Close()
/* Fail if status code is not in the 2xx range */ /* Fail if status code is not in the 2xx range */
/* *** */ /* *** */
if resp.StatusCode < 200 || resp.StatusCode >= 300 { if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return "", fmt.Errorf("error: HTTP status code %d", resp.StatusCode) close(stop)
<-done
fmt.Printf("\r\033[31m📝 Bloggify error: HTTP status code %d\033[0m\n", resp.StatusCode)
return "", false
} }
/* Read response's body */ /* Read response's body */
/* *** */ /* *** */
respBody, err := ioutil.ReadAll(resp.Body) respBody, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return "", fmt.Errorf("error reading response body: %v", err) close(stop)
<-done
fmt.Printf("\r\033[31m📝 Error reading bloggify response: %v\033[0m\n", err)
return "", false
} }
/* Parse JSON response */ /* Parse JSON response */
/* *** */ /* *** */
var data map[string]interface{} var data map[string]interface{}
if err := json.Unmarshal(respBody, &data); err != nil { if err := json.Unmarshal(respBody, &data); err != nil {
fmt.Printf("Error unmarshalling bloggify JSON: %v\n", err) close(stop)
return "", fmt.Errorf("error unmarshalling JSON: %v", err) <-done
fmt.Printf("\r\033[31m📝 Error parsing bloggify JSON: %v\033[0m\n", err)
return "", false
} }
/* Extract the URL from the response */ /* Extract the URL from the response */
/* *** */ /* *** */
url, ok := data["url"].(string) url, ok := data["url"].(string)
if !ok || url == "" { if !ok || url == "" {
return "", fmt.Errorf("response format error: 'url' field not found or empty") close(stop)
<-done
fmt.Printf("\r\033[31m📝 Bloggify response missing 'url' field\033[0m\n")
return "", false
} }
return url, nil close(stop)
<-done
fmt.Printf("\r\033[32m📝 Your email is a blog post at %s\033[0m\n", url)
return url, true
} }
func sendNews(cfg *Config, markdownFile string) { func sendNews(cfg *Config, markdownFile string) {
@ -307,8 +349,8 @@ func sendNews(cfg *Config, markdownFile string) {
/* Convert Markdown to Blog Post */ /* Convert Markdown to Blog Post */
/* *** */ /* *** */
url, err := bloggify(cfg, subject, body, true) url, valid := bloggify(cfg, subject, body, true)
if err != nil { if !valid {
fmt.Printf("Error converting '%s' into a blog post\n%v\n", markdownFile, err) fmt.Printf("Error converting '%s' into a blog post\n%v\n", markdownFile, err)
return return
} }
@ -1051,7 +1093,7 @@ func main() {
* Batch sends your invite using our Email API (Postmark) * Batch sends your invite using our Email API (Postmark)
* Produces .track file that tracks email addresses we attempted to send to * Produces .track file that tracks email addresses we attempted to send to
* Produces .log file with information received back from Postmark * Produces .log file with information received back from Postmark
* You can always blast again: useful when emailing new subscribers. Addresses already in .track file will be skipped * You can always blast again: addresses in .track files are skipped
--- ---
@ -1104,6 +1146,17 @@ func main() {
cmd.Usage() cmd.Usage()
os.Exit(1) os.Exit(1)
} }
reader := bufio.NewReader(os.Stdin)
fmt.Printf("Ready to blast to everyone in %s? (Y/n): ", config.HMC.City)
resp, _ := reader.ReadString('\n')
resp = strings.TrimSpace(strings.ToLower(resp))
if resp != "" && resp != "y" && resp != "yes" {
fmt.Println("Blast cancelled.")
return
}
updateFile := args[0] updateFile := args[0]
sendNews(config, updateFile) sendNews(config, updateFile)
}, },