From 2566ba981290c408dcff1e94714725b5bdb7996f Mon Sep 17 00:00:00 2001 From: "faizan.ali" Date: Thu, 14 May 2020 17:44:15 -0700 Subject: [PATCH 1/3] main: added structured log option [Fixes #39] --- README.md | 2 ++ main.go | 37 ++++++++++++++++++++++++++----------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 1e339c4..a5bc6a3 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/main.go b/main.go index 2d2422b..96130dc 100644 --- a/main.go +++ b/main.go @@ -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" ) @@ -39,17 +40,30 @@ 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() + // Init checks + if len(filename) == 0 { + log.Errorf("No configuration file provided") + flag.Usage() + os.Exit(ExitCodeFlagError) + } + if debug { log.SetLevel(log.DebugLevel) } @@ -60,15 +74,16 @@ func main() { } // TODO: Accept other logger inputs - log.SetHandler(text.New(os.Stderr)) - - log.Infof("Starting git2consul version: %s", version.Version) - - if len(filename) == 0 { - log.Error("No configuration file provided") - os.Exit(ExitCodeFlagError) + switch logfmt { + case "text": + log.SetHandler(text.New(os.Stderr)) + case "json": + log.SetHandler(json.New(os.Stderr)) } + //log.Infof("Starting git2consul version: %s", version.Version) + log.WithField("caller", "main").Infof("Starting git2consul version: %s", version.Version) + // Load configuration from file cfg, err := config.Load(filename) if err != nil { From 8b5307c9df0a44c867e504b4fc1e90aaff772bd9 Mon Sep 17 00:00:00 2001 From: "faizan.ali" Date: Thu, 21 May 2020 17:56:30 -0700 Subject: [PATCH 2/3] issue-39: removed commented line and changed errorf to error as per review comment --- main.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/main.go b/main.go index 96130dc..2b836b5 100644 --- a/main.go +++ b/main.go @@ -59,7 +59,7 @@ func main() { // Init checks if len(filename) == 0 { - log.Errorf("No configuration file provided") + log.Error("No configuration file provided") flag.Usage() os.Exit(ExitCodeFlagError) } @@ -81,7 +81,6 @@ func main() { log.SetHandler(json.New(os.Stderr)) } - //log.Infof("Starting git2consul version: %s", version.Version) log.WithField("caller", "main").Infof("Starting git2consul version: %s", version.Version) // Load configuration from file From 8928e4a557b56c3f276db58f7021650bc0bb8b25 Mon Sep 17 00:00:00 2001 From: "faizan.ali" Date: Sat, 23 May 2020 16:03:37 -0700 Subject: [PATCH 3/3] Fixed version print issue and added checks in e2e tests for validating two flags --- main.go | 10 +++++----- pkg/e2e/e2e_test.go | 28 +++++++++++++++++++++++++++- pkg/version/version.go | 2 ++ 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 2b836b5..23e7416 100644 --- a/main.go +++ b/main.go @@ -57,6 +57,11 @@ func main() { flag.StringVar(&logfmt, "logfmt", "text", "specify log format [text | json] ") flag.Parse() + if printVersion { + version.Print() + return + } + // Init checks if len(filename) == 0 { log.Error("No configuration file provided") @@ -68,11 +73,6 @@ func main() { log.SetLevel(log.DebugLevel) } - if printVersion { - version.Print() - return - } - // TODO: Accept other logger inputs switch logfmt { case "text": diff --git a/pkg/e2e/e2e_test.go b/pkg/e2e/e2e_test.go index 0f3d7e2..7592769 100644 --- a/pkg/e2e/e2e_test.go +++ b/pkg/e2e/e2e_test.go @@ -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", diff --git a/pkg/version/version.go b/pkg/version/version.go index 04521ec..dd38530 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -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())