Skip to content

Commit

Permalink
Passes through log level to mock engine.
Browse files Browse the repository at this point in the history
Allows engine version to be set.
  • Loading branch information
outofcoffee committed Aug 28, 2021
1 parent 4873908 commit 3971971
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ Usage:
imposter [command]
Available Commands:
help Help about any command
mock Start live mocks of APIs
help Help about any command
```

Create and start mocks:
Expand All @@ -40,7 +40,11 @@ Create and start mocks:
Starts a live mock of your APIs, using their Imposter configuration.
Usage:
imposter mock [CONFIG_DIR]
imposter mock [CONFIG_DIR] [flags]
Flags:
-p, --port string Port on which to listen (default "8080")
-v, --version string Imposter engine version (default "latest")
```

Help:
Expand Down
22 changes: 15 additions & 7 deletions cmd/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cmd
import (
"context"
"fmt"
"gatehill.io/imposter/util"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
Expand All @@ -31,12 +32,14 @@ import (
"os"
"os/signal"
"strconv"
"strings"
"syscall"
)

const EngineDockerImage = "outofcoffee/imposter"
const ContainerConfigDir = "/opt/imposter/config"

var ImageTag string
var Port string

// mockCmd represents the mock command
Expand All @@ -61,6 +64,7 @@ var mockCmd = &cobra.Command{
}

func init() {
mockCmd.Flags().StringVarP(&ImageTag, "version", "v", "latest", "Imposter engine version")
mockCmd.Flags().StringVarP(&Port, "port", "p", "8080", "Port on which to listen")
rootCmd.AddCommand(mockCmd)
}
Expand All @@ -74,17 +78,18 @@ func startMockEngine(configDir string, port int) {
panic(err)
}

reader, err := cli.ImagePull(ctx, "docker.io/"+EngineDockerImage, types.ImagePullOptions{})
imageAndTag := EngineDockerImage + ":" + ImageTag
logrus.Infof("checking '%v' image", ImageTag)
reader, err := cli.ImagePull(ctx, "docker.io/"+imageAndTag, types.ImagePullOptions{})
if err != nil {
panic(err)
}

var pullLogDestination io.Writer
if logrus.IsLevelEnabled(logrus.DebugLevel) {
if logrus.IsLevelEnabled(logrus.TraceLevel) {
pullLogDestination = os.Stdout
} else {
pullLogDestination = ioutil.Discard
logrus.Infof("pulling latest image")
}
_, err = io.Copy(pullLogDestination, reader)
if err != nil {
Expand All @@ -95,11 +100,14 @@ func startMockEngine(configDir string, port int) {
hostPort := fmt.Sprintf("%d", port)

resp, err := cli.ContainerCreate(ctx, &container.Config{
Image: EngineDockerImage,
Image: imageAndTag,
Cmd: []string{
"--configDir=" + ContainerConfigDir,
fmt.Sprintf("--listenPort=%d", port),
},
Env: []string{
"IMPOSTER_LOG_LEVEL=" + strings.ToUpper(util.LogLevel),
},
ExposedPorts: nat.PortSet{
containerPort: {},
},
Expand Down Expand Up @@ -146,7 +154,7 @@ func startMockEngine(configDir string, port int) {
}

func stopMockEngine(cli *client.Client, ctx context.Context, containerID string) {
logrus.Infof("\rstopping mock engine...\n")
logrus.Info("\rstopping mock engine...\n")
err := cli.ContainerStop(ctx, containerID, nil)
if err != nil {
panic(err)
Expand All @@ -160,13 +168,13 @@ func stopMockEngine(cli *client.Client, ctx context.Context, containerID string)
}
case <-statusCh:
}
logrus.Debug("mock engine stopped")
logrus.Trace("mock 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")
logrus.Trace("mock engine container removed")
}

// listen for an interrupt from the OS, then attempt engine cleanup
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ package main

import (
"gatehill.io/imposter/cmd"
"gatehill.io/imposter/util"
"github.com/sirupsen/logrus"
"os"
)

const DefaultLogLevel = "info"
const DefaultLogLevel = "debug"

func main() {
lvl, ok := os.LookupEnv("LOG_LEVEL")
Expand All @@ -34,6 +35,7 @@ func main() {
}
// set global log level
logrus.SetLevel(ll)
util.LogLevel = lvl

cmd.Execute()
}
3 changes: 3 additions & 0 deletions util/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package util

var LogLevel string

0 comments on commit 3971971

Please sign in to comment.