2021-07-25 15:10:34 +02:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2022-06-05 12:25:09 +02:00
|
|
|
"runtime"
|
2021-08-05 19:58:15 +02:00
|
|
|
|
2023-06-06 10:23:39 +02:00
|
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
2022-06-05 12:25:09 +02:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
2021-08-05 19:58:15 +02:00
|
|
|
"github.com/spf13/cobra"
|
2022-06-05 12:25:09 +02:00
|
|
|
"github.com/tcnksm/go-latest"
|
2021-07-25 15:10:34 +02:00
|
|
|
)
|
|
|
|
|
2023-01-26 08:41:21 +01:00
|
|
|
const (
|
|
|
|
deprecateNamespaceMessage = "use --user"
|
|
|
|
)
|
|
|
|
|
2022-06-05 12:25:09 +02:00
|
|
|
var cfgFile string = ""
|
|
|
|
|
2021-07-25 15:10:34 +02:00
|
|
|
func init() {
|
2023-01-26 08:41:21 +01:00
|
|
|
if len(os.Args) > 1 &&
|
|
|
|
(os.Args[1] == "version" || os.Args[1] == "mockoidc" || os.Args[1] == "completion") {
|
2022-09-20 21:59:22 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-05 12:25:09 +02:00
|
|
|
cobra.OnInitialize(initConfig)
|
|
|
|
rootCmd.PersistentFlags().
|
|
|
|
StringVarP(&cfgFile, "config", "c", "", "config file (default is /etc/headscale/config.yaml)")
|
2021-11-04 23:45:08 +01:00
|
|
|
rootCmd.PersistentFlags().
|
2021-11-07 09:58:45 +01:00
|
|
|
StringP("output", "o", "", "Output format. Empty for human-readable, 'json', 'json-line' or 'yaml'")
|
2021-11-13 09:36:45 +01:00
|
|
|
rootCmd.PersistentFlags().
|
|
|
|
Bool("force", false, "Disable prompts and forces the execution")
|
2021-07-25 15:10:34 +02:00
|
|
|
}
|
|
|
|
|
2022-06-05 12:25:09 +02:00
|
|
|
func initConfig() {
|
2022-08-21 10:22:15 +02:00
|
|
|
if cfgFile == "" {
|
|
|
|
cfgFile = os.Getenv("HEADSCALE_CONFIG")
|
|
|
|
}
|
2022-06-05 12:25:09 +02:00
|
|
|
if cfgFile != "" {
|
2023-06-06 10:23:39 +02:00
|
|
|
err := types.LoadConfig(cfgFile, true)
|
2022-06-07 16:24:35 +02:00
|
|
|
if err != nil {
|
2022-08-14 12:35:14 +02:00
|
|
|
log.Fatal().Caller().Err(err).Msgf("Error loading config file %s", cfgFile)
|
2022-06-05 12:25:09 +02:00
|
|
|
}
|
|
|
|
} else {
|
2023-06-06 10:23:39 +02:00
|
|
|
err := types.LoadConfig("", false)
|
2022-06-07 16:24:35 +02:00
|
|
|
if err != nil {
|
2022-08-14 12:35:14 +02:00
|
|
|
log.Fatal().Caller().Err(err).Msgf("Error loading config")
|
2022-06-05 12:25:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-06 10:23:39 +02:00
|
|
|
cfg, err := types.GetHeadscaleConfig()
|
2022-06-05 12:25:09 +02:00
|
|
|
if err != nil {
|
2023-07-26 11:53:42 +02:00
|
|
|
log.Fatal().Caller().Err(err).Msg("Failed to get headscale configuration")
|
2022-06-05 12:25:09 +02:00
|
|
|
}
|
|
|
|
|
2022-06-07 16:24:35 +02:00
|
|
|
machineOutput := HasMachineOutputFlag()
|
|
|
|
|
2022-09-11 21:37:23 +02:00
|
|
|
zerolog.SetGlobalLevel(cfg.Log.Level)
|
2022-06-07 16:24:35 +02:00
|
|
|
|
2022-06-05 12:25:09 +02:00
|
|
|
// If the user has requested a "machine" readable format,
|
|
|
|
// then disable login so the output remains valid.
|
|
|
|
if machineOutput {
|
|
|
|
zerolog.SetGlobalLevel(zerolog.Disabled)
|
|
|
|
}
|
|
|
|
|
2023-06-06 10:23:39 +02:00
|
|
|
if cfg.Log.Format == types.JSONLogFormat {
|
2022-06-18 14:00:47 +02:00
|
|
|
log.Logger = log.Output(os.Stdout)
|
|
|
|
}
|
|
|
|
|
2022-06-07 16:24:35 +02:00
|
|
|
if !cfg.DisableUpdateCheck && !machineOutput {
|
2022-06-05 12:25:09 +02:00
|
|
|
if (runtime.GOOS == "linux" || runtime.GOOS == "darwin") &&
|
|
|
|
Version != "dev" {
|
|
|
|
githubTag := &latest.GithubTag{
|
|
|
|
Owner: "juanfont",
|
|
|
|
Repository: "headscale",
|
|
|
|
}
|
|
|
|
res, err := latest.Check(githubTag, Version)
|
|
|
|
if err == nil && res.Outdated {
|
|
|
|
//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,
|
|
|
|
Version,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-25 15:10:34 +02:00
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "headscale",
|
|
|
|
Short: "headscale - a Tailscale control server",
|
|
|
|
Long: `
|
|
|
|
headscale is an open source implementation of the Tailscale control server
|
|
|
|
|
2021-08-05 19:58:15 +02:00
|
|
|
https://github.com/juanfont/headscale`,
|
2021-07-25 15:10:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func Execute() {
|
|
|
|
if err := rootCmd.Execute(); err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|