2020-06-21 10:33:43 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-09-27 14:26:18 +00:00
|
|
|
"fmt"
|
2021-08-05 18:19:25 +00:00
|
|
|
"os"
|
2021-09-27 15:24:34 +00:00
|
|
|
"runtime"
|
2021-08-05 18:19:25 +00:00
|
|
|
"time"
|
2020-06-21 10:33:43 +00:00
|
|
|
|
2021-08-06 06:29:57 +00:00
|
|
|
"github.com/efekarakus/termcolor"
|
2021-04-28 14:15:45 +00:00
|
|
|
"github.com/juanfont/headscale/cmd/headscale/cli"
|
2021-08-05 18:19:25 +00:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/spf13/viper"
|
2021-09-27 14:26:18 +00:00
|
|
|
"github.com/tcnksm/go-latest"
|
2020-06-21 10:33:43 +00:00
|
|
|
)
|
|
|
|
|
2021-04-25 15:24:42 +00:00
|
|
|
func main() {
|
2021-08-06 06:29:57 +00:00
|
|
|
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 16:52:55 +00:00
|
|
|
case termcolor.LevelNone:
|
|
|
|
colors = false
|
2021-08-06 06:29:57 +00:00
|
|
|
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 18:19:25 +00:00
|
|
|
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
|
|
|
|
log.Logger = log.Output(zerolog.ConsoleWriter{
|
|
|
|
Out: os.Stdout,
|
|
|
|
TimeFormat: time.RFC3339,
|
2021-08-06 06:29:57 +00:00
|
|
|
NoColor: !colors,
|
2021-08-05 18:19:25 +00:00
|
|
|
})
|
|
|
|
|
2021-11-14 17:09:22 +00:00
|
|
|
if err := cli.LoadConfig(""); err != nil {
|
2022-01-25 22:11:15 +00:00
|
|
|
log.Fatal().Caller().Err(err)
|
2021-08-05 18:19:25 +00:00
|
|
|
}
|
|
|
|
|
2021-11-04 22:32:13 +00:00
|
|
|
machineOutput := cli.HasMachineOutputFlag()
|
|
|
|
|
2021-08-05 18:19:25 +00:00
|
|
|
logLevel := viper.GetString("log_level")
|
2021-11-04 22:32:13 +00:00
|
|
|
level, err := zerolog.ParseLevel(logLevel)
|
|
|
|
if err != nil {
|
2021-08-05 18:19:25 +00:00
|
|
|
zerolog.SetGlobalLevel(zerolog.DebugLevel)
|
2021-11-04 22:32:13 +00:00
|
|
|
} 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)
|
2021-04-27 00:30:06 +00:00
|
|
|
}
|
2021-04-24 02:54:15 +00:00
|
|
|
|
2021-11-04 22:32:13 +00:00
|
|
|
if !viper.GetBool("disable_check_updates") && !machineOutput {
|
2021-11-13 08:36:45 +00:00
|
|
|
if (runtime.GOOS == "linux" || runtime.GOOS == "darwin") &&
|
|
|
|
cli.Version != "dev" {
|
2021-09-27 15:24:34 +00:00
|
|
|
githubTag := &latest.GithubTag{
|
|
|
|
Owner: "juanfont",
|
|
|
|
Repository: "headscale",
|
|
|
|
}
|
|
|
|
res, err := latest.Check(githubTag, cli.Version)
|
|
|
|
if err == nil && res.Outdated {
|
2021-11-15 18:36:02 +00:00
|
|
|
//nolint
|
2021-11-04 22:32:13 +00:00
|
|
|
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-09-27 15:24:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-25 13:14:09 +00:00
|
|
|
cli.Execute()
|
2021-02-21 00:30:03 +00:00
|
|
|
}
|