1
0
mirror of https://github.com/juanfont/headscale.git synced 2024-10-17 20:05:55 +02:00
juanfont.headscale/cmd/headscale/headscale.go

87 lines
2.0 KiB
Go
Raw Normal View History

2020-06-21 12:33:43 +02:00
package main
import (
2021-09-27 16:26:18 +02:00
"fmt"
2021-08-05 20:19:25 +02:00
"os"
"runtime"
2021-08-05 20:19:25 +02:00
"time"
2020-06-21 12:33:43 +02:00
"github.com/efekarakus/termcolor"
"github.com/juanfont/headscale/cmd/headscale/cli"
2021-08-05 20:19:25 +02:00
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
2021-09-27 16:26:18 +02:00
"github.com/tcnksm/go-latest"
2020-06-21 12:33:43 +02:00
)
2021-04-25 17:24:42 +02:00
func main() {
var colors bool
switch l := termcolor.SupportLevel(os.Stderr); l {
case termcolor.Level16M:
colors = true
case termcolor.Level256:
colors = true
case termcolor.LevelBasic:
colors = true
2021-11-14 17:52:55 +01:00
case termcolor.LevelNone:
colors = false
default:
// no color, return text as is.
colors = false
}
// Adhere to no-color.org manifesto of allowing users to
// turn off color in cli/services
if _, noColorIsSet := os.LookupEnv("NO_COLOR"); noColorIsSet {
colors = false
}
2021-08-05 20:19:25 +02:00
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
log.Logger = log.Output(zerolog.ConsoleWriter{
Out: os.Stdout,
TimeFormat: time.RFC3339,
NoColor: !colors,
2021-08-05 20:19:25 +02:00
})
2021-11-14 18:09:22 +01:00
if err := cli.LoadConfig(""); err != nil {
log.Fatal().Caller().Err(err)
2021-08-05 20:19:25 +02:00
}
machineOutput := cli.HasMachineOutputFlag()
2021-08-05 20:19:25 +02:00
logLevel := viper.GetString("log_level")
level, err := zerolog.ParseLevel(logLevel)
if err != nil {
2021-08-05 20:19:25 +02:00
zerolog.SetGlobalLevel(zerolog.DebugLevel)
} else {
zerolog.SetGlobalLevel(level)
}
// If the user has requested a "machine" readable format,
// then disable login so the output remains valid.
if machineOutput {
zerolog.SetGlobalLevel(zerolog.Disabled)
}
if !viper.GetBool("disable_check_updates") && !machineOutput {
2021-11-13 09:36:45 +01:00
if (runtime.GOOS == "linux" || runtime.GOOS == "darwin") &&
cli.Version != "dev" {
githubTag := &latest.GithubTag{
Owner: "juanfont",
Repository: "headscale",
}
res, err := latest.Check(githubTag, cli.Version)
if err == nil && res.Outdated {
2021-11-15 19:36:02 +01:00
//nolint
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",
res.Current,
cli.Version,
)
}
}
}
2021-07-25 15:14:09 +02:00
cli.Execute()
}