Skip to content
Draft
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
23 changes: 23 additions & 0 deletions cmd/magebox/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"qoliber/magebox/internal/nginx"
"qoliber/magebox/internal/php"
"qoliber/magebox/internal/platform"
"qoliber/magebox/internal/portforward"
)

var globalCmd = &cobra.Command{
Expand Down Expand Up @@ -105,6 +106,17 @@ func runGlobalStart(cmd *cobra.Command, args []string) error {
}
}

// Load port forwarding daemon on macOS so that ports 80/443 are forwarded
pfMgr := portforward.NewManager()
if pfMgr.IsInstalled() {
fmt.Print(" Port forwarding... ")
if err := pfMgr.StartDaemon(); err != nil {
fmt.Println(cli.Error("failed: " + err.Error()))
} else {
fmt.Println(cli.Success("started"))
}
}

fmt.Println()
cli.PrintSuccess("Global services started!")
fmt.Println()
Expand Down Expand Up @@ -143,6 +155,17 @@ func runGlobalStop(cmd *cobra.Command, args []string) error {
}
}

// Unload port forwarding daemon on macOS to free ports 80/443
pfMgr := portforward.NewManager()
if pfMgr.IsInstalled() {
fmt.Print(" Port forwarding... ")
if err := pfMgr.StopDaemon(); err != nil {
fmt.Printf("failed: %v\n", err)
} else {
fmt.Println("stopped")
}
}

fmt.Println("\nGlobal services stopped!")
return nil
}
Expand Down
39 changes: 39 additions & 0 deletions internal/portforward/pf.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,45 @@ func (m *Manager) needsUpgrade() bool {
return false
}

// StopDaemon unloads the LaunchDaemon to free web ports without removing the
// plist file. The daemon can be restarted with StartDaemon.
func (m *Manager) StopDaemon() error {
if m.platform != "darwin" {
return nil
}

if !m.IsInstalled() {
return nil
}

if !m.isDaemonLoaded() {
return nil
}

return m.unloadLaunchDaemon()
}

// StartDaemon loads the LaunchDaemon so that web ports are forwarded again.
func (m *Manager) StartDaemon() error {
if m.platform != "darwin" {
return nil
}

if !m.IsInstalled() {
return fmt.Errorf("port forwarding not configured — run 'magebox bootstrap' first")
}

if m.isDaemonLoaded() {
// Already loaded; make sure it is actually listening
if !m.AreRulesActive() {
return m.kickstartDaemon()
}
return nil
}

return m.loadLaunchDaemon()
}

// Remove uninstalls port forwarding
func (m *Manager) Remove() error {
if m.platform != "darwin" {
Expand Down
4 changes: 2 additions & 2 deletions vitepress/reference/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -1251,7 +1251,7 @@ Start global services.
magebox global start
```

Starts Nginx and Docker services.
Starts Nginx and Docker services. On macOS, also loads the port-forwarding LaunchDaemon so that ports 80 and 443 are forwarded to Nginx.

---

Expand All @@ -1263,7 +1263,7 @@ Stop all MageBox services.
magebox global stop
```

Stops all Docker containers and Nginx.
Stops all Docker containers and Nginx. On macOS, also unloads the port-forwarding LaunchDaemon so that ports 80 and 443 are released back to the system.

---

Expand Down
Loading