Skip to content

Commit

Permalink
refactor uncontrolled field (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
CorrectRoadH authored Apr 12, 2024
1 parent 05c9cb9 commit 3be6071
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 70 deletions.
94 changes: 24 additions & 70 deletions route/v2/compose_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"io"
"net/http"
"strings"

"github.com/IceWhaleTech/CasaOS-AppManagement/codegen"
"github.com/IceWhaleTech/CasaOS-AppManagement/common"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions service/compose_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
26 changes: 26 additions & 0 deletions service/compose_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 3be6071

Please sign in to comment.