-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcms_types.go
114 lines (101 loc) · 3.25 KB
/
cms_types.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
package gontentful
import (
"context"
"encoding/json"
"fmt"
"path/filepath"
"github.com/moonwalker/moonbase/pkg/content"
gh "github.com/moonwalker/moonbase/pkg/github"
)
func GetSchema(ctx context.Context, cfg *Config, owner, repo, ref, contentType string) (*content.Schema, error) {
path := filepath.Join(cfg.WorkDir, contentType)
rc, _, err := gh.GetSchema(ctx, cfg.Token, owner, repo, ref, path)
if err != nil {
return nil, fmt.Errorf("failed to get schema of %s: %s", contentType, err.Error())
}
schemaContent, err := rc.GetContent()
if err != nil {
return nil, fmt.Errorf("failed to get schema content: %s", err.Error())
}
schema := &content.Schema{}
err = json.Unmarshal([]byte(schemaContent), schema)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal schema %s: %s", contentType, err.Error())
}
return schema, nil
}
func GetCMSSchema(repo string, ct string) (*ContentType, error) {
ctx := context.Background()
cfg := getConfig(ctx, owner, repo, branch)
path := filepath.Join(cfg.WorkDir, ct)
res, _, err := gh.GetSchema(ctx, cfg.Token, owner, repo, branch, path)
if err != nil {
return nil, fmt.Errorf("failed to get schema from github: %s", err.Error())
}
ghc, err := res.GetContent()
if err != nil {
return nil, fmt.Errorf("repositoryContent.GetContent failed: %s", err.Error())
}
m := &content.Schema{}
_ = json.Unmarshal([]byte(ghc), m)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal schema %s: %s", *res.Path, err.Error())
}
return formatSchema(m), nil
}
func GetCMSSchemas(repo string, ct string) (*ContentTypes, error) {
ctx := context.Background()
cfg := getConfig(ctx, owner, repo, branch)
path := filepath.Join(cfg.WorkDir, ct)
res, _, err := gh.GetSchemasRecursive(ctx, cfg.Token, owner, repo, branch, path)
if err != nil {
return nil, fmt.Errorf("failed to get schemas from github: %s", err.Error())
}
schemas := &ContentTypes{
Total: len(res),
Limit: 0,
Skip: 0,
Items: make([]*ContentType, 0),
}
for _, rc := range res {
ghc, err := rc.GetContent()
if err != nil {
return nil, fmt.Errorf("repositoryContent.GetContent failed: %s", err.Error())
}
m := &content.Schema{}
_ = json.Unmarshal([]byte(ghc), m)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal schema %s: %s", *rc.Path, err.Error())
}
schemas.Items = append(schemas.Items, formatSchema(m))
}
return schemas, nil
}
func GetCMSSchemasExpanded(repo string, ct string) (*ContentTypes, error) {
ctx := context.Background()
cfg := getConfig(ctx, owner, repo, branch)
path := filepath.Join(cfg.WorkDir, ct)
res, _, err := gh.GetSchemasRecursive(ctx, cfg.Token, owner, repo, branch, path)
if err != nil {
return nil, fmt.Errorf("failed to get schemas from github: %s", err.Error())
}
schemas := &ContentTypes{
Total: len(res),
Limit: 0,
Skip: 0,
Items: make([]*ContentType, 0),
}
for _, rc := range res {
ghc, err := rc.GetContent()
if err != nil {
return nil, fmt.Errorf("repositoryContent.GetContent failed: %s", err.Error())
}
m := &content.Schema{}
_ = json.Unmarshal([]byte(ghc), m)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal schema %s: %s", *rc.Path, err.Error())
}
schemas.Items = append(schemas.Items, formatSchemaRecursive(m)...)
}
return schemas, nil
}