From a2d8eb42e7f54c289f5461053c946bf0a5e64692 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Mon, 9 Nov 2026 14:57:19 +0000 Subject: [PATCH] cli: print a config load error like every other command error initConfig logged it as one FTL line, newlines escaped, with the build path. Updates #1926 --- cmd/headscale/cli/root.go | 17 +++++++++++------ cmd/headscale/cli/root_test.go | 18 +++++++++++++++++- cmd/headscale/cli/serve_test.go | 2 +- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/cmd/headscale/cli/root.go b/cmd/headscale/cli/root.go index a84062fd9..13cad4de2 100644 --- a/cmd/headscale/cli/root.go +++ b/cmd/headscale/cli/root.go @@ -112,10 +112,10 @@ func initConfig() { if err != nil { // No log.format applies: main's console logger, on stderr. logWarnings(l.Warnings) - log.WithLevel(zerolog.FatalLevel).Caller().Err(err).Msg("error loading config") + // A Cobra initializer cannot return err to Execute; report it the same way. cmd, _, _ := rootCmd.Find(os.Args[1:]) - os.Exit(exitCode(cmd, l.File, err)) + exitWithError(cmd, l.File, err) } loaded = l @@ -202,14 +202,19 @@ https://github.com/juanfont/headscale`, func Execute() { cmd, err := rootCmd.ExecuteC() if err != nil { - outputFormat, _ := cmd.Flags().GetString("output") - printError(err, outputFormat) - var file string if loaded != nil { file = loaded.File } - os.Exit(exitCode(cmd, file, err)) + exitWithError(cmd, file, err) } } + +// exitWithError prints err in cmd's --output format and exits with +// exitCode; file is the configuration file loaded, if any. +func exitWithError(cmd *cobra.Command, file string, err error) { + outputFormat, _ := cmd.Flags().GetString("output") + printError(err, outputFormat) + os.Exit(exitCode(cmd, file, err)) +} diff --git a/cmd/headscale/cli/root_test.go b/cmd/headscale/cli/root_test.go index 5a7dbd759..4db0b25c8 100644 --- a/cmd/headscale/cli/root_test.go +++ b/cmd/headscale/cli/root_test.go @@ -611,6 +611,22 @@ func TestInitConfigWarnings(t *testing.T) { warnLine(t, stderr) assert.Contains(t, stderr, "unknown configuration key bogus_key") assert.NotContains(t, stderr, "panic") + + // Reported as Execute reports any error, in the --output format. + if output == nil { + assert.Contains(t, stderr, "Error: loading config file "+invalid+": ") + + return + } + + _, obj, ok := strings.Cut(stderr, "{") + require.True(t, ok, stderr) + + var out struct { + Error string `json:"error"` + } + require.NoError(t, json.Unmarshal([]byte("{"+obj), &out), stderr) + assert.Contains(t, out.Error, "unknown configuration key bogus_key") }) } } @@ -628,6 +644,6 @@ func TestClientCommandWithoutConfigFile(t *testing.T) { _, stderr, err := runHeadscale(t, env, "users", "list") require.Error(t, err, "no server listens on the socket") assert.Contains(t, stderr, "listing users") - assert.NotContains(t, stderr, "error loading config") + assert.NotContains(t, stderr, "loading config") assert.NotContains(t, stderr, "no config file") } diff --git a/cmd/headscale/cli/serve_test.go b/cmd/headscale/cli/serve_test.go index 00dc86891..49b4d2ac6 100644 --- a/cmd/headscale/cli/serve_test.go +++ b/cmd/headscale/cli/serve_test.go @@ -197,7 +197,7 @@ func TestServeExitCode(t *testing.T) { {name: "rejected value", config: "server_url: ftp://headscale.example.com\ndisable_check_updates: true\n", args: []string{"serve"}, want: exitConfig, stderr: "server_url"}, {name: "unknown key", config: valid + "bogus_key: 1\n", args: []string{"serve"}, want: exitConfig, stderr: "unknown configuration key bogus_key"}, {name: "syntax error", config: valid + "derp: [\n", args: []string{"serve"}, want: exitConfig, stderr: "yaml: line 3"}, - {name: "missing file", args: []string{"serve"}, want: 1, stderr: "error loading config"}, + {name: "missing file", args: []string{"serve"}, want: 1, stderr: "Error: loading config file"}, {name: "no file found", search: true, args: []string{"serve"}, want: 1, stderr: "server_url is required"}, {name: "old format found", file: "config.toml", config: "server_url = \"https://headscale.example.com\"\n", search: true, args: []string{"serve"}, want: exitConfig, stderr: "unsupported config file format"}, {name: "unwritable noise key", config: valid + "noise:\n private_key_path: " + notADir + "/noise.key\n", args: []string{"serve"}, want: 1, stderr: "Noise"},