Skip to content

Commit 23db935

Browse files
authored
split out fiiles (#6)
* split out fiiles * extract deploy procedure of products * make request parallel again * add setup task * make pages deployable * make blogs deployable
1 parent 7a72f98 commit 23db935

File tree

12 files changed

+556
-283
lines changed

12 files changed

+556
-283
lines changed

BUILD.bazel

+9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1+
load("@io_bazel_rules_go//go:def.bzl", "go_path")
12
load("@bazel_gazelle//:def.bzl", "gazelle")
23

34
# gazelle:prefix k9bookshelf
45
gazelle(name = "gazelle")
56

7+
go_path(
8+
name = "gopath",
9+
mode = "link",
10+
deps = [
11+
"//syncdata/cmd",
12+
],
13+
)
14+
615
config_setting(
716
name = "darwin",
817
values = {"cpu": "darwin"},

Makefile

+8
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,18 @@ bin/syncdata: $(GO_FILES) WORKSPACE
3838
$(BZL) build //syncdata/cmd:all
3939
cp -f $(BZL_BIN)/syncdata/cmd/cmd_/cmd bin/syncdata
4040

41+
.PHONY: setup
42+
setup: WORKSPACE */BUILD.bazel GOPATH
43+
4144
.PHONY: syncdata/BUILD.bazel gqlgenc/BUILD.bazel
4245

4346
*/BUILD.bazel: $(GO_FILES)
4447
$(BZL) run //:gazelle
4548

4649
WORKSPACE: go.mod syncdata/BUILD.bazel
4750
$(BZL) run //:gazelle -- update-repos -from_file=go.mod
51+
52+
.PHONY: GOPATH
53+
54+
GOPATH:
55+
$(BZL) build //:gopath
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
以下の書籍が入荷しました。
2+
3+
* [Language Implementation Patterns: Create Your Own Domain-Specific and General Programming Languages](https://k9bookshelf.com/products/language-implementation-patterns-create-your-own-domain-specific-and-general-programming-languages)
4+
* [Node.js Design Patterns - Third edition: Design and implement production-grade Node.js applications using proven patterns and techniques](https://k9bookshelf.com/products/node-js-design-patterns-third-edition-design-and-implement-production-grade-node-js-applications-using-proven-patterns-and-techniques)
5+
* [The Maker's Guide to the Zombie Apocalypse: Defend Your Base with Simple Circuits, Arduino, and Raspberry Pi](https://k9bookshelf.com/products/the-makers-guide-to-the-zombie-apocalypse-defend-your-base-with-simple-circuits-arduino-and-raspberry-pi)
6+
* [Programming Webassembly with Rust: Unified Development for Web, Mobile, and Embedded Applications (1ST ed.)](https://k9bookshelf.com/products/programming-webassembly-with-rust)

contents/blogs/news/新着入荷.md

-6
This file was deleted.

contents/pages/contact.md

-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
k9bookshelfに関するお問い合わせはこちらからお願いいたします。
2-

syncdata/BUILD.bazel

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ go_library(
44
name = "go_default_library",
55
srcs = [
66
"article.go",
7+
"client.go",
8+
"deploy.go",
9+
"download.go",
710
"syncdata.go",
811
],
912
importpath = "k9bookshelf/syncdata",

syncdata/article.go

+28-13
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,31 @@ import (
1111
// Article is documented at https://shopify.dev/docs/admin-api/rest/reference/online-store/article
1212
type Article struct {
1313
ID int64 `json:"id"`
14-
Title string `json:"title"`
15-
CreatedAt *time.Time `json:"created_at"`
16-
BodyHTML string `json:"body_html"`
17-
BlogID int64 `json:"blog_id"`
18-
Author string `json:"author"`
19-
UserID int64 `json:"user_id"`
20-
PublishedAt *time.Time `json:"published_at"`
21-
UpdatedAt *time.Time `json:"updated_at"`
22-
SummaryHTML *string `json:"summary_html"`
23-
TemplateSuffix *string `json:"template_suffix"`
24-
Handle string `json:"handle"`
25-
Tags string `json:"tags"`
26-
AdminGraphqlAPIID string `json:"admin_graphql_api_id"`
14+
Title string `json:"title,omitempty"`
15+
CreatedAt *time.Time `json:"created_at,omitempty"`
16+
BodyHTML string `json:"body_html,omitempty"`
17+
BlogID int64 `json:"blog_id,omitempty"`
18+
Author string `json:"author,omitempty"`
19+
UserID int64 `json:"user_id,omitempty"`
20+
PublishedAt *time.Time `json:"published_at,omitempty"`
21+
UpdatedAt *time.Time `json:"updated_at,omitempty"`
22+
SummaryHTML *string `json:"summary_html,omitempty"`
23+
TemplateSuffix *string `json:"template_suffix,omitempty"`
24+
Handle string `json:"handle,omitempty"`
25+
Tags string `json:"tags,omitempty"`
26+
AdminGraphqlAPIID string `json:"admin_graphql_api_id,omitempty"`
2727
}
2828

2929
// Articles is not documented yet.
3030
type Articles struct {
3131
Articles []Article `json:"articles"`
3232
}
3333

34+
// ArticlePayload is not documented yet.
35+
type ArticlePayload struct {
36+
Article Article `json:"article"`
37+
}
38+
3439
// ArticleResource is not documented yet.
3540
type ArticleResource struct {
3641
client *shopify.Client
@@ -50,3 +55,13 @@ func (a *ArticleResource) List(blogID int64) (*Articles, error) {
5055
}
5156
return &articles, nil
5257
}
58+
59+
// Put update article
60+
func (a *ArticleResource) Put(article Article) (*Article, error) {
61+
var response Article
62+
err := a.client.Put(path.Join("admin", "api", apiVersion, "blogs", fmt.Sprint(article.BlogID), "articles", fmt.Sprintf("%d.json", article.ID)), ArticlePayload{Article: article}, &response)
63+
if err != nil {
64+
return nil, err
65+
}
66+
return &response, nil
67+
}

syncdata/client.go

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package syncdata
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"k9bookshelf/generated"
7+
"net/http"
8+
9+
"github.com/Yamashou/gqlgenc/client"
10+
shopify "github.com/bold-commerce/go-shopify"
11+
)
12+
13+
func establishGqlClient() (*generated.Client, context.Context) {
14+
authHeader := func(req *http.Request) {
15+
req.Header.Set("X-Shopify-Access-Token", appSecret)
16+
}
17+
18+
return &generated.Client{
19+
Client: client.NewClient(http.DefaultClient,
20+
fmt.Sprintf("https://%s/admin/api/%s/graphql.json", shopDomain, apiVersion),
21+
authHeader),
22+
}, context.Background()
23+
}
24+
25+
func establishRestClient() *shopify.Client {
26+
app := shopify.App{
27+
ApiKey: appKey,
28+
ApiSecret: appSecret,
29+
}
30+
31+
return shopify.NewClient(app, shopDomain, appSecret, shopify.WithVersion(apiVersion))
32+
}
33+
34+
func fetchProducts(ctx context.Context, adminClient *generated.Client) (*generated.Products, error) {
35+
var cursor *string
36+
var res *generated.Products
37+
38+
for {
39+
tmpRes, err := adminClient.Products(ctx, 10, cursor)
40+
if err != nil {
41+
return nil, err
42+
}
43+
if res == nil {
44+
res = tmpRes
45+
} else {
46+
res.Products.Edges = append(res.Products.Edges, tmpRes.Products.Edges...)
47+
}
48+
49+
if !tmpRes.Products.PageInfo.HasNextPage {
50+
break
51+
} else {
52+
last := tmpRes.Products.Edges[len(tmpRes.Products.Edges)-1]
53+
cursor = &last.Cursor
54+
}
55+
}
56+
return res, nil
57+
}

syncdata/cmd/main.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
"log"
56
"os"
67

78
"k9bookshelf/syncdata"
@@ -25,8 +26,7 @@ var deployCmd = &cobra.Command{
2526
Run: func(cmd *cobra.Command, args []string) {
2627
err := syncdata.Deploy(cmd.Flag("input").Value.String())
2728
if err != nil {
28-
fmt.Fprintln(os.Stderr, err)
29-
os.Exit(1)
29+
log.Fatal(err)
3030
}
3131
},
3232
}
@@ -37,8 +37,7 @@ var downloadCmd = &cobra.Command{
3737
Run: func(cmd *cobra.Command, args []string) {
3838
err := syncdata.Download(cmd.Flag("output").Value.String())
3939
if err != nil {
40-
fmt.Fprintln(os.Stderr, err)
41-
os.Exit(1)
40+
log.Fatal(err)
4241
}
4342
},
4443
}
@@ -55,7 +54,6 @@ func main() {
5554
rootCmd.AddCommand(deployCmd)
5655

5756
if err := rootCmd.Execute(); err != nil {
58-
fmt.Fprintln(os.Stderr, err)
59-
os.Exit(1)
57+
log.Fatal(err)
6058
}
6159
}

0 commit comments

Comments
 (0)