-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
167 lines (135 loc) · 4.19 KB
/
main.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"flag"
"fmt"
"net/http"
"os"
"strings"
"time"
shopify "github.com/bold-commerce/go-shopify/v3"
"github.com/screenstaring/shopify_id_export/exportformat"
)
const version = "v0.0.6"
const shopifyFields = "id,title,product_type,handle,variants"
const usage = `shopify_id_export [hjv] [-k key] [-p password] [-r root-property] [-t token] shop
Options
-h --help display this help message
-j --json output dump as JSON
-k --key key Shopify API key; defaults to the SHOPIFY_API_KEY environment variable
-p --password password Shopify API password; defaults to the SHOPIFY_API_PASSWORD environment variable
-r --json-root property use property as the top-level property for each JSON object
--timeout integer set Shopify client timeout (default: 10 seconds)
-s --size integer page size to use when retrieving products (default: 250)
-t --token token Shopify API token; defaults to the SHOPIFY_API_TOKEN environment variable
-v --version display version information
--verbose output Shopify API request/response (default: false)
By default data is output to a CSV file.
Valid properties for the --json-root option are: %s
`
type dumper interface {
Dump(shopify.Product) error
Close() error
}
func exitFailure(error string, code int) {
fmt.Fprintln(os.Stderr, error)
os.Exit(code)
}
func dumpProducts(client *shopify.Client, dumper dumper, pageSize int) error {
listOptions := shopify.ListOptions{
Fields: shopifyFields,
Limit: pageSize,
}
for {
products, pages, err := client.Product.ListWithPagination(
shopify.ProductListOptions{
ListOptions: listOptions,
},
)
if err != nil {
return fmt.Errorf("Failed retrieve products: "+err.Error())
}
for _, product := range products {
err = dumper.Dump(product)
if err != nil {
return fmt.Errorf("Failed saving products: "+err.Error())
}
}
if pages.NextPageOptions == nil {
break
}
listOptions.PageInfo = pages.NextPageOptions.PageInfo
}
return nil
}
func main() {
var key, password, token string
var asJSON, showHelp, showVersion, verbose bool
var jsonRoot string
var timeout int64
var pageSize int
var options []shopify.Option
var client *shopify.Client
flag.Usage = func() {
exitFailure(fmt.Sprintf(usage, strings.Join(exportformat.JSONRootProperties, ", ")), 2)
}
flag.BoolVar(&showHelp, "h", false, "")
flag.BoolVar(&showHelp, "help", false, "")
flag.BoolVar(&asJSON, "j", false, "")
flag.BoolVar(&asJSON, "json", false, "")
flag.StringVar(&key, "k", os.Getenv("SHOPIFY_API_KEY"), "")
flag.StringVar(&key, "key", os.Getenv("SHOPIFY_API_KEY"), "")
flag.StringVar(&password, "p", os.Getenv("SHOPIFY_API_PASSWORD"), "")
flag.StringVar(&password, "password", os.Getenv("SHOPIFY_API_PASSWORD"), "")
flag.StringVar(&jsonRoot, "r", "", "")
flag.StringVar(&jsonRoot, "json-root", "", "")
flag.IntVar(&pageSize, "s", 250, "")
flag.IntVar(&pageSize, "size", 250, "")
flag.StringVar(&token, "t", os.Getenv("SHOPIFY_API_TOKEN"), "")
flag.StringVar(&token, "token", os.Getenv("SHOPIFY_API_TOKEN"), "")
flag.Int64Var(&timeout, "timeout", -1, "")
flag.BoolVar(&showVersion, "v", false, "")
flag.BoolVar(&showVersion, "version", false, "")
flag.BoolVar(&verbose, "verbose", false, "")
flag.Parse()
argv := flag.Args()
if showVersion {
fmt.Printf("%s\n", version)
os.Exit(0)
}
if showHelp || len(argv) == 0 {
flag.Usage()
}
var dumper dumper
var err error
if asJSON {
dumper, err = exportformat.NewJSON(argv[0], jsonRoot)
} else {
dumper, err = exportformat.NewCSV(argv[0])
}
if err != nil {
exitFailure(err.Error(), 1)
}
app := shopify.App{ApiKey: key, Password: password}
if timeout > -1 {
options = append(
options,
shopify.WithHTTPClient(
&http.Client{
Timeout: time.Second * time.Duration(timeout),
},
),
)
}
if verbose {
options = append(options, shopify.WithLogger(&shopify.LeveledLogger{Level: shopify.LevelDebug}))
}
client = shopify.NewClient(app, argv[0], token, options...)
err = dumpProducts(client, dumper, pageSize)
dumpErr := dumper.Close()
if err != nil {
exitFailure(err.Error(), 1)
}
if dumpErr != nil {
exitFailure(dumpErr.Error(), 1)
}
}