Skip to content

Commit

Permalink
Merge pull request #427 from alexandre-daubois/feat-stop-all
Browse files Browse the repository at this point in the history
Add support for `--all` flag on `server:stop`
  • Loading branch information
fabpot committed Feb 9, 2024
2 parents 0ae5c79 + cce8ca1 commit 1f88541
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 11 deletions.
2 changes: 1 addition & 1 deletion commands/local_server_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func printConfiguredServers() error {
if err != nil {
return errors.WithStack(err)
}
runningProjects, err := pid.ToConfiguredProjects()
runningProjects, err := pid.ToConfiguredProjects(true)
if err != nil {
return errors.WithStack(err)
}
Expand Down
62 changes: 55 additions & 7 deletions commands/local_server_stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,68 @@ var localServerStopCmd = &console.Command{
Usage: "Stop the local web server",
Flags: []console.Flag{
dirFlag,
&console.BoolFlag{Name: "all", Usage: "Stop all local web servers"},
},
Action: func(c *console.Context) error {
projectDir, err := getProjectDir(c.String("dir"))
if c.Bool("all") && c.IsSet("dir") {
return fmt.Errorf("you cannot use the --all option with a specific directory")
}

var dirs []string

if c.Bool("all") {
configuredAndRunning, err := pid.ToConfiguredProjects(false)
if err != nil {
return err
}

for dir := range configuredAndRunning {
dirs = append(dirs, dir)
}
} else {
projectDir, err := getProjectDir(c.String("dir"))
if err != nil {
return err
}

dirs = append(dirs, projectDir)
}

return stopProjects(dirs, c.Bool("all"))
},
}

func stopProjects(dirs []string, allFlag bool) error {
ui := terminal.SymfonyStyle(terminal.Stdout, terminal.Stdin)
running := 0

if len(dirs) == 0 {
ui.Success("No local web servers to stop")

return nil
}

for _, dir := range dirs {
projectDir, err := getProjectDir(dir)
runningProcessesForProject := 0
if err != nil {
return err
}
ui := terminal.SymfonyStyle(terminal.Stdout, terminal.Stdin)

if allFlag {
ui.Section(fmt.Sprintf("Stopping project %s", projectDir))
}

webserver := pid.New(projectDir, nil)
pids := append(pid.AllWorkers(projectDir), webserver)
var g errgroup.Group
running := 0
for _, p := range pids {
if !p.IsRunning() {
continue
}

running++
runningProcessesForProject++
g.Go(p.WaitForExit)

// we first notify the webserver in order to let it know it should
Expand All @@ -63,9 +108,9 @@ var localServerStopCmd = &console.Command{
}
}

if running == 0 {
if runningProcessesForProject == 0 {
ui.Success("The web server is not running")
return nil
continue
}

for _, p := range pids {
Expand All @@ -92,8 +137,11 @@ var localServerStopCmd = &console.Command{
if err := g.Wait(); err != nil {
return err
}
}

if running > 0 {
ui.Success(fmt.Sprintf("Stopped %d process(es) successfully", running))
return nil
},
}

return nil
}
4 changes: 2 additions & 2 deletions local/pid/pidfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ func (p *PidFile) Signal(sig os.Signal) error {
return process.Signal(sig)
}

func ToConfiguredProjects() (map[string]*projects.ConfiguredProject, error) {
func ToConfiguredProjects(shortenHomeDir bool) (map[string]*projects.ConfiguredProject, error) {
ps := make(map[string]*projects.ConfiguredProject)
userHomeDir, err := homedir.Dir()
if err != nil {
Expand All @@ -336,7 +336,7 @@ func ToConfiguredProjects() (map[string]*projects.ConfiguredProject, error) {
}
port := pid.Port
shortDir := pid.Dir
if strings.HasPrefix(shortDir, userHomeDir) {
if strings.HasPrefix(shortDir, userHomeDir) && shortenHomeDir {
shortDir = "~" + shortDir[len(userHomeDir):]
}
ps[shortDir] = &projects.ConfiguredProject{
Expand Down
85 changes: 85 additions & 0 deletions local/projects/configured_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (c) 2024-present Fabien Potencier <fabien@symfony.com>
*
* This file is part of Symfony CLI project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package projects

import (
"testing"
)

func TestGetConfiguredAndRunning(t *testing.T) {
proxyProjects := map[string]*ConfiguredProject{
"~/app1": {
Scheme: "https",
Port: 8000,
},
"/var/www/app2": {
Scheme: "http",
Port: 8001,
},
}

runningProjects := map[string]*ConfiguredProject{
"~/app1": {
Scheme: "https",
Port: 8000,
},
"/var/www/app2": {
Scheme: "http",
Port: 8001,
},
"/var/www/app3": {
Scheme: "ftp",
Port: 8002,
},
}

projects, err := GetConfiguredAndRunning(proxyProjects, runningProjects)
if err != nil {
t.Errorf("Error was not expected: %v", err)
}

if len(projects) != 3 {
t.Errorf("Expected 2 projects, got %d", len(projects))
}

if projects["~/app1"].Port != 8000 {
t.Errorf("Expected 8000, got %d", projects["~/app1"].Port)
}

if projects["~/app1"].Scheme != "https" {
t.Errorf("Expected \"https\", got %s", projects["~/app1"].Scheme)
}

if projects["/var/www/app2"].Port != 8001 {
t.Errorf("Expected 8001, got %d", projects["/var/www/app2"].Port)
}

if projects["/var/www/app2"].Scheme != "http" {
t.Errorf("Expected \"http\", got %s", projects["/var/www/app2"].Scheme)
}

if projects["/var/www/app3"].Port != 8002 {
t.Errorf("Expected 8002, got %d", projects["/var/www/app3"].Port)
}

if projects["/var/www/app3"].Scheme != "ftp" {
t.Errorf("Expected \"ftp\", got %s", projects["/var/www/app3"].Scheme)
}
}
2 changes: 1 addition & 1 deletion local/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ func (p *Proxy) serveIndex(w http.ResponseWriter, r *http.Request) {
if err != nil {
return
}
runningProjects, err := pid.ToConfiguredProjects()
runningProjects, err := pid.ToConfiguredProjects(true)
if err != nil {
return
}
Expand Down

0 comments on commit 1f88541

Please sign in to comment.