Skip to content

Commit

Permalink
Removes mock engine container on stop.
Browse files Browse the repository at this point in the history
  • Loading branch information
outofcoffee committed Aug 28, 2021
1 parent 49bc1c1 commit 7c0079d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ Usage:
Available Commands:
help Help about any command
mock Start live mocks of API dependencies
mock Start live mocks of APIs
```

Create and start mocks:

```
Starts a live mock of your API dependencies, using their Imposter configuration.
Starts a live mock of your APIs, using their Imposter configuration.
Usage:
imposter mock [CONFIG_DIR]
Expand Down
26 changes: 20 additions & 6 deletions cmd/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"io"
"io/ioutil"
"os"
"os/signal"
"strconv"
Expand All @@ -41,8 +42,8 @@ var Port string
// mockCmd represents the mock command
var mockCmd = &cobra.Command{
Use: "mock [CONFIG_DIR]",
Short: "Start live mocks of API dependencies",
Long: `Starts a live mock of your API dependencies, using their Imposter configuration.`,
Short: "Start live mocks of APIs",
Long: `Starts a live mock of your APIs, using their Imposter configuration.`,
Args: cobra.RangeArgs(0, 1),
Run: func(cmd *cobra.Command, args []string) {
var configDir string
Expand Down Expand Up @@ -77,7 +78,15 @@ func startMockEngine(configDir string, port int) {
if err != nil {
panic(err)
}
_, err = io.Copy(os.Stdout, reader)

var pullLogDestination io.Writer
if logrus.IsLevelEnabled(logrus.DebugLevel) {
pullLogDestination = os.Stdout
} else {
pullLogDestination = ioutil.Discard
logrus.Infof("pulling latest image")
}
_, err = io.Copy(pullLogDestination, reader)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -120,7 +129,7 @@ func startMockEngine(configDir string, port int) {
}

trapExit(cli, ctx, resp.ID)
println("container engine started - press ctrl+c to stop")
logrus.Info("mock engine started - press ctrl+c to stop")

out, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{
ShowStdout: true,
Expand All @@ -147,12 +156,17 @@ func stopMockEngine(cli *client.Client, ctx context.Context, containerID string)
select {
case err := <-errCh:
if err != nil {
panic(err)
logrus.Warnf("failed to stop mock engine: %v", err)
}
case <-statusCh:
}
logrus.Debug("mock engine stopped")

println("container engine stopped")
err = cli.ContainerRemove(ctx, containerID, types.ContainerRemoveOptions{})
if err != nil {
logrus.Warnf("failed to remove mock engine container: %v", err)
}
logrus.Debug("mock engine container removed")
}

// listen for an interrupt from the OS, then attempt engine cleanup
Expand Down

0 comments on commit 7c0079d

Please sign in to comment.