Skip to content

Commit

Permalink
feat: add mailpit command (alias of launch -m), fixes ddev#5627
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Dec 18, 2023
1 parent 955065b commit 3aeb9b1
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 2 deletions.
48 changes: 47 additions & 1 deletion cmd/ddev/cmd/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func TestCustomCommands(t *testing.T) {
assert.NoError(err)

// Make sure that all the official ddev-provided custom commands are usable by checking help
for _, c := range []string{"launch", "xdebug"} {
for _, c := range []string{"launch", "mailpit", "xdebug"} {
_, err = exec.RunHostCommand(DdevBin, c, "-h")
assert.NoError(err, "Failed to run ddev %s -h", c)
}
Expand Down Expand Up @@ -350,6 +350,52 @@ func TestLaunchCommand(t *testing.T) {
}
}

// TestMailpitCommand tests that the mailpit command behaves how it should behave
func TestMailpitCommand(t *testing.T) {
assert := asrt.New(t)

origDir, _ := os.Getwd()
// Create a temporary directory and switch to it.
testDir := testcommon.CreateTmpDir(t.Name())
err := os.Chdir(testDir)
assert.NoError(err)

t.Setenv("DDEV_DEBUG", "true")
app, err := ddevapp.NewApp(testDir, false)
require.NoError(t, err)
err = app.WriteConfig()
require.NoError(t, err)
t.Cleanup(func() {
err = os.Chdir(origDir)
assert.NoError(err)
err = app.Stop(true, false)
assert.NoError(err)
_ = os.RemoveAll(testDir)
})

// This only tests the https port changes, but that might be enough
app.RouterHTTPSPort = "8443"
err = app.WriteConfig()
assert.NoError(err)
err = app.Start()
require.NoError(t, err)

desc, err := app.Describe(false)
require.NoError(t, err)

expect := desc["mailpit_https_url"].(string)
if globalconfig.DdevGlobalConfig.MkcertCARoot == "" {
expect = desc["mailpit_url"].(string)
}

// Try with the base URL, simplest case
c := DdevBin + ` mailpit ` + ` | awk '/FULLURL/ {print $2}'`
out, err := exec.RunHostCommand("bash", "-c", c)
out = strings.Trim(out, "\r\n")
assert.NoError(err, `couldn't run "%s"", output=%s`, c, out)
assert.Contains(out, expect, "output of %s is incorrect with app.RouterHTTPSPort=%s: %s", c, app.RouterHTTPSPort, out)
}

// TestMysqlCommand tests `ddev mysql“
func TestMysqlCommand(t *testing.T) {
assert := asrt.New(t)
Expand Down
11 changes: 11 additions & 0 deletions docs/content/users/usage/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,17 @@ ddev logs -s db
ddev logs -s db my-project
```

## `mailpit`

Launch a browser with mailpit for the current project (global shell host container command).

Example:

```shell
# Open Mailpit in the default browser
ddev mailpit
```

## `magento`

Run the `magento` command; available only in projects of type `magento2`, and only works if `bin/magento` is in the project.
Expand Down
2 changes: 1 addition & 1 deletion docs/content/users/usage/developer-tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Use [`ddev composer`](../usage/commands.md#composer) (Composer inside the contai

[Mailpit](https://github.com/axllent/mailpit) is a mail catcher that’s configured to capture and display emails sent by PHP in the development environment.

After your project is started, access the Mailpit web interface at `https://mysite.ddev.site:8026`, or run [`ddev launch -m`](../usage/commands.md#launch) to launch it in your default browser.
After your project is started, access the Mailpit web interface at `https://mysite.ddev.site:8026`, or run [`ddev launch -m`](../usage/commands.md#launch) or [`ddev mailpit`](../usage/commands.md#mailpit) to launch it in your default browser.

Mailpit will **not** intercept emails if your application is configured to use SMTP or a third-party ESP integration.

Expand Down
56 changes: 56 additions & 0 deletions pkg/ddevapp/global_dotddev_assets/commands/host/mailpit
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/bin/bash

## #ddev-generated: If you want to edit and own this file, remove this line.
## Description: Launch a browser with Mailpit
## Usage: mailpit
## Example: "ddev mailpit"

# A lot of this is directly copied from the launch command.
# If launch is ever generalised to be callable from other commands,
# we should refactor this to use it.
if [ "${DDEV_PROJECT_STATUS-running}" != "running" ]; then
echo "Project ${DDEV_PROJECT} is not running, starting it"
ddev start
fi

FULLURL=${DDEV_PRIMARY_URL}
HTTPS=""
if [ ${DDEV_PRIMARY_URL%://*} = "https" ]; then HTTPS=true; fi

if [[ ! -z "${GITPOD_INSTANCE_ID}" ]] || [[ "${CODESPACES}" == "true" ]]; then
FULLURL="${FULLURL/-${DDEV_HOST_WEBSERVER_PORT}/-${DDEV_HOST_MAILPIT_PORT}}"
else
if [ "${HTTPS}" = "" ]; then
FULLURL="${FULLURL%:[0-9]*}:${DDEV_MAILPIT_PORT}"
else
FULLURL="${FULLURL%:[0-9]*}:${DDEV_MAILPIT_HTTPS_PORT}"
fi
fi

if [ -n "${1:-}" ] ; then
if [[ ${1::1} != "/" ]] ; then
FULLURL="${FULLURL}/";
fi

FULLURL="${FULLURL}${1}";
fi

if [ "${DDEV_DEBUG:-}" = "true" ]; then
printf "FULLURL $FULLURL\n" && exit 0
fi

case $OSTYPE in
linux-gnu)
if [[ ! -z "${GITPOD_INSTANCE_ID}" ]]; then
gp preview ${FULLURL}
else
xdg-open ${FULLURL}
fi
;;
"darwin"*)
open ${FULLURL}
;;
"win*"* | "msys"*)
start ${FULLURL}
;;
esac

0 comments on commit 3aeb9b1

Please sign in to comment.