mirror of
https://github.com/juanfont/headscale.git
synced 2026-11-24 22:26:29 +09:00
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
This commit is contained in:
1 parent
3e23f167c9
commit
a2d8eb42e7
3 files changed
+29
-8
No files matched your search
@@ -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))
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
@@ -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"},
|
||||
|
||||
Reference in new issue
Block a user