github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/cmd/fns/initialization/base/configs.go (about) 1 /* 2 * Copyright 2023 Wang Min Xiang 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 */ 17 18 package base 19 20 import ( 21 "context" 22 "github.com/aacfactory/errors" 23 "github.com/aacfactory/fns/cmd/generates/files" 24 "github.com/aacfactory/fns/configs" 25 "github.com/aacfactory/fns/logs" 26 "github.com/aacfactory/fns/shareds" 27 "github.com/aacfactory/fns/transports" 28 "github.com/goccy/go-yaml" 29 "os" 30 "path/filepath" 31 "strings" 32 ) 33 34 func NewConfigFiles(dir string) (v []*ConfigFile, err error) { 35 v = make([]*ConfigFile, 0, 1) 36 // root 37 root, rootErr := NewConfigFile("", dir) 38 if rootErr != nil { 39 err = rootErr 40 return 41 } 42 v = append(v, root) 43 // local 44 local, localErr := NewConfigFile("local", dir) 45 if localErr != nil { 46 err = localErr 47 return 48 } 49 v = append(v, local) 50 // dev 51 dev, devErr := NewConfigFile("dev", dir) 52 if devErr != nil { 53 err = devErr 54 return 55 } 56 v = append(v, dev) 57 // test 58 test, testErr := NewConfigFile("test", dir) 59 if testErr != nil { 60 err = testErr 61 return 62 } 63 v = append(v, test) 64 // prod 65 prod, prodErr := NewConfigFile("prod", dir) 66 if prodErr != nil { 67 err = prodErr 68 return 69 } 70 v = append(v, prod) 71 return 72 } 73 74 func NewConfigFile(kind string, dir string) (cf *ConfigFile, err error) { 75 if !filepath.IsAbs(dir) { 76 dir, err = filepath.Abs(dir) 77 if err != nil { 78 err = errors.Warning("fns: new config file failed").WithCause(err).WithMeta("dir", dir) 79 return 80 } 81 } 82 name := "fns.yaml" 83 if kind != "" { 84 kind = strings.TrimSpace(strings.ToLower(kind)) 85 switch kind { 86 case "local": 87 name = "fns-local.yaml" 88 break 89 case "dev": 90 name = "fns-dev.yaml" 91 break 92 case "test": 93 name = "fns-test.yaml" 94 break 95 case "prod": 96 name = "fns-prod.yaml" 97 break 98 default: 99 err = errors.Warning("fns: new config file failed").WithCause(errors.Warning("kind is invalid")).WithMeta("kind", kind) 100 return 101 } 102 } 103 dir = filepath.ToSlash(filepath.Join(dir, "configs")) 104 filename := filepath.ToSlash(filepath.Join(dir, name)) 105 cf = &ConfigFile{ 106 kind: kind, 107 dir: dir, 108 filename: filename, 109 } 110 return 111 } 112 113 type ConfigFile struct { 114 kind string 115 dir string 116 filename string 117 } 118 119 func (f *ConfigFile) Name() (name string) { 120 name = f.filename 121 return 122 } 123 124 func (f *ConfigFile) Write(_ context.Context) (err error) { 125 if !files.ExistFile(f.dir) { 126 mdErr := os.MkdirAll(f.dir, 0644) 127 if mdErr != nil { 128 err = errors.Warning("fns: config file write failed").WithCause(mdErr).WithMeta("dir", f.dir) 129 return 130 } 131 } 132 config := configs.Config{} 133 switch f.kind { 134 case "local": 135 config.Log = logs.Config{ 136 Level: logs.Debug, 137 Formatter: logs.TextColorfulConsoleFormatter, 138 Console: logs.Stdout, 139 DisableConsole: false, 140 Consumes: 0, 141 Buffer: 0, 142 SendTimeout: "", 143 ShutdownTimeout: "", 144 Writers: nil, 145 } 146 config.Runtime = configs.RuntimeConfig{ 147 Procs: configs.ProcsConfig{}, 148 Workers: configs.WorkersConfig{}, 149 Shared: shareds.LocalSharedConfig{}, 150 } 151 break 152 case "dev": 153 config.Log = logs.Config{ 154 Level: logs.Info, 155 Formatter: logs.JsonConsoleFormatter, 156 Console: logs.Stdout, 157 DisableConsole: false, 158 Consumes: 0, 159 Buffer: 0, 160 SendTimeout: "", 161 ShutdownTimeout: "", 162 Writers: nil, 163 } 164 config.Runtime = configs.RuntimeConfig{ 165 Procs: configs.ProcsConfig{Min: 2}, 166 Workers: configs.WorkersConfig{}, 167 Shared: shareds.LocalSharedConfig{}, 168 } 169 break 170 case "test": 171 config.Log = logs.Config{ 172 Level: logs.Warn, 173 Formatter: logs.JsonConsoleFormatter, 174 Console: logs.Stdout, 175 DisableConsole: false, 176 Consumes: 0, 177 Buffer: 0, 178 SendTimeout: "", 179 ShutdownTimeout: "", 180 Writers: nil, 181 } 182 config.Runtime = configs.RuntimeConfig{ 183 Procs: configs.ProcsConfig{Min: 2}, 184 Workers: configs.WorkersConfig{}, 185 Shared: shareds.LocalSharedConfig{}, 186 } 187 break 188 case "prod": 189 config.Log = logs.Config{ 190 Level: logs.Error, 191 Formatter: logs.JsonConsoleFormatter, 192 Console: logs.Stdout, 193 DisableConsole: false, 194 Consumes: 0, 195 Buffer: 0, 196 SendTimeout: "", 197 ShutdownTimeout: "", 198 Writers: nil, 199 } 200 config.Runtime = configs.RuntimeConfig{ 201 Procs: configs.ProcsConfig{Min: 8}, 202 Workers: configs.WorkersConfig{}, 203 Shared: shareds.LocalSharedConfig{}, 204 } 205 break 206 default: 207 config.Transport = transports.Config{ 208 Port: 18080, 209 } 210 break 211 } 212 p, encodeErr := yaml.Marshal(config) 213 if encodeErr != nil { 214 err = errors.Warning("fns: config file write failed").WithCause(encodeErr).WithMeta("filename", f.filename) 215 return 216 } 217 writeErr := os.WriteFile(f.filename, p, 0644) 218 if writeErr != nil { 219 err = errors.Warning("fns: config file write failed").WithCause(writeErr).WithMeta("filename", f.filename) 220 return 221 } 222 return 223 }