Skip to content

Commit

Permalink
fix incorrect error output in ListApps and ShowLocal commands (#30)
Browse files Browse the repository at this point in the history
Signed-off-by: Tiger Wang <tigerwang@outlook.com>
  • Loading branch information
tigerinus committed Apr 28, 2023
1 parent 56d0019 commit 6648f36
Show file tree
Hide file tree
Showing 20 changed files with 106 additions and 77 deletions.
1 change: 0 additions & 1 deletion cmd/appManagementApply.go
Expand Up @@ -17,7 +17,6 @@ package cmd

import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
Expand Down
1 change: 0 additions & 1 deletion cmd/appManagementInstall.go
Expand Up @@ -17,7 +17,6 @@ package cmd

import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
Expand Down
1 change: 0 additions & 1 deletion cmd/appManagementListAppStores.go
Expand Up @@ -17,7 +17,6 @@ package cmd

import (
"context"
"encoding/json"
"fmt"
"net/http"
"text/tabwriter"
Expand Down
130 changes: 74 additions & 56 deletions cmd/appManagementListApps.go
Expand Up @@ -17,7 +17,6 @@ package cmd

import (
"context"
"encoding/json"
"fmt"
"io"
"net"
Expand Down Expand Up @@ -66,85 +65,123 @@ var appManagementListAppsCmd = &cobra.Command{
if response.StatusCode != http.StatusOK {
var baseResponse app_management.BaseResponse
if err := json.Unmarshal(buf, &baseResponse); err != nil {
return fmt.Errorf("%s - %s", response.Status, response.Body)
body, err := io.ReadAll(response.Body)
if err != nil {
return err
}

message := string(body)
if message == "" {
message = "is the casaos-app-management service running?"
}

return fmt.Errorf("%s - %s", response.Status, message)
}

return fmt.Errorf("%s - %s", response.Status, *baseResponse.Message)
}

// get mapstruct of response body - can't unmarshal directly due to https://github.com/compose-spec/compose-go/issues/353
var body map[string]interface{}
if err := json.Unmarshal(buf, &body); err != nil {
return err
}

_, ok := body["data"]
if !ok {
return fmt.Errorf("body does not contain `data`")
}

data, ok := body["data"].(map[string]interface{})
if !ok {
return fmt.Errorf("data is not a map[string]interface")
}
data := json.Get(buf, "data")

w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 0, 3, ' ', 0)
defer w.Flush()

fmt.Fprintln(w, "APPID\tSTATUS\tWEB UI\tDESCRIPTION")
fmt.Fprintln(w, "-----\t------\t------\t-----------")

for id, app := range data {
status, err := status(app)
if err != nil {
status = "unknown"
fmt.Fprintln(w, "APPID\tSTATUS\tWEB UI\tIMAGES\tDESCRIPTION")
fmt.Fprintln(w, "-----\t------\t------\t------\t-----------")

for _, id := range data.Keys() {
app := data.Get(id)

status := app.Get("status").ToString()

images := []string{}
compose := app.Get("compose")
if compose.LastError() == nil {
services := compose.Get("services")
if services.LastError() == nil {
for _, id := range services.Keys() {
service := services.Get(id)
if service.LastError() == nil {
image := service.Get("image").ToString()
if image != "" {
images = append(images, image)
}
}
}
}
}

storeInfo, err := composeAppStoreInfo(app)
if err != nil || storeInfo == nil || storeInfo.Apps == nil {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n",
storeInfo := app.Get("store_info")
if storeInfo.LastError() != nil {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n",
id,
status,
"n/a",
strings.Join(images, ","),
"(not a CasaOS compose app)",
)
continue
}

scheme := "http"
if storeInfo.Scheme != nil {
scheme = string(*storeInfo.Scheme)
schemeAny := storeInfo.Get("scheme")
if schemeAny.LastError() == nil && schemeAny.ToString() != "" {
scheme = schemeAny.ToString()
}

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

if storeInfo.Hostname != nil {
hostname = *storeInfo.Hostname
hostnameAny := storeInfo.Get("hostname")
if hostnameAny.LastError() == nil && hostnameAny.ToString() != "" {
hostname = hostnameAny.ToString()
}

portMap := "unknown"
portMapAny := storeInfo.Get("port_map")
if portMapAny.LastError() == nil && portMapAny.ToString() != "" {
portMap = portMapAny.ToString()
}

index := ""
indexAny := storeInfo.Get("index")
if indexAny.LastError() == nil && indexAny.ToString() != "" {
index = indexAny.ToString()
}

webUI := fmt.Sprintf("%s://%s:%s/%s",
scheme,
hostname,
storeInfo.PortMap,
strings.TrimLeft(storeInfo.Index, "/"),
portMap,
strings.TrimLeft(index, "/"),
)

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

if storeInfo.Description != nil {
description = storeInfo.Description
descriptionAny := storeInfo.Get("description")
if descriptionAny.LastError() == nil {
for _, key := range descriptionAny.Keys() {
description[key] = descriptionAny.Get(key).ToString()
}
}

fmt.Fprintf(w, "%s\t%s\t%s\t%s\n",
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n",
id,
status,
webUI,
trim(lo.Values(description)[0], 78),
strings.Join(images, ","),
trim(
lo.If(
description["en_us"] != "", description["en_us"],
).Else(
lo.Values(description)[0],
),
78,
),
)
}

Expand All @@ -166,25 +203,6 @@ func init() {
// appManagementListAppsCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

func status(composeApp interface{}) (string, error) {
composeAppMapStruct, ok := composeApp.(map[string]interface{})
if !ok {
return "", fmt.Errorf("app is not a map[string]interface{}")
}

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

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

return status, nil
}

func composeAppStoreInfo(composeApp interface{}) (*app_management.ComposeAppStoreInfo, error) {
composeAppMapStruct, ok := composeApp.(map[string]interface{})
if !ok {
Expand Down
1 change: 0 additions & 1 deletion cmd/appManagementLogs.go
Expand Up @@ -17,7 +17,6 @@ package cmd

import (
"context"
"encoding/json"
"fmt"
"net/http"

Expand Down
1 change: 0 additions & 1 deletion cmd/appManagementRegisterAppStore.go
Expand Up @@ -17,7 +17,6 @@ package cmd

import (
"context"
"encoding/json"
"fmt"
"net/http"

Expand Down
1 change: 0 additions & 1 deletion cmd/appManagementRestart.go
Expand Up @@ -17,7 +17,6 @@ package cmd

import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
Expand Down
1 change: 0 additions & 1 deletion cmd/appManagementSearch.go
Expand Up @@ -17,7 +17,6 @@ package cmd

import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
Expand Down
13 changes: 11 additions & 2 deletions cmd/appManagementShowLocal.go
Expand Up @@ -18,7 +18,6 @@ package cmd
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -182,7 +181,17 @@ func showAppList(ctx context.Context, writer io.Writer, client *app_management.C
if response.StatusCode != http.StatusOK {
var baseResponse app_management.BaseResponse
if err := json.Unmarshal(buf, &baseResponse); err != nil {
return fmt.Errorf("%s - %s", response.Status, response.Body)
body, err := io.ReadAll(response.Body)
if err != nil {
return err
}

message := string(body)
if message == "" {
message = "is the casaos-app-management service running?"
}

return fmt.Errorf("%s - %s", response.Status, message)
}

return fmt.Errorf("%s - %s", response.Status, *baseResponse.Message)
Expand Down
1 change: 0 additions & 1 deletion cmd/appManagementStart.go
Expand Up @@ -17,7 +17,6 @@ package cmd

import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
Expand Down
1 change: 0 additions & 1 deletion cmd/appManagementStop.go
Expand Up @@ -17,7 +17,6 @@ package cmd

import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
Expand Down
1 change: 0 additions & 1 deletion cmd/appManagementUninstall.go
Expand Up @@ -17,7 +17,6 @@ package cmd

import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
Expand Down
1 change: 0 additions & 1 deletion cmd/appManagementUnregisterAppStore.go
Expand Up @@ -17,7 +17,6 @@ package cmd

import (
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"
Expand Down
1 change: 0 additions & 1 deletion cmd/appManagementUpdateApp.go
Expand Up @@ -17,7 +17,6 @@ package cmd

import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
Expand Down
1 change: 0 additions & 1 deletion cmd/messageBusSubscribeSocketIO.go
Expand Up @@ -16,7 +16,6 @@ limitations under the License.
package cmd

import (
"encoding/json"
"fmt"
"io"
"log"
Expand Down
1 change: 0 additions & 1 deletion cmd/messageBusSubscribeWebSocket.go
Expand Up @@ -16,7 +16,6 @@ limitations under the License.
package cmd

import (
"encoding/json"
"fmt"
"log"
"strings"
Expand Down
3 changes: 3 additions & 0 deletions cmd/root.go
Expand Up @@ -19,6 +19,7 @@ import (
"os"
"time"

jsoniter "github.com/json-iterator/go"
"github.com/spf13/cobra"
)

Expand All @@ -35,6 +36,8 @@ var (
Version string
Commit string
Date string

json = jsoniter.ConfigCompatibleWithStandardLibrary
)

// rootCmd represents the base command when called without any subcommands
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Expand Up @@ -7,6 +7,7 @@ require (
github.com/compose-spec/compose-go v1.11.0
github.com/deepmap/oapi-codegen v1.12.4
github.com/docker/compose/v2 v2.16.0
github.com/json-iterator/go v1.1.12
github.com/mitchellh/mapstructure v1.5.0
github.com/samber/lo v1.37.0
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
Expand All @@ -31,14 +32,16 @@ require (
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/moby/term v0.0.0-20221205130635-1aeaba878587 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect
golang.org/x/mod v0.7.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/tools v0.3.0 // indirect
)

Expand Down

0 comments on commit 6648f36

Please sign in to comment.