Эх сурвалжийг харах

正在添加解析SQL资源文件

yjp 1 жил өмнө
parent
commit
faa19a4585
3 өөрчлөгдсөн 140 нэмэгдсэн , 67 устгасан
  1. 121 0
      instance.go
  2. 19 10
      options.go
  3. 0 57
      sdk.go

+ 121 - 0
instance.go

@@ -0,0 +1,121 @@
+package ds_sdk
+
+import (
+	"errors"
+	"git.sxidc.com/go-tools/utils/fileutils"
+	"git.sxidc.com/go-tools/utils/strutils"
+	"git.sxidc.com/go-tools/utils/yaml/yaml_loader"
+	"git.sxidc.com/service-supports/ds-sdk/client"
+	"strings"
+)
+
+var sdkInstance *SDK
+
+func GetInstance() *SDK {
+	return sdkInstance
+}
+
+func InitInstance(token string, baseUrl string, namespace string, dataSource *DataSourceOption, opts ...Option) error {
+	if sdkInstance != nil {
+		return nil
+	}
+
+	options := &Options{
+		token:      token,
+		baseUrl:    baseUrl,
+		namespace:  namespace,
+		dataSource: dataSource,
+	}
+
+	for _, opt := range opts {
+		opt(options)
+	}
+
+	c := client.New(options.timeout)
+
+	err := initNamespace(c, options)
+	if err != nil {
+		return err
+	}
+
+	err = initDataSource(c, options)
+	if err != nil {
+		return err
+	}
+
+	err = initSqlResources(c, options)
+	if err != nil {
+		return err
+	}
+
+	sdkInstance = &SDK{
+		options: options,
+		client:  c,
+	}
+
+	return nil
+}
+
+func initNamespace(c *client.Client, options *Options) error {
+	namespaceInfos, err := c.GetNamespaces(options.token, options.baseUrl, options.namespace, 1, 1)
+	if err != nil {
+		return err
+	}
+
+	if namespaceInfos != nil && len(namespaceInfos) != 0 {
+		return nil
+	}
+
+	err = c.CreateNamespace(options.token, options.baseUrl, options.namespace)
+	if err != nil && !strings.Contains(err.Error(), "已存在") {
+		return err
+	}
+
+	return nil
+}
+
+func initDataSource(c *client.Client, options *Options) error {
+	dataSourceInfos, err := c.GetDataSources(options.token, options.baseUrl, options.namespace,
+		options.dataSource.Name, options.dataSource.Type, 1, 1)
+	if err != nil {
+		return err
+	}
+
+	if dataSourceInfos != nil && len(dataSourceInfos) != 0 {
+		return nil
+	}
+
+	err = c.CreateDataSource(options.token, options.baseUrl, options.namespace,
+		options.dataSource.Name, options.dataSource.Type, options.dataSource.Spec)
+	if err != nil && !strings.Contains(err.Error(), "已存在") {
+		return err
+	}
+
+	return nil
+}
+
+func initSqlResources(c *client.Client, options *Options) error {
+	if strutils.IsStringEmpty(options.sqlResourcesDir) {
+		return nil
+	}
+
+	if !fileutils.PathExists(options.sqlResourcesDir) {
+		return errors.New("指定的SQL资源目录不存在")
+	}
+
+	files, err := fileutils.GetDirFiles(options.sqlResourcesDir)
+	if err != nil {
+		return err
+	}
+
+	for _, file := range files {
+		err := yaml_loader.LoadYamlMapFile(file, func(objectMap map[string]any) error {
+
+		})
+		if err != nil {
+			return err
+		}
+	}
+
+	return nil
+}

+ 19 - 10
options.go

@@ -1,6 +1,22 @@
 package ds_sdk
 
-import "time"
+import (
+	"time"
+)
+
+type Option func(opts *Options)
+
+func WithTimeout(timeout time.Duration) Option {
+	return func(opts *Options) {
+		opts.timeout = timeout
+	}
+}
+
+func WithSqlResourceDir(sqlResourcesDir string) Option {
+	return func(opts *Options) {
+		opts.sqlResourcesDir = sqlResourcesDir
+	}
+}
 
 type DataSourceOption struct {
 	Name string
@@ -16,13 +32,6 @@ type Options struct {
 	dataSource *DataSourceOption
 
 	// 选传
-	timeout time.Duration
-}
-
-type Option func(opts *Options)
-
-func WithTimeout(timeout time.Duration) Option {
-	return func(opts *Options) {
-		opts.timeout = timeout
-	}
+	timeout         time.Duration
+	sqlResourcesDir string
 }

+ 0 - 57
sdk.go

@@ -4,65 +4,8 @@ import (
 	"errors"
 	"git.sxidc.com/go-tools/utils/strutils"
 	"git.sxidc.com/service-supports/ds-sdk/client"
-	"strings"
 )
 
-var sdkInstance *SDK
-
-func InitInstance(token string, baseUrl string, namespace string, dataSource *DataSourceOption, opts ...Option) error {
-	if sdkInstance == nil {
-		options := &Options{
-			token:      token,
-			baseUrl:    baseUrl,
-			namespace:  namespace,
-			dataSource: dataSource,
-		}
-
-		for _, opt := range opts {
-			opt(options)
-		}
-
-		c := client.New(options.timeout)
-
-		namespaceInfos, err := c.GetNamespaces(token, baseUrl, namespace, 1, 1)
-		if err != nil {
-			return err
-		}
-
-		if namespaceInfos == nil || len(namespaceInfos) == 0 {
-			err := c.CreateNamespace(token, baseUrl, namespace)
-			if err != nil && !strings.Contains(err.Error(), "已存在") {
-				return err
-			}
-		}
-
-		dataSourceInfos, err := c.GetDataSources(token, baseUrl, namespace,
-			options.dataSource.Name, options.dataSource.Type, 1, 1)
-		if err != nil {
-			return err
-		}
-
-		if dataSourceInfos == nil || len(dataSourceInfos) == 0 {
-			err := c.CreateDataSource(token, baseUrl, namespace,
-				options.dataSource.Name, options.dataSource.Type, options.dataSource.Spec)
-			if err != nil && !strings.Contains(err.Error(), "已存在") {
-				return err
-			}
-		}
-
-		sdkInstance = &SDK{
-			options: options,
-			client:  c,
-		}
-	}
-
-	return nil
-}
-
-func GetInstance() *SDK {
-	return sdkInstance
-}
-
 type SDK struct {
 	options *Options
 	client  *client.Client