Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update logic to adopt new x-casaos extension structure for compose app #27

Merged
merged 2 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 28 additions & 43 deletions cmd/appManagementListApps.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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