Skip to content

Commit

Permalink
feat: moves to dedicated logger instance in place of default.
Browse files Browse the repository at this point in the history
  • Loading branch information
outofcoffee committed Jul 28, 2022
1 parent 8e4a120 commit f7a49dd
Show file tree
Hide file tree
Showing 52 changed files with 263 additions and 252 deletions.
3 changes: 1 addition & 2 deletions cmd/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package cmd
import (
"fmt"
"gatehill.io/imposter/engine"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"strings"
)
Expand All @@ -42,7 +41,7 @@ var doctorCmd = &cobra.Command{
Long: `Checks prerequisites for running Imposter, including those needed
by the engines.`,
Run: func(cmd *cobra.Command, args []string) {
logrus.Debug("running check up...")
logger.Debug("running check up...")
println(checkPrereqs())
},
}
Expand Down
7 changes: 3 additions & 4 deletions cmd/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package cmd

import (
"gatehill.io/imposter/engine"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
"path/filepath"
Expand All @@ -44,14 +43,14 @@ func init() {
}

func stopAll(engineType engine.EngineType) {
logrus.Info("stopping all managed mocks...")
logger.Info("stopping all managed mocks...")

configDir := filepath.Join(os.TempDir(), "imposter-down")
mockEngine := engine.BuildEngine(engineType, configDir, engine.StartOptions{})

if stopped := mockEngine.StopAllManaged(); stopped > 0 {
logrus.Infof("stopped %d managed mock(s)", stopped)
logger.Infof("stopped %d managed mock(s)", stopped)
} else {
logrus.Info("no managed mocks were found")
logger.Info("no managed mocks were found")
}
}
5 changes: 2 additions & 3 deletions cmd/engine_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package cmd
import (
"gatehill.io/imposter/engine"
"github.com/olekukonko/tablewriter"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
)
Expand Down Expand Up @@ -50,14 +49,14 @@ If engine type is not specified, it defaults to all.`,
}

func list(engineTypes []engine.EngineType) {
logrus.Tracef("listing engines")
logger.Tracef("listing engines")
var available []engine.EngineMetadata

for _, e := range engineTypes {
library := engine.GetLibrary(e)
engines, err := library.List()
if err != nil {
logrus.Fatal(err)
logger.Fatal(err)
}
available = append(available, engines...)
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/engine_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package cmd

import (
"gatehill.io/imposter/engine"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -52,9 +51,9 @@ func pull(version string, engineType engine.EngineType, pullPolicy engine.PullPo
downloader := engine.GetLibrary(engineType).GetProvider(version)
err := downloader.Provide(pullPolicy)
if err != nil {
logrus.Fatal(err)
logger.Fatal(err)
}
logrus.Infof("pulled engine version %v", version)
logger.Infof("pulled engine version %v", version)
}

func init() {
Expand Down
7 changes: 3 additions & 4 deletions cmd/plugin_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package cmd
import (
"gatehill.io/imposter/engine"
"gatehill.io/imposter/plugin"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -65,11 +64,11 @@ func installPlugins(plugins []string, version string, saveDefault bool) {
ensured, err = plugin.EnsurePlugins(plugins, version, saveDefault)
}
if err != nil {
logrus.Fatal(err)
logger.Fatal(err)
}
if ensured == 0 {
logrus.Infof("no plugins to install")
logger.Infof("no plugins to install")
} else {
logrus.Infof("%d plugin(s) installed", ensured)
logger.Infof("%d plugin(s) installed", ensured)
}
}
17 changes: 8 additions & 9 deletions cmd/remote_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package cmd

import (
"gatehill.io/imposter/remote"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
)
Expand Down Expand Up @@ -74,31 +73,31 @@ func init() {
func setRemoteConfigType(dir string, remoteType string) {
active, err := remote.SaveActiveRemoteType(dir, remoteType)
if err != nil {
logrus.Fatalf("failed to set remote type: %s", err)
logger.Fatalf("failed to set remote type: %s", err)
}
logrus.Infof("set remote type to '%s' for remote: %s", remoteType, active.Name)
logger.Infof("set remote type to '%s' for remote: %s", remoteType, active.Name)
}

func setRemoteConfigUrl(dir string, url string) {
active, r, err := remote.LoadActive(dir)
if err != nil {
logrus.Fatalf("failed to load remote: %s", err)
logger.Fatalf("failed to load remote: %s", err)
}
err = (*r).SetUrl(url)
if err != nil {
logrus.Fatalf("failed to set remote URL: %s", err)
logger.Fatalf("failed to set remote URL: %s", err)
}
logrus.Infof("set remote URL to '%s' for remote: %s", url, active.Name)
logger.Infof("set remote URL to '%s' for remote: %s", url, active.Name)
}

func setRemoteConfigToken(dir string, token string) {
active, r, err := remote.LoadActive(dir)
if err != nil {
logrus.Fatalf("failed to load remote: %s", err)
logger.Fatalf("failed to load remote: %s", err)
}
err = (*r).SetToken(token)
if err != nil {
logrus.Fatalf("failed to set remote token: %s", err)
logger.Fatalf("failed to set remote token: %s", err)
}
logrus.Infof("set remote token for remote: %s", active.Name)
logger.Infof("set remote token for remote: %s", active.Name)
}
9 changes: 4 additions & 5 deletions cmd/remote_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package cmd

import (
"gatehill.io/imposter/remote"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
)
Expand Down Expand Up @@ -46,13 +45,13 @@ func init() {
func remoteDeploy(dir string) {
active, r, err := remote.LoadActive(dir)
if err != nil {
logrus.Fatalf("failed to load remote: %s", err)
logger.Fatalf("failed to load remote: %s", err)
}
logrus.Infof("deploying workspace '%s' to remote: %s", active.Name, (*r).GetUrl())
logger.Infof("deploying workspace '%s' to remote: %s", active.Name, (*r).GetUrl())

endpoint, err := (*r).Deploy()
if err != nil {
logrus.Fatalf("failed to deploy workspace: %s", err)
logger.Fatalf("failed to deploy workspace: %s", err)
}
logrus.Infof("deployed workspace '%s'\nBase URL: %s\nSpec: %s\nStatus: %s", active.Name, endpoint.BaseUrl, endpoint.SpecUrl, endpoint.StatusUrl)
logger.Infof("deployed workspace '%s'\nBase URL: %s\nSpec: %s\nStatus: %s", active.Name, endpoint.BaseUrl, endpoint.SpecUrl, endpoint.StatusUrl)
}
7 changes: 3 additions & 4 deletions cmd/remote_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package cmd

import (
"gatehill.io/imposter/remote"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
)
Expand Down Expand Up @@ -46,17 +45,17 @@ func init() {
func showRemote(dir string) {
active, r, err := remote.LoadActive(dir)
if err != nil {
logrus.Fatalf("failed to load remote: %s", err)
logger.Fatalf("failed to load remote: %s", err)
}

remoteType := (*r).GetType()
url := (*r).GetUrl()
token, err := (*r).GetObfuscatedToken()
if err != nil {
logrus.Fatalf("failed to get remote token: %s", err)
logger.Fatalf("failed to get remote token: %s", err)
}

logrus.Infof(`Workspace '%s' remote:
logger.Infof(`Workspace '%s' remote:
Type: %s
URL: %s
Token: %s`, active.Name, remoteType, url, token)
Expand Down
7 changes: 3 additions & 4 deletions cmd/remote_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package cmd

import (
"gatehill.io/imposter/remote"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
"time"
Expand Down Expand Up @@ -47,14 +46,14 @@ func init() {
func showRemoteStatus(dir string) {
active, r, err := remote.LoadActive(dir)
if err != nil {
logrus.Fatalf("failed to load remote: %s", err)
logger.Fatalf("failed to load remote: %s", err)
}

status, err := (*r).GetStatus()
if err != nil {
logrus.Fatalf("failed to get remote status: %s", err)
logger.Fatalf("failed to get remote status: %s", err)
}

lastModified := time.UnixMilli(int64(status.LastModified))
logrus.Infof("Workspace '%s' remote status: %s\nLast modified: %v", active.Name, status.Status, lastModified)
logger.Infof("Workspace '%s' remote status: %s\nLast modified: %v", active.Name, status.Status, lastModified)
}
8 changes: 5 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ package cmd
import (
"gatehill.io/imposter/config"
"gatehill.io/imposter/engine"
"github.com/sirupsen/logrus"
"gatehill.io/imposter/logging"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
"strings"
)

var logger = logging.GetLogger()

var rootFlags = struct {
cfgFile string
flagPrintVersion bool
Expand Down Expand Up @@ -101,12 +103,12 @@ func initConfig() {

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
logrus.Tracef("using CLI config file: %v", viper.ConfigFileUsed())
logger.Tracef("using CLI config file: %v", viper.ConfigFileUsed())
}
}

func initLogging() {
if rootFlags.logLevel != "" {
config.SetLogLevel(rootFlags.logLevel)
logging.SetLogLevel(rootFlags.logLevel)
}
}
11 changes: 5 additions & 6 deletions cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"gatehill.io/imposter/fileutil"
"gatehill.io/imposter/impostermodel"
"gatehill.io/imposter/plugin"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
Expand Down Expand Up @@ -74,7 +73,7 @@ If CONFIG_DIR is not specified, the current working directory is used.`,
if upFlags.flagEnsurePlugins {
_, err := plugin.EnsureDefaultPlugins(version)
if err != nil {
logrus.Fatal(err)
logger.Fatal(err)
}
}

Expand All @@ -85,7 +84,7 @@ If CONFIG_DIR is not specified, the current working directory is used.`,
configDir, _ = filepath.Abs(args[0])
}
if err := validateConfigExists(configDir, upFlags.flagScaffoldMissing); err != nil {
logrus.Fatal(err)
logger.Fatal(err)
}

startOptions := engine.StartOptions{
Expand Down Expand Up @@ -145,7 +144,7 @@ func validateConfigExists(configDir string, scaffoldMissing bool) error {
}

if scaffoldMissing {
logrus.Infof("scaffolding Imposter configuration files")
logger.Infof("scaffolding Imposter configuration files")
impostermodel.CreateFromSpecs(configDir, false, false, impostermodel.ScriptEngineNone)
return nil
}
Expand All @@ -166,14 +165,14 @@ func start(lib *engine.EngineLibrary, startOptions engine.StartOptions, configDi
go func() {
for {
<-dirUpdated
logrus.Infof("detected change in: %v - triggering restart", configDir)
logger.Infof("detected change in: %v - triggering restart", configDir)
mockEngine.Restart(wg)
}
}()
}

wg.Wait()
logrus.Debug("shutting down")
logger.Debug("shutting down")
}

// listen for an interrupt from the OS, then attempt engine cleanup
Expand Down
3 changes: 1 addition & 2 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"fmt"
"gatehill.io/imposter/config"
"gatehill.io/imposter/engine"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -64,7 +63,7 @@ func getInstalledEngineVersion(engineType engine.EngineType, version string) str
})
versionString, err := mockEngine.GetVersionString()
if err != nil {
logrus.Warn(err)
logger.Warn(err)
return "error"
}
return versionString
Expand Down
5 changes: 2 additions & 3 deletions cmd/workspace_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package cmd

