-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
158 lines (133 loc) · 3.42 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
package main
import (
"context"
"fmt"
"os"
"path/filepath"
invv1alpha1 "github.com/sdcio/config-server/apis/inv/v1alpha1"
"github.com/sdcio/config-server/pkg/git/auth"
schemaloader "github.com/sdcio/config-server/pkg/schema"
"github.com/sdcio/schema-server/pkg/config"
"github.com/sdcio/schema-server/pkg/schema"
log "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/yaml"
)
const (
tmpPath = "tmp"
schemasPath = "schemas"
)
func main() {
args := os.Args
os.MkdirAll(tmpPath, 0755|os.ModeDir)
os.MkdirAll(schemasPath, 0755|os.ModeDir)
log.SetFormatter(&log.TextFormatter{FullTimestamp: true})
log.SetLevel(log.DebugLevel)
if len(args) < 1 {
panic("cannot execute need config and base dir")
}
// schemaconfig is the first arg
schemacr, err := getConfig(args[1])
if err != nil {
panic(err)
}
/*
basepath := args[2]
if err := validatePath(basepath); err != nil {
panic(err)
}
*/
schemaLoader, err := schemaloader.NewLoader(
filepath.Join(tmpPath),
filepath.Join(schemasPath),
NewNopResolver(),
)
if err != nil {
panic(err)
}
ctx := context.Background()
schemaspec := &schemacr.Spec
schemaLoader.AddRef(ctx, schemaspec)
_, dirExists, err := schemaLoader.GetRef(ctx, schemaspec.GetKey())
if err != nil {
panic(err)
}
if !dirExists {
fmt.Println("loading...")
if err := schemaLoader.Load(ctx, schemaspec.GetKey(), types.NamespacedName{
Namespace: schemacr.Namespace,
Name: schemaspec.Credentials,
}); err != nil {
panic(err)
}
}
//models := initSlice(schemacr.Spec.Schema.Models, ".")
//includes := initSlice(schemacr.Spec.Schema.Includes, "")
//excludes := initSlice(schemacr.Spec.Schema.Excludes, "")
//fmt.Println("models", getNewBase(basepath, models))
//fmt.Println("includes", getNewBase(basepath, includes))
schemaConfig := &config.SchemaConfig{
Name: "",
Vendor: schemacr.Spec.Provider,
Version: schemacr.Spec.Version,
//Files: getNewBase(basepath, models),
Files: schemaspec.GetNewSchemaBase(schemasPath).Models,
//Directories: getNewBase(basepath, includes),
Directories: schemaspec.GetNewSchemaBase(schemasPath).Includes,
//Excludes: excludes,
Excludes: schemaspec.GetNewSchemaBase(schemasPath).Excludes,
}
x, err := schema.NewSchema(schemaConfig)
if err != nil {
panic(err)
}
fmt.Println(x)
}
func getConfig(schemaConfigPath string) (*invv1alpha1.Schema, error) {
b, err := os.ReadFile(schemaConfigPath)
if err != nil {
panic(err)
}
schema := &invv1alpha1.Schema{}
if err := yaml.Unmarshal(b, schema); err != nil {
return nil, err
}
return schema, nil
}
/*
func validatePath(path string) error {
fi, err := os.Stat(path)
if err != nil {
return err
}
if !fi.IsDir() {
return fmt.Errorf("input path needs to be a directory")
}
return nil
}
func initSlice(in []string, init string) []string {
if len(in) == 0 {
if init != "" {
return []string{init}
} else {
return []string{}
}
}
return in
}
func getNewBase(basePath string, in []string) []string {
str := make([]string, 0, len(in))
for _, s := range in {
str = append(str, filepath.Join(basePath, s))
}
return str
}
*/
func NewNopResolver() auth.CredentialResolver {
return &secretNopResolver{}
}
var _ auth.CredentialResolver = &secretNopResolver{}
type secretNopResolver struct{}
func (r *secretNopResolver) ResolveCredential(ctx context.Context, nsn types.NamespacedName) (auth.Credential, error) {
return nil, nil
}