From 6924b7bf4c4b6b24d697d38a0f35ed244979c12e Mon Sep 17 00:00:00 2001 From: Juan Font Alonso Date: Tue, 12 Oct 2021 23:48:08 +0200 Subject: [PATCH 1/2] Output json when deleting node (fixes #152) --- cmd/headscale/cli/nodes.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cmd/headscale/cli/nodes.go b/cmd/headscale/cli/nodes.go index 5f30dc1..c01bce9 100644 --- a/cmd/headscale/cli/nodes.go +++ b/cmd/headscale/cli/nodes.go @@ -129,6 +129,7 @@ var deleteNodeCmd = &cobra.Command{ return nil }, Run: func(cmd *cobra.Command, args []string) { + output, _ := cmd.Flags().GetString("output") h, err := getHeadscaleApp() if err != nil { log.Fatalf("Error initializing: %s", err) @@ -153,11 +154,19 @@ var deleteNodeCmd = &cobra.Command{ if confirm { err = h.DeleteMachine(m) + if strings.HasPrefix(output, "json") { + JsonOutput(map[string]string{"Result": "Node deleted"}, err, output) + return + } if err != nil { log.Fatalf("Error deleting node: %s", err) } fmt.Printf("Node deleted\n") } else { + if strings.HasPrefix(output, "json") { + JsonOutput(map[string]string{"Result": "Node not deleted"}, err, output) + return + } fmt.Printf("Node not deleted\n") } }, From 27947c67462ad14519df4800e7817d2cb9f84a01 Mon Sep 17 00:00:00 2001 From: Juan Font Alonso Date: Wed, 13 Oct 2021 00:18:55 +0200 Subject: [PATCH 2/2] This commit disables the version checker when JSON output (#153) --- cmd/headscale/cli/utils.go | 9 +++++++++ cmd/headscale/headscale.go | 3 ++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/cmd/headscale/cli/utils.go b/cmd/headscale/cli/utils.go index f879f91..95555e9 100644 --- a/cmd/headscale/cli/utils.go +++ b/cmd/headscale/cli/utils.go @@ -262,3 +262,12 @@ func JsonOutput(result interface{}, errResult error, outputFormat string) { } fmt.Println(string(j)) } + +func HasJsonOutputFlag() bool { + for _, arg := range os.Args { + if arg == "json" || arg == "json-line" { + return true + } + } + return false +} diff --git a/cmd/headscale/headscale.go b/cmd/headscale/headscale.go index f815001..6b1a843 100644 --- a/cmd/headscale/headscale.go +++ b/cmd/headscale/headscale.go @@ -62,7 +62,8 @@ func main() { zerolog.SetGlobalLevel(zerolog.DebugLevel) } - if !viper.GetBool("disable_check_updates") { + jsonOutput := cli.HasJsonOutputFlag() + if !viper.GetBool("disable_check_updates") && !jsonOutput { if (runtime.GOOS == "linux" || runtime.GOOS == "darwin") && cli.Version != "dev" { githubTag := &latest.GithubTag{ Owner: "juanfont",