Skip to content

Commit

Permalink
update logic to adopt new x-casaos extension structure for compose …
Browse files Browse the repository at this point in the history
…app (#27)

Signed-off-by: Tiger Wang <tigerwang@outlook.com>
  • Loading branch information
tigerinus committed Apr 4, 2023
1 parent 8b85885 commit 791cf83
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 58 deletions.
71 changes: 28 additions & 43 deletions cmd/appManagementListApps.go
Expand Up @@ -100,49 +100,54 @@ var appManagementListAppsCmd = &cobra.Command{
status = "unknown"
}

mainApp, appList, err := appList(app)
if err != nil {
storeInfo, err := composeAppStoreInfo(app)
if err != nil || storeInfo == nil || storeInfo.Apps == nil {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n",
id,
status,
"n/a",
"(not a CasaOS compose app)",
)
return nil
continue
}

mainAppStoreInfo, ok := appList[mainApp]
if !ok {
return fmt.Errorf("main app not found in app list")
var mainAppStoreInfo app_management.AppStoreInfo

if storeInfo.Main != nil {
mainAppStoreInfo = (*storeInfo.Apps)[*storeInfo.Main]
} else {
for _, mainAppStoreInfo = range *storeInfo.Apps {
break
}
}

scheme := "http"
if mainAppStoreInfo.Container.Scheme != nil {
scheme = string(*mainAppStoreInfo.Container.Scheme)
if mainAppStoreInfo.Scheme != nil {
scheme = string(*mainAppStoreInfo.Scheme)
}

hostname, err := hostname()
if err != nil {
return err
}

if mainAppStoreInfo.Container.Hostname != nil {
hostname = *mainAppStoreInfo.Container.Hostname
if mainAppStoreInfo.Hostname != nil {
hostname = *mainAppStoreInfo.Hostname
}

webUI := fmt.Sprintf("%s://%s:%s/%s",
scheme,
hostname,
mainAppStoreInfo.Container.PortMap,
strings.TrimLeft(mainAppStoreInfo.Container.Index, "/"),
mainAppStoreInfo.PortMap,
strings.TrimLeft(mainAppStoreInfo.Index, "/"),
)

description := map[string]string{
"en_US": "No description available",
}

if mainAppStoreInfo.Description != nil {
description = mainAppStoreInfo.Description
if storeInfo.Description != nil {
description = storeInfo.Description
}

fmt.Fprintf(w, "%s\t%s\t%s\t%s\n",
Expand Down Expand Up @@ -190,49 +195,29 @@ func status(composeApp interface{}) (string, error) {
return status, nil
}

func appList(composeApp interface{}) (string, map[string]app_management.AppStoreInfo, error) {
func composeAppStoreInfo(composeApp interface{}) (*app_management.ComposeAppStoreInfo, error) {
composeAppMapStruct, ok := composeApp.(map[string]interface{})
if !ok {
return "", nil, fmt.Errorf("app is not a map[string]interface{}")
return nil, fmt.Errorf("app is not a map[string]interface{}")
}

_, ok = composeAppMapStruct["store_info"]
if !ok {
return "", nil, fmt.Errorf("app does not have \"store_info\"")
}

composeAppStoreInfo, ok := composeAppMapStruct["store_info"].(map[string]interface{})
if !ok {
return "", nil, fmt.Errorf("app[\"store_info\"] is not a map[string]interface{}")
}

_, ok = composeAppStoreInfo["main_app"]
if !ok {
return "", nil, fmt.Errorf("app[\"store_info\"] does not have \"main_app\"")
}

mainApp, ok := composeAppStoreInfo["main_app"].(string)
if !ok {
return "", nil, fmt.Errorf("app[\"store_info\"][\"main_app\"] is not a string")
}

_, ok = composeAppStoreInfo["apps"]
if !ok {
return "", nil, fmt.Errorf("app[\"store_info\"] does not have \"apps\"")
return nil, fmt.Errorf("app does not have \"store_info\"")
}

appListMapStruct, ok := composeAppStoreInfo["apps"].(map[string]interface{})
composeAppStoreInfoMapStruct, ok := composeAppMapStruct["store_info"].(map[string]interface{})
if !ok {
return "", nil, fmt.Errorf("app[\"store_info\"][\"apps\"] is not a map[string]interface{}")
return nil, fmt.Errorf("app[\"store_info\"] is not a map[string]interface{}")
}

var appList map[string]app_management.AppStoreInfo
composeAppStoreInfo := &app_management.ComposeAppStoreInfo{}

if err := mapstructure.Decode(appListMapStruct, &appList); err != nil {
return "", nil, err
if err := mapstructure.Decode(composeAppStoreInfoMapStruct, composeAppStoreInfo); err != nil {
return nil, err
}

return mainApp, appList, nil
return composeAppStoreInfo, nil
}

func hostname() (string, error) {
Expand Down
14 changes: 1 addition & 13 deletions cmd/appManagementSearch.go
Expand Up @@ -121,23 +121,11 @@ var appManagementSearchCmd = &cobra.Command{
fmt.Fprintln(w, "----\t--------\t------\t---------\t-----------")

for storeAppID, composeApp := range *response.JSON200.Data.List {
if composeApp.Apps == nil || len(*composeApp.Apps) == 0 {
fmt.Printf("skipping compose app %s because it has no apps", storeAppID)
continue
}

if composeApp.MainApp == nil || *composeApp.MainApp == "" {
fmt.Printf("skipping compose app %s because it has no main app", storeAppID)
continue
}

mainApp := (*composeApp.Apps)[*composeApp.MainApp]

if lo.Contains(installedList, storeAppID) {
storeAppID = fmt.Sprintf("%s [installed]", storeAppID)
}

fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", storeAppID, mainApp.Category, mainApp.Author, mainApp.Developer, trim(mainApp.Description["en_US"], 78))
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", storeAppID, composeApp.Category, composeApp.Author, composeApp.Developer, trim(composeApp.Description["en_US"], 78))
}

return nil
Expand Down
9 changes: 7 additions & 2 deletions cmd/appManagementShowLocal.go
Expand Up @@ -204,12 +204,17 @@ func showAppList(ctx context.Context, writer io.Writer, client *app_management.C
return fmt.Errorf("data is not a map[string]interface")
}

mainApp, appList, err := appList(data)
storeInfo, err := composeAppStoreInfo(data)
if err != nil {
return err
}

for name, app := range appList {
mainApp := "unknown"
if storeInfo.Main != nil {
mainApp = *storeInfo.Main
}

for name, app := range *storeInfo.Apps {
var buf bytes.Buffer

enc := json.NewEncoder(&buf)
Expand Down

0 comments on commit 791cf83

Please sign in to comment.