Simplify loglevel parser, turn off logs when machine output is set

This commit is contained in:
Kristoffer Dalby 2021-11-04 22:32:13 +00:00
parent cd34a5d6f3
commit 9e9049307e

View File

@ -46,24 +46,23 @@ func main() {
log.Fatal().Err(err) log.Fatal().Err(err)
} }
machineOutput := cli.HasMachineOutputFlag()
logLevel := viper.GetString("log_level") logLevel := viper.GetString("log_level")
switch logLevel { level, err := zerolog.ParseLevel(logLevel)
case "trace": if err != nil {
zerolog.SetGlobalLevel(zerolog.TraceLevel)
case "debug":
zerolog.SetGlobalLevel(zerolog.DebugLevel)
case "info":
zerolog.SetGlobalLevel(zerolog.InfoLevel)
case "warn":
zerolog.SetGlobalLevel(zerolog.WarnLevel)
case "error":
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
default:
zerolog.SetGlobalLevel(zerolog.DebugLevel) zerolog.SetGlobalLevel(zerolog.DebugLevel)
} else {
zerolog.SetGlobalLevel(level)
} }
jsonOutput := cli.HasJsonOutputFlag() // If the user has requested a "machine" readable format,
if !viper.GetBool("disable_check_updates") && !jsonOutput { // then disable login so the output remains valid.
if machineOutput {
zerolog.SetGlobalLevel(zerolog.Disabled)
}
if !viper.GetBool("disable_check_updates") && !machineOutput {
if (runtime.GOOS == "linux" || runtime.GOOS == "darwin") && cli.Version != "dev" { if (runtime.GOOS == "linux" || runtime.GOOS == "darwin") && cli.Version != "dev" {
githubTag := &latest.GithubTag{ githubTag := &latest.GithubTag{
Owner: "juanfont", Owner: "juanfont",
@ -71,8 +70,11 @@ func main() {
} }
res, err := latest.Check(githubTag, cli.Version) res, err := latest.Check(githubTag, cli.Version)
if err == nil && res.Outdated { if err == nil && res.Outdated {
fmt.Printf("An updated version of Headscale has been found (%s vs. your current %s). Check it out https://github.com/juanfont/headscale/releases\n", fmt.Printf(
res.Current, cli.Version) "An updated version of Headscale has been found (%s vs. your current %s). Check it out https://github.com/juanfont/headscale/releases\n",
res.Current,
cli.Version,
)
} }
} }
} }