forked from orangebees/kclvm-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkclvm.go
180 lines (150 loc) · 9.26 KB
/
kclvm.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
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright 2021 The KCL Authors. All rights reserved.
/*
KCLVM binding for Go
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ kcl files │ │ KCLVM-Go-API │ │ KCLResultList │
│ ┌───────────┐ │ │ │ │ │
│ │ 1.k │ │ │ │ │ │
│ └───────────┘ │ │ │ │ ┌───────────┐ │ ┌───────────────┐
│ ┌───────────┐ │ │ ┌───────────┐ │ │ │ KCLResult │──┼────────▶│x.Get("a.b.c") │
│ │ 2.k │ │ │ │ Run(path) │ │ │ └───────────┘ │ └───────────────┘
│ └───────────┘ │────┐ │ └───────────┘ │ │ │
│ ┌───────────┐ │ │ │ │ │ ┌───────────┐ │ ┌───────────────┐
│ │ 3.k │ │ │ │ │ │ │ KCLResult │──┼────────▶│x.Get("k", &v) │
│ └───────────┘ │ │ │ │ │ └───────────┘ │ └───────────────┘
│ ┌───────────┐ │ ├───▶│ ┌───────────┐ │──────────▶│ │
│ │setting.yml│ │ │ │ │RunFiles() │ │ │ ┌───────────┐ │ ┌───────────────┐
│ └───────────┘ │ │ │ └───────────┘ │ │ │ KCLResult │──┼────────▶│x.JSONString() │
└─────────────────┘ │ │ │ │ └───────────┘ │ └───────────────┘
│ │ │ │ │
┌─────────────────┐ │ │ │ │ ┌───────────┐ │ ┌───────────────┐
│ Options │ │ │ ┌───────────┐ │ │ │ KCLResult │──┼────────▶│x.YAMLString() │
│WithOptions │ │ │ │MustRun() │ │ │ └───────────┘ │ └───────────────┘
│WithOverrides │────┘ │ └───────────┘ │ │ │
│WithWorkDir │ │ │ │ │
│WithDisableNone │ │ │ │ │
└─────────────────┘ └─────────────────┘ └─────────────────┘
*/
package kclvm
import (
"kusionstack.io/kclvm-go/pkg/kcl"
"kusionstack.io/kclvm-go/pkg/kclvm_runtime"
"kusionstack.io/kclvm-go/pkg/tools/format"
"kusionstack.io/kclvm-go/pkg/tools/lint"
"kusionstack.io/kclvm-go/pkg/tools/list"
"kusionstack.io/kclvm-go/pkg/tools/override"
"kusionstack.io/kclvm-go/pkg/tools/validate"
)
type (
Option = kcl.Option
ListDepsOptions = list.DepOptions
ListDepFilesOption = list.Option
ValidateOptions = validate.ValidateOptions
KCLResult = kcl.KCLResult
KCLResultList = kcl.KCLResultList
KclType = kcl.KclType
)
// InitKclvmPath init kclvm path.
func InitKclvmPath(kclvmRoot string) {
kclvm_runtime.InitKclvmRoot(kclvmRoot)
}
// InitKclvmRuntime init kclvm process.
func InitKclvmRuntime(n int) {
kclvm_runtime.InitRuntime(n)
}
// MustRun is like Run but panics if return any error.
func MustRun(path string, opts ...Option) *KCLResultList {
return kcl.MustRun(path, opts...)
}
// Run evaluates the KCL program with path and opts, then returns the object list.
func Run(path string, opts ...Option) (*KCLResultList, error) {
return kcl.Run(path, opts...)
}
// RunFiles evaluates the KCL program with multi file path and opts, then returns the object list.
func RunFiles(paths []string, opts ...Option) (*KCLResultList, error) {
return kcl.RunFiles(paths, opts...)
}
// WithCode returns a Option which hold a kcl source code list.
func WithCode(codes ...string) Option { return kcl.WithCode(codes...) }
// WithKFilenames returns a Option which hold a filenames list.
func WithKFilenames(filenames ...string) Option { return kcl.WithKFilenames(filenames...) }
// WithOptions returns a Option which hold a key=value pair list for option function.
func WithOptions(key_value_list ...string) Option { return kcl.WithOptions(key_value_list...) }
// WithOverrides returns a Option which hold a override list.
func WithOverrides(override_list ...string) Option { return kcl.WithOverrides(override_list...) }
// WithSettings returns a Option which hold a settings file.
func WithSettings(filename string) Option { return kcl.WithSettings(filename) }
// WithWorkDir returns a Option which hold a work dir.
func WithWorkDir(workDir string) Option { return kcl.WithWorkDir(workDir) }
// WithDisableNone returns a Option which hold a disable none switch.
func WithDisableNone(disableNone bool) Option { return kcl.WithDisableNone(disableNone) }
// WithPrintOverridesAST returns a Option which hold a printOverridesAST switch.
func WithPrintOverridesAST(printOverridesAST bool) Option {
return kcl.WithPrintOverridesAST(printOverridesAST)
}
// WithSortKeys returns a Option which hold a sortKeys switch.
func WithSortKeys(sortKeys bool) Option {
return kcl.WithSortKeys(sortKeys)
}
// WithIncludeSchemaTypePath returns a Option which hold a includeSchemaTypePath switch.
func WithIncludeSchemaTypePath(includeSchemaTypePath bool) Option {
return kcl.WithIncludeSchemaTypePath(includeSchemaTypePath)
}
// FormatCode returns the formatted code.
func FormatCode(code interface{}) ([]byte, error) {
return format.FormatCode(code)
}
// FormatPath formats files from the given path
// path:
// if path is `.` or empty string, all KCL files in current directory will be formatted, not recursively
// if path is `path/file.k`, the specified KCL file will be formatted
// if path is `path/to/dir`, all KCL files in the specified dir will be formatted, not recursively
// if path is `path/to/dir/...`, all KCL files in the specified dir will be formatted recursively
//
// the returned changedPaths are the changed file paths (relative path)
func FormatPath(path string) (changedPaths []string, err error) {
return format.FormatPath(path)
}
// ListDepFiles return the depend files from the given path
func ListDepFiles(workDir string, opt *ListDepFilesOption) (files []string, err error) {
return list.ListDepFiles(workDir, opt)
}
// ListUpStreamFiles return a list of upstream depend files from the given path list
func ListUpStreamFiles(workDir string, opt *ListDepsOptions) (deps []string, err error) {
return list.ListUpStreamFiles(workDir, opt)
}
// ListDownStreamFiles return a list of downstream depend files from the given changed path list.
func ListDownStreamFiles(workDir string, opt *ListDepsOptions) ([]string, error) {
return list.ListDownStreamFiles(workDir, opt)
}
// LintPath lint files from the given path
func LintPath(path string) (results []string, err error) {
return lint.LintPath(path)
}
// OverrideFile rewrites a file with override spec
// file: string. The File that need to be overridden
// specs: []string. List of specs that need to be overridden.
// Each spec string satisfies the form: <pkgpath>:<field_path>=<filed_value> or <pkgpath>:<field_path>-
// When the pkgpath is '__main__', it can be omitted.
// importPaths. List of import statements that need to be added
func OverrideFile(file string, specs, importPaths []string) (bool, error) {
return override.OverrideFile(file, specs, importPaths)
}
// ValidateCode validate data match code
func ValidateCode(data, code string, opt *ValidateOptions) (ok bool, err error) {
return validate.ValidateCode(data, code, opt)
}
func EvalCode(code string) (*KCLResult, error) {
return kcl.EvalCode(code)
}
// GetSchemaType returns schema types from a kcl file or code.
//
// file: string
// The kcl filename
// code: string
// The kcl code string
// schema_name: string
// The schema name got, when the schema name is empty, all schemas are returned.
func GetSchemaType(file, code, schemaName string) ([]*KclType, error) {
return kcl.GetSchemaType(file, code, schemaName)
}