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

improve magento2 base url detection #23

Merged
merged 3 commits into from
Sep 13, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
only-new-issues: true
2 changes: 1 addition & 1 deletion .github/workflows/unit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: go test
- run: go test
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand Down
26 changes: 17 additions & 9 deletions magento2.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"os"
"path/filepath"
"regexp"
"slices"
"strconv"
"strings"

"github.com/sansecio/gocommerce/phpcfg"
)
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -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)
}
}
Expand Down
Loading