Skip to content
This repository has been archived by the owner on Apr 23, 2022. It is now read-only.

Commit

Permalink
Merge pull request #47 from faizan82/issue-39
Browse files Browse the repository at this point in the history
main: added structured log option [Fixes #39]
  • Loading branch information
seanmalloy authored May 27, 2020
2 parents 1b27b2a + 26001c4 commit f7da486
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 14 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ Usage of git2consul:
path to config file
-debug
enable debugging mode
-logfmt string
specify log format [text | json] (default "text")
-once
run git2consul once and exit
-version
Expand Down
40 changes: 27 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/KohlsTechnology/git2consul-go/pkg/version"
"github.com/KohlsTechnology/git2consul-go/runner"
"github.com/apex/log"
"github.com/apex/log/handlers/json"
"github.com/apex/log/handlers/text"
)

Expand All @@ -39,36 +40,49 @@ const (
)

func main() {
var filename string
var printVersion bool
var debug bool
var once bool

var (
filename string
printVersion bool
debug bool
once bool
logfmt string
)

flag.StringVar(&filename, "config", "", "path to config file")
flag.BoolVar(&printVersion, "version", false, "show version")
flag.BoolVar(&debug, "debug", false, "enable debugging mode")
flag.BoolVar(&once, "once", false, "run git2consul once and exit")
// allow switching logformat. Structured output helps with parsers
flag.StringVar(&logfmt, "logfmt", "text", "specify log format [text | json] ")
flag.Parse()

if debug {
log.SetLevel(log.DebugLevel)
}

if printVersion {
version.Print()
return
}

// TODO: Accept other logger inputs
log.SetHandler(text.New(os.Stderr))

log.Infof("Starting git2consul version: %s", version.Version)

// Init checks
if len(filename) == 0 {
log.Error("No configuration file provided")
flag.Usage()
os.Exit(ExitCodeFlagError)
}

if debug {
log.SetLevel(log.DebugLevel)
}

// TODO: Accept other logger inputs
switch logfmt {
case "text":
log.SetHandler(text.New(os.Stderr))
case "json":
log.SetHandler(json.New(os.Stderr))
}

log.WithField("caller", "main").Infof("Starting git2consul version: %s", version.Version)

// Load configuration from file
cfg, err := config.Load(filename)
if err != nil {
Expand Down
28 changes: 27 additions & 1 deletion pkg/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,33 @@ func TestE2E(t *testing.T) {
t.Fatal("failed to get working project directory", err)
}

g2cCmd := exec.Command(projectDir+"/git2consul",
// Command line argument validations
// no flag provided
g2cCmd := exec.Command(projectDir + "/git2consul")
expectedNoConfigMsg := fmt.Sprintf("%v error No configuration file provided", time.Now().Format("2006/01/02 15:04:05"))
t.Log("Testig argument parsing...")
t.Logf("Expected output: %s \n", expectedNoConfigMsg)
err = executeCommand(g2cCmd, expectedNoConfigMsg)
if err != nil {
t.Fatal("git2consul run failed", err)
} else {
t.Log("git2consul ran successfully")
}

// version
g2cCmd = exec.Command(projectDir+"/git2consul",
"-version",
)
t.Logf("Expected output: %s \n", "git2consul, version")
err = executeCommand(g2cCmd, "git2consul, version")
if err != nil {
t.Fatal("git2consul run failed", err)
} else {
t.Log("git2consul ran successfully")
}

// Integration tests
g2cCmd = exec.Command(projectDir+"/git2consul",
"-config",
projectDir+"/pkg/e2e/data/create-config.json",
"-debug",
Expand Down
2 changes: 2 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ var (
// Print writes application version details to standard output.
func Print() {
// TODO remove hard coded "git2consul" string here
// TODO update e2e version test once "git2consul" as described
// above is removed
fmt.Printf("git2consul, version %v (branch: %v, revision: %v)\n", Version, Branch, GitSHA1)
fmt.Println("build date:", BuildDate)
fmt.Println("go version:", runtime.Version())
Expand Down

0 comments on commit f7da486

Please sign in to comment.