From 3be607148ad4063ec7e5a1644e85557fb2bbbfc2 Mon Sep 17 00:00:00 2001 From: CorrectRoad Date: Fri, 12 Apr 2024 15:16:11 +0800 Subject: [PATCH] refactor uncontrolled field (#185) --- route/v2/compose_app.go | 94 ++++++++++--------------------------- service/compose_app.go | 22 +++++++++ service/compose_app_test.go | 26 ++++++++++ 3 files changed, 72 insertions(+), 70 deletions(-) diff --git a/route/v2/compose_app.go b/route/v2/compose_app.go index 5e402ba3..c046056d 100644 --- a/route/v2/compose_app.go +++ b/route/v2/compose_app.go @@ -6,7 +6,6 @@ import ( "fmt" "io" "net/http" - "strings" "github.com/IceWhaleTech/CasaOS-AppManagement/codegen" "github.com/IceWhaleTech/CasaOS-AppManagement/common" @@ -109,29 +108,25 @@ func (a *AppManagement) IsNewComposeUncontrolled(newComposeApp *service.ComposeA // TODO refactor this. because if user not update. the status will be uncontrolled. if newTag == "latest" { return false, nil - } else { - // compare store info - StoreApp, err := service.MyService.AppStoreManagement().ComposeApp(newComposeApp.Name) - if err != nil { - return false, err - } + } - if StoreApp == nil { - logger.Error("store app not found", zap.String("composeAppID", newComposeApp.Name)) - return false, nil - } + // compare store info + StoreApp, err := service.MyService.AppStoreManagement().ComposeApp(newComposeApp.Name) + if err != nil { + return false, err + } - StableTag, err := StoreApp.MainTag() - if err != nil { - return false, err - } + if StoreApp == nil { + logger.Error("store app not found", zap.String("composeAppID", newComposeApp.Name)) + return false, nil + } - if StableTag != newTag { - return true, nil - } else { - return false, nil - } + StableTag, err := StoreApp.MainTag() + if err != nil { + return false, err } + + return StableTag != newTag, nil } func (a *AppManagement) ApplyComposeAppSettings(ctx echo.Context, id codegen.ComposeAppID, params codegen.ApplyComposeAppSettingsParams) error { @@ -178,36 +173,14 @@ func (a *AppManagement) ApplyComposeAppSettings(ctx echo.Context, id codegen.Com Message: &message, }) } - if uncontrolled { - // set to uncontrolled app - xcasaos := composeApp.Extensions[common.ComposeExtensionNameXCasaOS] - xcasaosMap, ok := xcasaos.(map[string]interface{}) - if !ok { - logger.Error("failed to get map compose app extensions", zap.String("composeAppID", composeApp.Name)) - } else { - xcasaosMap[common.ComposeExtensionPropertyNameIsUncontrolled] = true - composeApp.Extensions[common.ComposeExtensionNameXCasaOS] = xcasaosMap - // replace the buf is_uncontrolled is_uncontrolled: false => true - // otherwise, the new buf will be the same as the old one - // the replace is only in apply not install Compose. Because only the old compose has is_uncontrolled - // TODO: refactor the implement in a better way. not replace the string directly - buf = []byte(strings.ReplaceAll(string(buf), "is_uncontrolled: false", "is_uncontrolled: true")) - - } - } else { - // set to controlled app - xcasaos := composeApp.Extensions[common.ComposeExtensionNameXCasaOS] - xcasaosMap, ok := xcasaos.(map[string]interface{}) - if !ok { - logger.Error("failed to get map compose app extensions", zap.String("composeAppID", composeApp.Name)) - } else { - xcasaosMap[common.ComposeExtensionPropertyNameIsUncontrolled] = false - composeApp.Extensions[common.ComposeExtensionNameXCasaOS] = xcasaosMap - - // replace the buf is_uncontrolled is_uncontrolled: true => false - buf = []byte(strings.ReplaceAll(string(buf), "is_uncontrolled: true", "is_uncontrolled: false")) - } + _ = newComposeApp.SetUncontrolled(uncontrolled) + buf, err = service.GenerateYAMLFromComposeApp(*newComposeApp) + if err != nil { + message := err.Error() + return ctx.JSON(http.StatusInternalServerError, codegen.ResponseInternalServerError{ + Message: &message, + }) } if params.CheckPortConflict == nil || *params.CheckPortConflict { @@ -313,27 +286,8 @@ func (a *AppManagement) InstallComposeApp(ctx echo.Context, params codegen.Insta Message: &message, }) } - if uncontrolled { - // set to uncontrolled app - xcasaos := composeApp.Extensions[common.ComposeExtensionNameXCasaOS] - xcasaosMap, ok := xcasaos.(map[string]interface{}) - if !ok { - logger.Error("failed to get map compose app extensions", zap.String("composeAppID", composeApp.Name)) - } else { - xcasaosMap[common.ComposeExtensionPropertyNameIsUncontrolled] = true - composeApp.Extensions[common.ComposeExtensionNameXCasaOS] = xcasaosMap - } - } else { - // set to controlled app - xcasaos := composeApp.Extensions[common.ComposeExtensionNameXCasaOS] - xcasaosMap, ok := xcasaos.(map[string]interface{}) - if !ok { - logger.Error("failed to get map compose app extensions", zap.String("composeAppID", composeApp.Name)) - } else { - xcasaosMap[common.ComposeExtensionPropertyNameIsUncontrolled] = false - composeApp.Extensions[common.ComposeExtensionNameXCasaOS] = xcasaosMap - } - } + + _ = composeApp.SetUncontrolled(uncontrolled) if params.CheckPortConflict == nil || *params.CheckPortConflict { // validation 1 - check if there are ports in use diff --git a/service/compose_app.go b/service/compose_app.go index 28b61b47..7a17467f 100644 --- a/service/compose_app.go +++ b/service/compose_app.go @@ -52,6 +52,12 @@ func (a *ComposeApp) StoreInfo(includeApps bool) (*codegen.ComposeAppStoreInfo, return nil, err } + // TODO refactor this with ComposeAppWithStoreInfo + isUncontrolled, ok := a.Extensions[common.ComposeExtensionNameXCasaOS].(map[string]interface{})[common.ComposeExtensionPropertyNameIsUncontrolled].(bool) + if ok { + storeInfo.IsUncontrolled = &isUncontrolled + } + // locate main app if storeInfo.Main == nil || *storeInfo.Main == "" { // if main app is not specified, use the first app @@ -1039,3 +1045,19 @@ func getNameFrom(composeYAML []byte) string { return baseStructure.Name } + +func (a *ComposeApp) SetUncontrolled(uncontrolled bool) error { + xCasaos := a.Extensions[common.ComposeExtensionNameXCasaOS] + xCasaosMap, ok := xCasaos.(map[string]interface{}) + + // set to controlled app + if !ok { + logger.Error("failed to get map compose app extensions", zap.String("composeAppID", a.Name)) + return ErrComposeExtensionNameXCasaOSNotFound + } else { + xCasaosMap[common.ComposeExtensionPropertyNameIsUncontrolled] = uncontrolled + a.Extensions[common.ComposeExtensionNameXCasaOS] = xCasaosMap + } + + return nil +} diff --git a/service/compose_app_test.go b/service/compose_app_test.go index ff676ca3..9d24e569 100644 --- a/service/compose_app_test.go +++ b/service/compose_app_test.go @@ -77,3 +77,29 @@ func TestNameAndTitle(t *testing.T) { assert.Assert(t, len(storeInfo.Title) > 0) assert.Equal(t, storeComposeApp.Name, storeInfo.Title[common.DefaultLanguage]) } + +func TestUncontrolledApp(t *testing.T) { + logger.LogInitConsoleOnly() + + app, err := service.NewComposeAppFromYAML([]byte(common.SampleComposeAppYAML), true, false) + assert.NilError(t, err) + + storeInfo, err := app.StoreInfo(false) + assert.NilError(t, err) + // assert nil + assert.Assert(t, storeInfo.IsUncontrolled == nil) + + err = app.SetUncontrolled(true) + assert.NilError(t, err) + + storeInfo, err = app.StoreInfo(false) + assert.NilError(t, err) + assert.Assert(t, *storeInfo.IsUncontrolled) + + err = app.SetUncontrolled(false) + assert.NilError(t, err) + + storeInfo, err = app.StoreInfo(false) + assert.NilError(t, err) + assert.Assert(t, !*storeInfo.IsUncontrolled) +}