github.com/erda-project/erda-infra@v1.0.9/base/servicehub/config.go (about) 1 // Copyright (c) 2021 Terminus, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package servicehub 16 17 import ( 18 "fmt" 19 "os" 20 "reflect" 21 "strings" 22 23 "github.com/erda-project/erda-infra/pkg/config" 24 ) 25 26 func (h *Hub) loadConfigWithArgs(file string, cfg map[string]interface{}, args ...string) (map[string]interface{}, error) { 27 if len(args) > 0 { 28 args = args[1:] 29 var idx int 30 var arg string 31 for idx, arg = range args { 32 if !strings.HasPrefix(arg, "-") { 33 cfg[arg] = nil 34 } else { 35 break 36 } 37 } 38 args, num, idx := args[idx:], len(args[idx:]), 0 39 for ; idx < num; idx++ { 40 arg := args[idx] 41 if strings.HasPrefix(arg, "-") { 42 if arg == "-c" || arg == "--config" || arg == "-config" { 43 idx++ 44 if idx < len(args) && len(args[idx]) > 0 { 45 file = args[idx] 46 } 47 } else if strings.HasPrefix(arg, "-c=") { 48 arg = arg[len("-c="):] 49 if len(arg) > 0 { 50 file = arg 51 } 52 } else if strings.HasPrefix(arg, "--config=") { 53 arg = arg[len("--config="):] 54 if len(arg) > 0 { 55 file = arg 56 } 57 } else if strings.HasPrefix(arg, "-config=") { 58 arg = arg[len("-config="):] 59 if len(arg) > 0 { 60 file = arg 61 } 62 } 63 } 64 } 65 } 66 if len(file) <= 0 { 67 return cfg, nil 68 } 69 err := config.LoadToMap(file, cfg) 70 if err != nil { 71 if os.IsNotExist(err) { 72 if len(cfg) <= 0 { 73 h.logger.Warnf("config file %s not exist", file) 74 } else { 75 h.logger.Debugf("config file %s not exist", file) 76 } 77 return cfg, nil 78 } 79 h.logger.Errorf("fail to load config: %s", err) 80 return nil, err 81 } 82 h.logger.Debugf("using config file: %s", file) 83 return cfg, nil 84 } 85 86 func (h *Hub) loadProviders(config map[string]interface{}) error { 87 h.providersMap = map[string][]*providerContext{} 88 err := h.doLoadProviders(config, "providers") 89 if err != nil { 90 return err 91 } 92 list := config["providers"] 93 if list != nil { 94 switch providers := list.(type) { 95 case []interface{}: 96 for _, item := range providers { 97 if cfg, ok := item.(map[string]interface{}); ok { 98 err = h.addProvider("", cfg) 99 if err != nil { 100 return nil 101 } 102 } else { 103 return fmt.Errorf("invalid provider config type: %v", reflect.TypeOf(cfg)) 104 } 105 } 106 case map[string]interface{}: 107 err = h.doLoadProviders(providers, "") 108 if err != nil { 109 return err 110 } 111 } 112 } 113 return nil 114 } 115 116 func (h *Hub) doLoadProviders(config map[string]interface{}, filter string) error { 117 for key, cfg := range config { 118 if key == filter { 119 continue 120 } 121 err := h.addProvider(key, cfg) 122 if err != nil { 123 return err 124 } 125 } 126 return nil 127 } 128 129 func (h *Hub) addProvider(key string, cfg interface{}) error { 130 name, label := key, "" 131 idx := strings.Index(key, "@") 132 if idx > 0 { 133 name = key[0:idx] 134 label = key[idx+1:] 135 } 136 if cfg != nil { 137 if v, ok := cfg.(map[string]interface{}); ok { 138 if val, ok := v["_name"]; ok { 139 if n, ok := val.(string); ok { 140 name = n 141 } 142 } 143 if val, ok := v["_enable"]; ok { 144 if enable, ok := val.(bool); ok && !enable { 145 return nil 146 } 147 } 148 } 149 } 150 if len(name) <= 0 { 151 return fmt.Errorf("provider name must not be empty") 152 } 153 define, ok := serviceProviders[name] 154 if !ok { 155 return fmt.Errorf("provider %s not exist", name) 156 } 157 provider := define.Creator()() 158 pctx := &providerContext{ 159 Context: h.ctx, 160 hub: h, 161 key: key, 162 label: label, 163 name: name, 164 cfg: cfg, 165 provider: provider, 166 define: define, 167 } 168 if provider != nil { 169 value := reflect.ValueOf(provider) 170 typ := value.Type() 171 for typ.Kind() == reflect.Ptr { 172 value = value.Elem() 173 typ = value.Type() 174 } 175 if typ.Kind() == reflect.Struct { 176 pctx.structValue = value 177 pctx.structType = typ 178 } 179 } 180 h.providersMap[name] = append(h.providersMap[name], pctx) 181 return nil 182 }