From d45cc52468f60f4e65fd38f5fd29afc30f0505d6 Mon Sep 17 00:00:00 2001 From: Tobias Speicher Date: Tue, 31 Oct 2023 19:30:00 +0100 Subject: [PATCH 1/6] command version: add json output option --- cmd/restic/cmd_version.go | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/cmd/restic/cmd_version.go b/cmd/restic/cmd_version.go index b17103706..686bb9c0c 100644 --- a/cmd/restic/cmd_version.go +++ b/cmd/restic/cmd_version.go @@ -1,6 +1,7 @@ package main import ( + "encoding/json" "fmt" "runtime" @@ -21,8 +22,31 @@ Exit status is 0 if the command was successful, and non-zero if there was any er `, DisableAutoGenTag: true, Run: func(cmd *cobra.Command, args []string) { - fmt.Printf("restic %s compiled with %v on %v/%v\n", - version, runtime.Version(), runtime.GOOS, runtime.GOARCH) + if globalOptions.JSON { + type jsonVersion struct { + Version string `json:"version"` + GoVersion string `json:"go_version"` + GoTarget string `json:"go_target"` + } + + jsonS := jsonVersion{ + Version: version, + GoVersion: runtime.Version(), + GoTarget: runtime.GOOS + "/" + runtime.GOARCH, + } + + jsonB, err := json.Marshal(jsonS) + if err != nil { + Warnf("Marshall failed: %v\n", err) + return + } + + fmt.Println(string(jsonB)) + } else { + fmt.Printf("restic %s compiled with %v on %v/%v\n", + version, runtime.Version(), runtime.GOOS, runtime.GOARCH) + } + }, } From 6ca07ee0042c0f8407bdc5ae9330886363065824 Mon Sep 17 00:00:00 2001 From: Tobias Speicher Date: Tue, 31 Oct 2023 19:39:52 +0100 Subject: [PATCH 2/6] add changelog/unreleases for issue-4547 --- changelog/unreleased/issue-4547 | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 changelog/unreleased/issue-4547 diff --git a/changelog/unreleased/issue-4547 b/changelog/unreleased/issue-4547 new file mode 100644 index 000000000..37a0bdf71 --- /dev/null +++ b/changelog/unreleased/issue-4547 @@ -0,0 +1,6 @@ +Enhancement: add --json option to version command. + +Restic now supports outputting restic version and used go version and platform +target via json when using the version command + +https://github.com/restic/restic/issues/4547 From 6f1efcb28b4b93dd1802ad364006ac0633ac44ae Mon Sep 17 00:00:00 2001 From: Tobias Speicher Date: Wed, 1 Nov 2023 22:12:19 +0100 Subject: [PATCH 3/6] Update wording on changelog entry --- changelog/unreleased/issue-4547 | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/changelog/unreleased/issue-4547 b/changelog/unreleased/issue-4547 index 37a0bdf71..619923055 100644 --- a/changelog/unreleased/issue-4547 +++ b/changelog/unreleased/issue-4547 @@ -1,6 +1,7 @@ -Enhancement: add --json option to version command. +Enhancement: Add support for `--json` option to `version` command Restic now supports outputting restic version and used go version and platform -target via json when using the version command +target via json when using the version command. https://github.com/restic/restic/issues/4547 +https://github.com/restic/restic/pull/4553 From ab23d033b63b7413a0018417d245ad7c3fd5dcc3 Mon Sep 17 00:00:00 2001 From: Tobias Speicher Date: Wed, 1 Nov 2023 22:13:57 +0100 Subject: [PATCH 4/6] Add version command output to JSON format documentation --- doc/075_scripting.rst | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/doc/075_scripting.rst b/doc/075_scripting.rst index 71ecd4c2b..3be5cc4c9 100644 --- a/doc/075_scripting.rst +++ b/doc/075_scripting.rst @@ -576,3 +576,17 @@ The snapshots command returns a single JSON object. +------------------------------+-----------------------------------------------------+ | ``compression_space_saving`` | Overall space saving due to compression | +------------------------------+-----------------------------------------------------+ + + +version +------- + +The version command returns a single JSON object. + ++----------------+--------------------+ +| ``version`` | restic version | ++----------------+--------------------+ +| ``go_version`` | Go compile version | ++----------------+--------------------+ +| ``go_target`` | Go target platform | ++----------------+--------------------+ From 10cbc169c1bbeb9fd4e024415bf9e724d8023b01 Mon Sep 17 00:00:00 2001 From: Tobias Speicher Date: Wed, 1 Nov 2023 22:18:37 +0100 Subject: [PATCH 5/6] Use different function to be more consistent with other code --- cmd/restic/cmd_version.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/cmd/restic/cmd_version.go b/cmd/restic/cmd_version.go index 686bb9c0c..3c625abd5 100644 --- a/cmd/restic/cmd_version.go +++ b/cmd/restic/cmd_version.go @@ -35,13 +35,11 @@ Exit status is 0 if the command was successful, and non-zero if there was any er GoTarget: runtime.GOOS + "/" + runtime.GOARCH, } - jsonB, err := json.Marshal(jsonS) + err := json.NewEncoder(globalOptions.stdout).Encode(jsonS) if err != nil { - Warnf("Marshall failed: %v\n", err) + Warnf("Encode failed: %v\n", err) return } - - fmt.Println(string(jsonB)) } else { fmt.Printf("restic %s compiled with %v on %v/%v\n", version, runtime.Version(), runtime.GOOS, runtime.GOARCH) From ce53ea32c6ce5ee122a0d870ec4d1deb99652e48 Mon Sep 17 00:00:00 2001 From: Tobias Speicher Date: Wed, 1 Nov 2023 22:43:38 +0100 Subject: [PATCH 6/6] Split `go_target` into `go_os` and `go_arch` --- changelog/unreleased/issue-4547 | 4 ++-- cmd/restic/cmd_version.go | 8 +++++--- doc/075_scripting.rst | 4 +++- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/changelog/unreleased/issue-4547 b/changelog/unreleased/issue-4547 index 619923055..edb1cf693 100644 --- a/changelog/unreleased/issue-4547 +++ b/changelog/unreleased/issue-4547 @@ -1,7 +1,7 @@ Enhancement: Add support for `--json` option to `version` command -Restic now supports outputting restic version and used go version and platform -target via json when using the version command. +Restic now supports outputting restic version and used go version, OS and +architecture via JSON when using the version command. https://github.com/restic/restic/issues/4547 https://github.com/restic/restic/pull/4553 diff --git a/cmd/restic/cmd_version.go b/cmd/restic/cmd_version.go index 3c625abd5..73469750f 100644 --- a/cmd/restic/cmd_version.go +++ b/cmd/restic/cmd_version.go @@ -26,18 +26,20 @@ Exit status is 0 if the command was successful, and non-zero if there was any er type jsonVersion struct { Version string `json:"version"` GoVersion string `json:"go_version"` - GoTarget string `json:"go_target"` + GoOS string `json:"go_os"` + GoArch string `json:"go_arch"` } jsonS := jsonVersion{ Version: version, GoVersion: runtime.Version(), - GoTarget: runtime.GOOS + "/" + runtime.GOARCH, + GoOS: runtime.GOOS, + GoArch: runtime.GOARCH, } err := json.NewEncoder(globalOptions.stdout).Encode(jsonS) if err != nil { - Warnf("Encode failed: %v\n", err) + Warnf("JSON encode failed: %v\n", err) return } } else { diff --git a/doc/075_scripting.rst b/doc/075_scripting.rst index 3be5cc4c9..f17e222cc 100644 --- a/doc/075_scripting.rst +++ b/doc/075_scripting.rst @@ -588,5 +588,7 @@ The version command returns a single JSON object. +----------------+--------------------+ | ``go_version`` | Go compile version | +----------------+--------------------+ -| ``go_target`` | Go target platform | +| ``go_os`` | Go OS | ++----------------+--------------------+ +| ``go_arch`` | Go architecture | +----------------+--------------------+