import (
"gatehill.io/imposter/workspace"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
)
Expand Down Expand Up @@ -48,7 +47,7 @@ func init() {
func deleteWorkspace(dir string, name string) {
err := workspace.Delete(dir, name)
if err != nil {
logrus.Fatalf("failed to delete workspace: %s", err)
logger.Fatalf("failed to delete workspace: %s", err)
}
logrus.Infof("deleted workspace '%s'", name)
logger.Infof("deleted workspace '%s'", name)
}
5 changes: 2 additions & 3 deletions cmd/workspace_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package cmd
import (
"gatehill.io/imposter/workspace"
"github.com/olekukonko/tablewriter"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
)
Expand Down Expand Up @@ -47,11 +46,11 @@ func init() {
func listWorkspaces(dir string) {
workspaces, err := workspace.List(dir)
if err != nil {
logrus.Fatalf("failed to list workspaces: %s", err)
logger.Fatalf("failed to list workspaces: %s", err)
}
active, err := workspace.GetActive(dir)
if err != nil {
logrus.Fatalf("failed to list workspaces: %s", err)
logger.Fatalf("failed to list workspaces: %s", err)
}
var activeName string
if active != nil {
Expand Down
5 changes: 2 additions & 3 deletions cmd/workspace_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package cmd

import (
"gatehill.io/imposter/workspace"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
)
Expand Down Expand Up @@ -48,7 +47,7 @@ func init() {
func createWorkspace(dir string, name string) {
_, err := workspace.New(dir, name)
if err != nil {
logrus.Fatalf("failed to create new workspace: %s", err)
logger.Fatalf("failed to create new workspace: %s", err)
}
logrus.Infof("created workspace '%s'", name)
logger.Infof("created workspace '%s'", name)
}
Loading

0 comments on commit f7a49dd

Please sign in to comment.