-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkout.go
67 lines (58 loc) · 1.36 KB
/
checkout.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package shync
import (
"fmt"
"io"
"os"
"github.com/mash/shync/log"
"github.com/mash/shync/shopify"
)
func Checkout(c Config) error {
log.Infof("opening Chrome")
client := shopify.NewClient(c.Head)
// you might want to skip cancel() to keep the browser window around
if false {
defer client.Close()
}
// run task list
log.Infof("logging into Shopify")
if err := client.Login(c.Store, c.Username, c.Password); err != nil {
return fmt.Errorf("Checkout: %w", err)
}
for _, id := range c.Ids() {
log.Infof("checking out %s", id)
if err := checkoutSingle(c, client, id); err != nil {
return fmt.Errorf("Checkout: failed processing: %s, error: %w", id, err)
}
}
return nil
}
func checkoutSingle(c Config, client *shopify.Client, id string) error {
var subject, body string
if err := client.FetchEmailTemplate(id, &subject, &body); err != nil {
return err
}
return write(c, id, subject, body)
}
func write(c Config, id, subject, body string) error {
if c.Dir == "-" {
fmt.Println(body)
return nil
}
subjectFile := c.SubjectFile(id)
bodyFile := c.BodyFile(id)
sf, err := os.Create(subjectFile)
if err != nil {
return err
}
if _, err := io.WriteString(sf, subject); err != nil {
return err
}
bf, err := os.Create(bodyFile)
if err != nil {
return err
}
if _, err := io.WriteString(bf, body); err != nil {
return err
}
return nil
}