From 397197105399a1a4b63510099a61ccf3dd72827c Mon Sep 17 00:00:00 2001 From: Pete Cornish Date: Sat, 28 Aug 2021 01:55:40 +0100 Subject: [PATCH] Passes through log level to mock engine. Allows engine version to be set. --- README.md | 8 ++++++-- cmd/mock.go | 22 +++++++++++++++------- main.go | 4 +++- util/config.go | 3 +++ 4 files changed, 27 insertions(+), 10 deletions(-) create mode 100644 util/config.go diff --git a/README.md b/README.md index e1ceb29..138a746 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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: diff --git a/cmd/mock.go b/cmd/mock.go index f0538ef..72accc5 100644 --- a/cmd/mock.go +++ b/cmd/mock.go @@ -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" @@ -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 @@ -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) } @@ -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 { @@ -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: {}, }, @@ -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) @@ -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 diff --git a/main.go b/main.go index aba5f3d..c54a139 100644 --- a/main.go +++ b/main.go @@ -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") @@ -34,6 +35,7 @@ func main() { } // set global log level logrus.SetLevel(ll) + util.LogLevel = lvl cmd.Execute() } diff --git a/util/config.go b/util/config.go new file mode 100644 index 0000000..833a99c --- /dev/null +++ b/util/config.go @@ -0,0 +1,3 @@ +package util + +var LogLevel string