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..23e7416 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,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 { 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())