From 745daedf91db3c1958fb5f3df58603abdf839947 Mon Sep 17 00:00:00 2001 From: icechen Date: Wed, 29 Dec 2021 19:40:57 +0800 Subject: [PATCH] update --- config_handler/config_handler.go | 38 ++++++++++++++-- config_handler/destination_config.go | 44 ++++++++++++++++--- config_handler/real_config.go | 2 + .../{go_handler.go => go_api_handler.go} | 6 ++- go_handler/go_service_handler.go | 11 +++++ 5 files changed, 90 insertions(+), 11 deletions(-) rename go_handler/{go_handler.go => go_api_handler.go} (79%) create mode 100644 go_handler/go_service_handler.go diff --git a/config_handler/config_handler.go b/config_handler/config_handler.go index 7af6e2e..d3b0727 100644 --- a/config_handler/config_handler.go +++ b/config_handler/config_handler.go @@ -50,20 +50,33 @@ func (p *plugin) Find(ctx context.Context, req *config.Request) (*drone.Config, // 4. 根据文件树以及原始配置文件的信息,组装需要构建的服务的ci信息 // 4.1 api modifiedApiList := getModifiedApi(cfg.Api, modifiedFileList) + destinationApi, err := modifiedApiList.toDestinationConfig() + if err != nil { + logrus.Error(err) + return nil, err + } - _ = modifiedApiList // 4.2 service + modifiedServiceList := getModifiedService(cfg.Service, modifiedFileList) + destinationService, err := modifiedServiceList.toDestinationConfig() + if err != nil { + logrus.Error(err) + return nil, err + } // 5. 组装所有ci信息并输出 - return nil, nil + return &drone.Config{ + Data: destinationApi + "\n\n" + destinationService, + Kind: "pipeline", + }, nil } func getGitClient(req *config.Request) git.Client { return git.New(req.Repo.Namespace, req.Repo.Name, req.Build.After, req.Repo.Config) } -func getModifiedApi(api []Api, modifiedFileList []string) []Api { - ret := make([]Api, 0) +func getModifiedApi(api ApiList, modifiedFileList []string) ApiList { + ret := make(ApiList, 0) tempIndex := NewSet() for i, a := range api { for _, file := range modifiedFileList { @@ -78,3 +91,20 @@ func getModifiedApi(api []Api, modifiedFileList []string) []Api { } return ret } + +func getModifiedService(service ServiceList, modifiedFileList []string) ServiceList { + ret := make(ServiceList, 0) + tempIndex := NewSet() + for i, s := range service { + for _, file := range modifiedFileList { + if strings.HasSuffix(file, s.Root) { + tempIndex.Add(i) + } + } + } + + for _, i := range tempIndex.ToSlice() { + ret = append(ret, service[i]) + } + return ret +} diff --git a/config_handler/destination_config.go b/config_handler/destination_config.go index e763fd7..6d864b7 100644 --- a/config_handler/destination_config.go +++ b/config_handler/destination_config.go @@ -2,26 +2,58 @@ package config_handler import "git.icechen.cn/pkg/drone_plugin/go_handler" -type ApiHandler interface { +type Handler interface { ToDestinationConfig() (string, error) } func (al ApiList) toDestinationConfig() (string, error) { - return "", nil + retConfig := "" + for _, api := range al { + config, err := api.toDestinationConfig() + if err != nil { + return "", err + } + retConfig += config + "\n\n---\n\n" + } + return retConfig, nil } func (a Api) toDestinationConfig() (string, error) { - var handler ApiHandler + var handler Handler switch a.Type { case go_handler.TypeGolang: - handler = go_handler.GoApiHandler{} + handler = go_handler.GoApiHandler{ + Name: a.Name, + Root: a.Root, + Port: a.Port, + } } return handler.ToDestinationConfig() } -func (sl ServiceList) toDestinationConfig() { +func (sl ServiceList) toDestinationConfig() (string, error) { + retConfig := "" + for _, service := range sl { + config, err := service.toDestinationConfig() + if err != nil { + return "", err + } + retConfig += config + "\n\n---\n\n" + } + return retConfig, nil } -func (s Service) toDestinationConfig() { +func (s Service) toDestinationConfig() (string, error) { + var handler Handler + switch s.Type { + case go_handler.TypeGolang: + handler = go_handler.GoApiHandler{ + Name: s.Name, + Root: s.Root, + Port: s.Port, + } + } + + return handler.ToDestinationConfig() } diff --git a/config_handler/real_config.go b/config_handler/real_config.go index e17f51e..4209ddf 100644 --- a/config_handler/real_config.go +++ b/config_handler/real_config.go @@ -21,6 +21,7 @@ type ( Name string `json:"name" yaml:"name"` Root string `json:"root" yaml:"root"` Type string `json:"type" yaml:"type"` + Port string `json:"port" yaml:"port"` } ) @@ -30,6 +31,7 @@ type ( Name string `json:"name" yaml:"name"` Root string `json:"root" yaml:"root"` Type string `json:"type" yaml:"type"` + Port string `json:"port" yaml:"port"` } ) diff --git a/go_handler/go_handler.go b/go_handler/go_api_handler.go similarity index 79% rename from go_handler/go_handler.go rename to go_handler/go_api_handler.go index 36a9506..31ca1eb 100644 --- a/go_handler/go_handler.go +++ b/go_handler/go_api_handler.go @@ -13,7 +13,11 @@ steps: - go test -v ` -type GoApiHandler struct{} +type GoApiHandler struct { + Name string + Root string + Port string +} func (GoApiHandler) ToDestinationConfig() (string, error) { return "", nil diff --git a/go_handler/go_service_handler.go b/go_handler/go_service_handler.go new file mode 100644 index 0000000..20c74c4 --- /dev/null +++ b/go_handler/go_service_handler.go @@ -0,0 +1,11 @@ +package go_handler + +type GoServiceHandler struct { + Name string + Root string + Port string +} + +func (GoServiceHandler) ToDestinationConfig() (string, error) { + return "", nil +}