From d720a547884b891c5fce601d95702e209a99ab47 Mon Sep 17 00:00:00 2001 From: Daniel Sloof Date: Fri, 13 Sep 2024 12:45:07 +0200 Subject: [PATCH 1/3] improve magento2 base url detection --- magento2.go | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/magento2.go b/magento2.go index d083b26..12ef694 100644 --- a/magento2.go +++ b/magento2.go @@ -7,7 +7,9 @@ import ( "os" "path/filepath" "regexp" + "slices" "strconv" + "strings" "github.com/sansecio/gocommerce/phpcfg" ) @@ -69,13 +71,15 @@ func (m2 *Magento2) ParseConfig(cfgPath string) (*StoreConfig, error) { func (m2 *Magento2) BaseURLs(docroot string) ([]string, error) { cfgPath := filepath.Join(docroot, m2.ConfigPath()) - - urls, err := m2.getBaseURLsFromDatabase(cfgPath) - if err == nil { - return urls, nil + urls := []string{} + if ud, err := m2.getBaseURLsFromDatabase(cfgPath); err == nil { + urls = append(urls, ud...) } - - return m2.getBaseURLsFromConfig(cfgPath) + if uc, err := m2.getBaseURLsFromConfig(cfgPath); err == nil { + urls = append(urls, uc...) + } + slices.Sort(urls) + return slices.Compact(urls), nil } func (m2 *Magento2) Version(docroot string) (string, error) { @@ -87,20 +91,24 @@ func (m2 *Magento2) Version(docroot string) (string, error) { return getVersionFromJsonFile(docroot + "/composer.json") } +func urlIsPlaceholder(url string) bool { + return strings.HasPrefix(url, "{{") || strings.HasSuffix(url, "}}") +} + func (m2 *Magento2) getBaseURLsFromConfig(cfgPath string) ([]string, error) { cm, err := phpcfg.ParsePath(cfgPath) if err != nil { return nil, err } - r, err := regexp.Compile(`root.system.\w+.web.secure.base_url`) + r, err := regexp.Compile(`root.system.\w+.web.(un)?secure.base_url`) if err != nil { return nil, err } urls := []string{} for k, v := range cm { - if r.MatchString(k) { + if r.MatchString(k) && !urlIsPlaceholder(v) { urls = append(urls, v) } } @@ -135,7 +143,7 @@ func (m2 *Magento2) getBaseURLsFromDatabase(cfgPath string) ([]string, error) { urls := []string{} for rows.Next() { var url string - if err := rows.Scan(&url); err == nil { + if err := rows.Scan(&url); err == nil && !urlIsPlaceholder(url) { urls = append(urls, url) } } From d542bffb5a4fc7a7eea712a345b6972d20017715 Mon Sep 17 00:00:00 2001 From: Daniel Sloof Date: Fri, 13 Sep 2024 12:48:57 +0200 Subject: [PATCH 2/3] bump go version --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index bb79bb6..920a4e1 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/sansecio/gocommerce -go 1.20 +go 1.22 require ( github.com/go-sql-driver/mysql v1.5.0 // 20190901 last release is incompatible with older mysql servers From 0610817e7b4fceb418b8a4c1d521963a9b4299e7 Mon Sep 17 00:00:00 2001 From: Daniel Sloof Date: Fri, 13 Sep 2024 12:55:04 +0200 Subject: [PATCH 3/3] drop old go version from workflows --- .github/workflows/lint.yml | 6 +----- .github/workflows/unit.yaml | 2 +- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 82db92a..0b14d2b 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -14,12 +14,8 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - - uses: actions/setup-go@v4 - with: - go-version: '1.20' - cache: false - name: Lint uses: golangci/golangci-lint-action@v3 with: version: latest - only-new-issues: true \ No newline at end of file + only-new-issues: true diff --git a/.github/workflows/unit.yaml b/.github/workflows/unit.yaml index 89733f1..af3e666 100644 --- a/.github/workflows/unit.yaml +++ b/.github/workflows/unit.yaml @@ -10,4 +10,4 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - - run: go test \ No newline at end of file + - run: go test