Repository navigation
Expand file tree
/
Copy pathstore.go
More file actions
138 lines (126 loc) · 3.47 KB
/
Copy pathstore.go
File metadata and controls
138 lines (126 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package gocommerce
import (
"os"
"path/filepath"
"strings"
)
var commonDocRoots = []string{
"$PWD", // convenient when manually searching for current dir
"$DOCROOT",
"$HOME/public_html/deployments/current",
"$HOME/public_html/current",
"$HOME/public_html",
"$HOME/www",
"$HOME/httpdocs",
"$HOME/master/Magento1Website",
"$HOME/current",
"$HOME/magento2",
"/httpdocs", // youwe
"/app/$USER", // adobe commerce
"$HOME/var/$SITE_NAME/logs",
"/var/www/html",
"$HOME/html", // jetrails
"/home/jetrails/*/html", // jetrails
"$HOME/applications/*/public_html", // cloudways
"/data/web/public", // hypernode
"/home/cloudpanel/htdocs/*/current",
"/home/cloudpanel/htdocs/*",
"/code",
"/domains/*/http", // sonassi
"/srv/*",
"$HOME/domains/*/public_html",
"/var/www/*",
"/var/www/*/current",
"/var/www/*/public_html",
"/var/www/vhosts/*/htdocs",
"/vhosts/*/httpdocs", // plesk
"$HOME/public_html/..", // nexcess
"$HOME/public/..", // hypernode
"$HOME/public", // hypernode, nimbus hosting
"$HOME/??*.*", // generic domain pattern
}
func (s *Store) ConfigPath() string {
return filepath.Join(s.DocRoot, s.Platform.ConfigPath())
}
func FindStoreAtRoot(docroot string) *Store {
for _, pl := range AllPlatforms {
path := filepath.Join(docroot, pl.UniquePath())
if !pathExists(path) {
continue
}
if s := docrootToStore(docroot, pl); s != nil {
return s
}
}
return nil
}
// For recursively walking the filesystem:
// derive a store from a uniquely identifying path
func FindStoreAtUniquePath(path string) *Store {
for _, pl := range AllPlatforms {
if !strings.HasSuffix(path, pl.UniquePath()) {
continue
}
docroot := path[:len(path)-len(pl.UniquePath())-1]
if s := docrootToStore(docroot, pl); s != nil {
return s
}
}
return nil
}
func findDocRoots() []string {
var err error
var roots []string
for _, dr := range commonDocRoots {
dr = os.ExpandEnv(dr)
// Following is required to support "$HOME/public_html/.." in possible store paths.
// There, public_html being a symlink to magento2/pub (Nexcess and others).
// Without this lookup, public_html/.. would get lexically parsed by filepath.Clean()
// ending up at the parent of public_html instead of magento2. --wdg
if strings.Contains(dr, "/..") && !strings.Contains(dr, "*") && !strings.Contains(dr, "?") {
dr, err = filepath.EvalSymlinks(dr)
if err != nil { // eg some path component does not exist
continue
}
}
// extrapolate possible globs
allPaths, err := filepath.Glob(dr)
if err != nil {
continue
}
roots = append(roots, allPaths...)
}
// deduplicate while preserving order (first occurrence wins)
// this ensures $PWD, $DOCROOT and friends take priority
seen := make(map[string]bool)
result := []string{}
for _, r := range roots {
if !seen[r] {
seen[r] = true
result = append(result, r)
}
}
return result
}
// DiscoverStores searches several common docroot locations for stores
func DiscoverStores() []*Store {
stores := []*Store{}
for _, p := range findDocRoots() {
if s := FindStoreAtRoot(p); s != nil {
stores = append(stores, s)
}
}
return stores
}
func docrootToStore(docroot string, pl PlatformInterface) *Store {
cfgPath := filepath.Join(docroot, pl.ConfigPath())
cfg, err := pl.ParseConfig(cfgPath)
if err != nil {
cfg = &StoreConfig{DB: &DBConfig{}}
}
return &Store{docroot, pl, cfg}
}
func pathExists(p string) bool {
_, err := os.Stat(p)
return err == nil
}