package main

import (
	"flag"
	"github.com/rs/zerolog"
	"github.com/rs/zerolog/log"
	"golang.org/x/crypto/ssh/terminal"
	"os"
)

var Version string
var Buildtime string
var Builduser string
var Buildarch string

func main() {
	os.Exit(app())
}

func identify() {
	log.Debug().
		Str("version", Version).
		Str("buildarch", Buildarch).
		Str("buildtime", Buildtime).
		Str("builduser", Builduser).Send()
}

func app() int {
	if terminal.IsTerminal(int(os.Stdout.Fd())) {
		log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
	}

	debug := flag.Bool("debug", false, "sets log level to debug")
	flag.Parse()

	identify()

	log.Print("hello world")

	// Default level for this example is info, unless debug flag is present
	zerolog.SetGlobalLevel(zerolog.InfoLevel)
	if *debug {
		zerolog.SetGlobalLevel(zerolog.DebugLevel)
	}

	log.Debug().Msg("This message appears only when log level set to Debug")
	log.Info().Msg("This message appears when log level set to Debug or Info")

	return 0
}
