Skip to content

Commit

Permalink
feat: allow yaml variant of configs
Browse files Browse the repository at this point in the history
  • Loading branch information
shyim committed Oct 29, 2024
1 parent 532208a commit 3b0e4bb
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ dist/
/local
/.shopware-cli
/.shopware-project.yml
/.shopware-project.yaml
/test-app
/go-shopware-admin-api-sdk
/*.zip
Expand Down
2 changes: 1 addition & 1 deletion cmd/account/account_producer_extension_info_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ var accountCompanyProducerExtensionInfoPullCmd = &cobra.Command{
return fmt.Errorf("cannot encode yaml: %w", err)
}

extCfgFile := fmt.Sprintf("%s/%s", zipExt.GetPath(), ".shopware-extension.yml")
extCfgFile := fmt.Sprintf("%s/%s", zipExt.GetPath(), newCfg.FileName)
err = os.WriteFile(extCfgFile, content, os.ModePerm)

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/extension/extension_admin_watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var extensionAdminWatchCmd = &cobra.Command{
for _, extensionPath := range args[:len(args)-1] {
ext, err := extension.GetExtensionByFolder(extensionPath)
if err != nil {
shopCfg, err := shop.ReadConfig(path.Join(extensionPath, ".shopware-project.yml"), true)
shopCfg, err := shop.ReadConfig(path.Join(extensionPath, shop.DefaultConfigFileName()), true)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/project/project.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package project

import (
"github.com/FriendsOfShopware/shopware-cli/shop"
"github.com/spf13/cobra"
)

Expand All @@ -13,5 +14,5 @@ var projectRootCmd = &cobra.Command{

func Register(rootCmd *cobra.Command) {
rootCmd.AddCommand(projectRootCmd)
projectRootCmd.PersistentFlags().StringVar(&projectConfigPath, "project-config", ".shopware-project.yml", "Path to .shopware-project.yml")
projectRootCmd.PersistentFlags().StringVar(&projectConfigPath, "project-config", shop.DefaultConfigFileName(), "Path to config")
}
19 changes: 10 additions & 9 deletions cmd/project/project_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ import (
"compress/gzip"
"database/sql"
"fmt"
"io"
"os"
"strings"

"github.com/doutorfinancas/go-mad/core"
"github.com/doutorfinancas/go-mad/database"
"github.com/doutorfinancas/go-mad/generator"
"github.com/klauspost/compress/zstd"
"github.com/spf13/cobra"
"go.uber.org/zap"
"io"
"os"
"strings"

"github.com/FriendsOfShopware/shopware-cli/logging"
"github.com/FriendsOfShopware/shopware-cli/shop"
Expand Down Expand Up @@ -149,9 +148,7 @@ var projectDatabaseDumpCmd = &cobra.Command{

var projectCfg *shop.Config
if projectCfg, err = shop.ReadConfig(projectConfigPath, true); err != nil {
if !strings.Contains(err.Error(), "cannot find .shopware-project.yml") {
return err
}
return err
}

if projectCfg != nil && projectCfg.ConfigDump != nil {
Expand Down Expand Up @@ -207,6 +204,10 @@ var projectDatabaseDumpCmd = &cobra.Command{
}

if err = dumper.Dump(w); err != nil {
if strings.Contains(err.Error(), "the RELOAD or FLUSH_TABLES privilege") {
return fmt.Errorf("%s, you maybe want to disable locking with --skip-lock-tables", err.Error())
}

return err
}

Expand All @@ -225,8 +226,8 @@ var projectDatabaseDumpCmd = &cobra.Command{
func init() {
projectRootCmd.AddCommand(projectDatabaseDumpCmd)
projectDatabaseDumpCmd.Flags().String("host", "127.0.0.1", "hostname")
projectDatabaseDumpCmd.Flags().String("username", "root", "mysql user")
projectDatabaseDumpCmd.Flags().String("password", "root", "mysql password")
projectDatabaseDumpCmd.Flags().StringP("username", "u", "root", "mysql user")
projectDatabaseDumpCmd.Flags().StringP("password", "p", "root", "mysql password")
projectDatabaseDumpCmd.Flags().String("port", "3306", "mysql port")
projectDatabaseDumpCmd.Flags().String("output", "dump.sql", "file or - (for stdout)")
projectDatabaseDumpCmd.Flags().Bool("clean", false, "Ignores cart, enqueue, message_queue_stats")
Expand Down
23 changes: 14 additions & 9 deletions extension/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package extension
import (
"fmt"
"os"
"path/filepath"

"github.com/FriendsOfShopware/shopware-cli/internal/changelog"

Expand Down Expand Up @@ -31,7 +32,7 @@ type ConfigBuild struct {
Pack struct {
Excludes struct {
Paths []string `yaml:"paths,omitempty"`
} `yaml:"excludes"`
} `yaml:"excludes,omitempty"`
BeforeHooks []string `yaml:"before_hooks,omitempty"`
} `yaml:"pack"`
} `yaml:"zip"`
Expand Down Expand Up @@ -93,29 +94,31 @@ type ConfigStoreImagePreview struct {
}

type Config struct {
FileName string
Store ConfigStore `yaml:"store"`
Build ConfigBuild `yaml:"build"`
Changelog changelog.Config `yaml:"changelog"`
}

func readExtensionConfig(dir string) (*Config, error) {
errorFormat := "file: " + dir + "/.shopware-extension.yml: %v"
config := &Config{}
config.Build.Zip.Assets.Enabled = true
config.Build.Zip.Composer.Enabled = true
config.FileName = ".shopware-extension.yml"

fileName := fmt.Sprintf("%s/.shopware-extension.yml", dir)
_, err := os.Stat(fileName)
configLocation := ""

if os.IsNotExist(err) {
if _, err := os.Stat(filepath.Join(dir, ".shopware-extension.yml")); err == nil {
configLocation = filepath.Join(dir, ".shopware-extension.yml")
} else if _, err := os.Stat(filepath.Join(dir, ".shopware-extension.yaml")); err == nil {
configLocation = filepath.Join(dir, ".shopware-extension.yaml")
} else {
return config, nil
}

if err != nil {
return nil, err
}
errorFormat := "file: " + configLocation + ": %v"

fileHandle, err := os.ReadFile(fileName)
fileHandle, err := os.ReadFile(configLocation)
if err != nil {
return nil, fmt.Errorf(errorFormat, err)
}
Expand All @@ -126,6 +129,8 @@ func readExtensionConfig(dir string) (*Config, error) {
return nil, fmt.Errorf(errorFormat, err)
}

config.FileName = filepath.Base(configLocation)

err = validateExtensionConfig(config)
if err != nil {
return nil, fmt.Errorf(errorFormat, err)
Expand Down
19 changes: 17 additions & 2 deletions shop/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/invopop/jsonschema"
orderedmap "github.com/wk8/go-ordered-map/v2"
"os"
"path"
"strings"

"dario.cat/mergo"
Expand Down Expand Up @@ -223,7 +224,7 @@ func ReadConfig(fileName string, allowFallback bool) (*Config, error) {

fileHandle, err := os.ReadFile(fileName)
if err != nil {
return nil, fmt.Errorf("ReadConfig: %v", err)
return nil, fmt.Errorf("ReadConfig (%s): %v", fileName, err)
}

config.foundConfig = true
Expand All @@ -248,7 +249,7 @@ func ReadConfig(fileName string, allowFallback bool) (*Config, error) {
}

if err != nil {
return nil, fmt.Errorf("ReadConfig: %v", err)
return nil, fmt.Errorf("ReadConfig(%s): %v", fileName, err)
}

return fillEmptyConfig(config), nil
Expand Down Expand Up @@ -280,3 +281,17 @@ func (c Config) IsFallback() bool {
func NewUuid() string {
return strings.ReplaceAll(uuid.New().String(), "-", "")
}

func DefaultConfigFileName() string {
currentDir, err := os.Getwd()

if err != nil {
return ".shopware-project.yml"
}

if _, err := os.Stat(path.Join(currentDir, ".shopware-project.yaml")); err == nil {
return ".shopware-project.yaml"
}

return ".shopware-project.yml"
}

0 comments on commit 3b0e4bb

Please sign in to comment.