MCPcopy Index your code
hub / github.com/1Panel-dev/1Panel / UpdateModule

Method UpdateModule

agent/app/service/nginx.go:296–354  ·  view source on GitHub ↗
(req request.NginxModuleUpdate)

Source from the content-addressed store, hash-verified

294}
295
296func (n NginxService) UpdateModule(req request.NginxModuleUpdate) error {
297 nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
298 if err != nil {
299 return err
300 }
301 fileOp := files.NewFileOp()
302 var (
303 modules []dto.NginxModule
304 )
305 moduleConfigPath := path.Join(nginxInstall.GetPath(), "build", "module.json")
306 if !fileOp.Stat(moduleConfigPath) {
307 _ = fileOp.CreateFile(moduleConfigPath)
308 }
309 moduleContent, err := fileOp.GetContent(moduleConfigPath)
310 if err != nil {
311 return err
312 }
313 if len(moduleContent) > 0 {
314 _ = json.Unmarshal(moduleContent, &modules)
315 }
316
317 switch req.Operate {
318 case "create":
319 for _, module := range modules {
320 if module.Name == req.Name {
321 return buserr.New("ErrNameIsExist")
322 }
323 }
324 modules = append(modules, dto.NginxModule{
325 Name: req.Name,
326 Script: req.Script,
327 Packages: strings.Split(req.Packages, ","),
328 Params: req.Params,
329 Enable: true,
330 })
331 case "update":
332 for i, module := range modules {
333 if module.Name == req.Name {
334 modules[i].Script = req.Script
335 modules[i].Packages = strings.Split(req.Packages, ",")
336 modules[i].Params = req.Params
337 modules[i].Enable = req.Enable
338 break
339 }
340 }
341 case "delete":
342 for i, module := range modules {
343 if module.Name == req.Name {
344 modules = append(modules[:i], modules[i+1:]...)
345 break
346 }
347 }
348 }
349 moduleByte, err := json.Marshal(modules)
350 if err != nil {
351 return err
352 }
353 return fileOp.SaveFileWithByte(moduleConfigPath, moduleByte, constant.DirPerm)

Callers

nothing calls this directly

Calls 6

StatMethod · 0.95
CreateFileMethod · 0.95
GetContentMethod · 0.95
SaveFileWithByteMethod · 0.95
getAppInstallByKeyFunction · 0.85
GetPathMethod · 0.45

Tested by

no test coverage detected