Skip to content

Commit e36a7ac

Browse files
committed
chore: Refactor startAllSites and stopAllSites functions to use os.ReadDir
1 parent 727fca5 commit e36a7ac

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

cmd/global.go

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -109,27 +109,28 @@ func startAllSites() {
109109
sitesDir := "/home/fly"
110110
foundSite := false
111111

112-
filepath.Walk(sitesDir, func(path string, info os.FileInfo, err error) error {
113-
if err != nil {
114-
return err
115-
}
112+
entries, err := os.ReadDir(sitesDir)
113+
if err != nil {
114+
fmt.Printf("Error reading directory %s: %v\n", sitesDir, err)
115+
return
116+
}
116117

117-
// Skip hidden directories
118-
if info.IsDir() && strings.HasPrefix(info.Name(), ".") {
119-
return filepath.SkipDir
120-
}
118+
for _, entry := range entries {
119+
if entry.IsDir() {
120+
// Skip hidden directories
121+
if strings.HasPrefix(entry.Name(), ".") {
122+
continue
123+
}
121124

122-
if info.IsDir() && filepath.Base(path) != "fly" {
125+
path := filepath.Join(sitesDir, entry.Name())
123126
composePath := filepath.Join(path, "docker-compose.yml")
124127
if _, err := os.Stat(composePath); err == nil {
125128
fmt.Printf("Starting site in %s\n", path)
126129
docker.RunCompose(composePath, "up", "-d")
127130
foundSite = true
128131
}
129132
}
130-
131-
return nil
132-
})
133+
}
133134

134135
if !foundSite {
135136
fmt.Println("No sites found to start.")
@@ -139,26 +140,29 @@ func startAllSites() {
139140
func stopAllSites() {
140141
sitesDir := "/home/fly"
141142
foundSite := false
142-
filepath.Walk(sitesDir, func(path string, info os.FileInfo, err error) error {
143-
if err != nil {
144-
return err
145-
}
146143

147-
// Skip hidden directories
148-
if info.IsDir() && strings.HasPrefix(info.Name(), ".") {
149-
return filepath.SkipDir
150-
}
144+
entries, err := os.ReadDir(sitesDir)
145+
if err != nil {
146+
fmt.Printf("Error reading directory %s: %v\n", sitesDir, err)
147+
return
148+
}
149+
150+
for _, entry := range entries {
151+
if entry.IsDir() {
152+
// Skip hidden directories
153+
if strings.HasPrefix(entry.Name(), ".") {
154+
continue
155+
}
151156

152-
if info.IsDir() && filepath.Base(path) != "fly" {
157+
path := filepath.Join(sitesDir, entry.Name())
153158
composePath := filepath.Join(path, "docker-compose.yml")
154159
if _, err := os.Stat(composePath); err == nil {
155-
fmt.Printf("Stopping site in %s\n", path)
156-
docker.RunCompose(composePath, "down")
160+
fmt.Printf("Starting site in %s\n", path)
161+
docker.RunCompose(composePath, "up", "-d")
157162
foundSite = true
158163
}
159164
}
160-
return nil
161-
})
165+
}
162166

163167
if !foundSite {
164168
fmt.Println("No sites found to stop.")

0 commit comments

Comments
 (0)