Skip to content

Commit

Permalink
feat: add project debug, fixes #344
Browse files Browse the repository at this point in the history
  • Loading branch information
shyim committed Apr 21, 2024
1 parent c3d2df8 commit 93fd58c
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 2 deletions.
61 changes: 61 additions & 0 deletions cmd/project/project_debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package project

import (
"fmt"
"github.com/FriendsOfShopware/shopware-cli/extension"
"github.com/FriendsOfShopware/shopware-cli/logging"
"github.com/FriendsOfShopware/shopware-cli/shop"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"os"
"path/filepath"
)

var projectDebug = &cobra.Command{
Use: "debug",
Short: "Shows detected Shopware version and detected extensions for further debugging",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var err error
args[0], err = filepath.Abs(args[0])
if err != nil {
return err
}

shopCfg, err := shop.ReadConfig(projectConfigPath, true)
if err != nil {
return err
}

shopwareConstraint, err := extension.GetShopwareProjectConstraint(args[0])
if err != nil {
return err
}

if shopCfg.IsFallback() {
fmt.Printf("Could not find a %s, using fallback config\n", projectConfigPath)
} else {
fmt.Printf("Found config: Yes\n")
}
fmt.Printf("Detected following Shopware version: %s\n", shopwareConstraint.String())

sources := extension.FindAssetSourcesOfProject(logging.DisableLogger(cmd.Context()), args[0], shopCfg)

fmt.Println("Following extensions/bundles has been detected")
table := tablewriter.NewWriter(os.Stdout)
table.SetColWidth(100)
table.SetHeader([]string{"Name", "Path"})

for _, source := range sources {
table.Append([]string{source.Name, source.Path})
}

table.Render()

return nil
},
}

func init() {
projectRootCmd.AddCommand(projectDebug)
}
1 change: 1 addition & 0 deletions extension/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ func (p PlatformPlugin) GetPath() string {

func (p PlatformPlugin) GetMetaData() *extensionMetadata {
return &extensionMetadata{
Name: p.composer.Name,
Label: extensionTranslated{
German: p.composer.Extra.Label["de-DE"],
English: p.composer.Extra.Label["en-GB"],
Expand Down
2 changes: 1 addition & 1 deletion extension/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func FindExtensionsFromProject(ctx context.Context, project string) []Extension
version, _ := ext.GetVersion()

logging.FromContext(ctx).Infof("Found extension in custom/plugins: %s (%s)", name, version)
logging.FromContext(ctx).Errorf("Extension %s should be installed using Composer. Please remove the extension from custom/plugins.", name)
logging.FromContext(ctx).Warnf("Extension %s should be installed using Composer. Please run composer require %s.", name, ext.GetMetaData().Name)

extensions[name] = ext
}
Expand Down
1 change: 1 addition & 0 deletions extension/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type extensionTranslated struct {
}

type extensionMetadata struct {
Name string
Label extensionTranslated
Description extensionTranslated
}
Expand Down
4 changes: 4 additions & 0 deletions logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,7 @@ func FromContext(ctx context.Context) *zap.SugaredLogger {

return fallbackLogger
}

func DisableLogger(ctx context.Context) context.Context {
return WithLogger(ctx, zap.NewNop().Sugar())
}
9 changes: 8 additions & 1 deletion shop/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Config struct {
AdminApi *ConfigAdminApi `yaml:"admin_api,omitempty"`
ConfigDump *ConfigDump `yaml:"dump,omitempty"`
Sync *ConfigSync `yaml:"sync,omitempty"`
foundConfig bool
}

type ConfigBuild struct {
Expand Down Expand Up @@ -85,7 +86,7 @@ type MailTemplateTranslation struct {
}

func ReadConfig(fileName string, allowFallback bool) (*Config, error) {
config := &Config{}
config := &Config{foundConfig: false}

_, err := os.Stat(fileName)

Expand All @@ -106,6 +107,8 @@ func ReadConfig(fileName string, allowFallback bool) (*Config, error) {
return nil, fmt.Errorf("ReadConfig: %v", err)
}

config.foundConfig = true

substitutedConfig := os.ExpandEnv(string(fileHandle))
err = yaml.Unmarshal([]byte(substitutedConfig), &config)

Expand Down Expand Up @@ -140,6 +143,10 @@ func fillEmptyConfig(c *Config) *Config {
return c
}

func (c Config) IsFallback() bool {
return !c.foundConfig
}

func NewUuid() string {
return strings.ReplaceAll(uuid.New().String(), "-", "")
}

0 comments on commit 93fd58c

Please sign in to comment.